不同货币的Pearson相关系数不同?

时间:2017-03-21 17:47:44

标签: java correlation pearson pearson-correlation

我对这个关于皮尔森相关系数的问题感到非常沮丧。 我有一个程序,在指定的时间段内为每天的两个代码输出股票值,我还绘制了这些值的图表。您可以选择USD,EUR和SEK作为输出。但是,根据我选择的货币,我的皮尔森相关系数似乎有所不同? 我无法弄清楚这是否正确或某处是否有错误?计算的实际功能似乎没问题,所以我不明白......

系数应该是不同的,还是应该根据货币相同?股票的欧元和瑞典克朗货币价值是通过将该股票的美元价值与当天的欧元或瑞典克朗的收盘价相乘来计算的。

此方法计算系数:

public double CorrelationCalc(ArrayList<Integer> list1, ArrayList<Integer> list2) {
    double sumX= 0.0, sumY = 0.0, sumXX = 0.0, sumYY = 0.0, sumXY = 0.0;

    int length = list1.size();

    for (int i=0; i<length; i++) {
        int x = list1.get(i);
        int y = list2.get(i);

        sumX+=x;
        sumY+=y;
        sumXX+=x*x;
        sumYY+=y*y;
        sumXY+=x*y;
    }

        double cov = sumXY / length - sumX*sumY / length / length;

        double sigmaX = Math.sqrt(sumXX / length - sumX*sumX / length / length);
        double sigmaY = Math.sqrt(sumYY / length - sumY*sumY / length / length);

        return cov/sigmaX/sigmaY;


}

股票收盘价是通过.csv文件从yahoo finance获取的,这些循环获取值并将它们添加到arraylist。

//This one is for when USD is selected. (Close value for each date located 
//at index 4 on each line in the csv file)
while ((line = buf.readLine())!=null && (line2 = buf2.readLine())!= null) {
    if(n != 0) { //First row contains text only
        close1 = (Math.round(100 * Double.parseDouble(line.split(",")[4]))/100.0);
        close2 = Math.round(100 * Double.parseDouble(line2.split(",")[4]))/100.0;

        ticker1List.add((int) Math.round(close1));
        ticker2List.add((int) Math.round(close2));
    }
    n++;
}

和。 buf3是一个读取货币汇率csv

的字符串缓冲区
//This one is for non USD (SEK or EUR). multiplies the values with the currency rate
while ((line = buf.readLine())!=null && (line2 = buf2.readLine())!= null && (line3 = buf3.readLine())!= null) {
    if(n != 0) {//First row contains text only
            while (!line.split(",")[0].equals(line3.split(",")[0])) { //Makes sure that dates match
                line3 = buf3.readLine();
            }
        close1 = (Math.round(100 * Double.parseDouble(line.split(",")[4])*Double.parseDouble(line3.split(",")[4]))/100.0);
        close2 = Math.round(100 * Double.parseDouble(line2.split(",")[4])*Double.parseDouble(line3.split(",")[4]))/100.0;


        ticker1List.add((int) Math.round(close1));
        ticker2List.add((int) Math.round(close2));
    }
    n++;
}

ticker1List和ticker2List是稍后发送到上述方法进行计算的列表。

输出样本:

USD

ticker1List = {542,535,539,547,559,563,575,579,578,581,573,574,560,556,561,553,562,558,566,564,565,565, 578,567,564,558,561,555,549,541,544,545,549,548,549,549,540,541,544,533,545,543,549,557,574,566,564, 561,549,551,553,543,535,542,549,546,539,539,549,546,547,549,553,557,555,547,554,554,545,549,554,555, 552,550,543,542,553,550,547,543,545,547,556,558,560,563,559,558,553,541,540,543,547,546,550,542,545, 556,572,584,584,602,700,693,695,695,675,655,658,660,661} ticker2List = {44,44,44,44,44,44,44,43,43,43,42,43,42,42,41,41,42,42,43,42,43,43,43,41 ,41,41,41,41,41,40,42,42,41,41,42,42,42,42,42,42,43,43,43,43,48,48,49,49,49 ,49,48,48,46,47,48,47,47,48,49,48,48,48,48,47,47,47,48,47,47,47,47,47,46,46 ,46,46,47,46,46,45,46,46,47,46,46,46,46,46,45,44,44,44,44,44,44,44,45,45,46 ,46,46,47,47,47,47,46,46,46,45,45,46}

Pearson:0.1439484634863799

SEK

货币汇率= {8.55523,8.509,8.5914,8.564,8.5947,8.5379,8.6162,8.6554,8.54502,8.5075,8.5015,8.4947,8.4503,8.4634,8.46479,8.51185,8.4701,8.51984,8.38558,8.2967,8.23152,8.4052 ,8.24647,8.23443,8.253,8.13911,8.11373,8.12167,8.07872,8.17692,8.1593,8.20512,8.2148,8.26875,8.28135,8.29895,8.3914,8.2918,8.31362,8.4409,8.58413,8.548,8.4556,8.51005,8.4676,8.4153,8.33381 ,8.3411,8.3424,8.22841,8.19809,8.24101,8.23992,8.3265,8.30831,8.25121,8.2754,8.2294,8.3507,8.36978,8.40981,8.3328,8.34131,8.5058,8.60651,8.6354,8.6344,8.72934,8.6574,8.69648,8.63365,8.58971 ,8.714,8.7647,8.81522,8.81353,8.75727,8.6755,8.6743,8.5591,8.52942,8.65399,8.6243,8.5889,8.586,8.58851,8.493,8.51087,8.50157,8.61801,8.6706,8.6013,8.68419,8.6546,8.7287,8.5913,8.634343 ,8.55167,8.4879,8.4632,8.33779,8.32539,8.2671,8.3381,8.36064,8.3922,8.28541,8.40471,8.4139,8.3832,8.42055}

给出

ticker1List = {4562,4485,4532,4600,4634,4722,4808,4826,4781,4841,4777,4859,4752,4752,4839,4751,4902,4826,4917,4488,4898,4872, 4910,4826,4787,4789,4818,4764,4739,4684,4640,4664,4761,4754,4804,4835,4758,4742,4736,4576,4701,4721,4754,4866,4953,4889,4857, 4775,4577,4593,4649,4545,4468,4461,4543,4503,4476,4492,4525,4504,4482,4520,4609,4644,4621,4605,4693,4716,4611,4695,4755,4687, 4587,4557,4561,4499,4576,4548,4497,4455,4446,4470,4504,4528,4541,4585,4610,4594,4561,4549,4445,4508,4590,4649,4659,4611,4610, 4707,4831,4962,4965,5120,5978,5997,5991,5935,5799,5607,5655,5613,5659}

ticker2List = {369,370,371,370,365,368,367,361,356,359,353,363,357,359,354,356,363,361,369,364,372,369, 365,353,350,352,352,349,351,349,354,355,359,360,365,368,367,370,367,358,370,371,372,378,413,415,423, 417,406,405,406,398,386,384,395,391,393,397,301,398,394,392,397,396,391,392,403,404,396,404,403,395, 385,383,384,379,386,384,378,373,374,376,378,374,375,374,377,376,373,373,363,369,372,378,375,377,377, 378,385,388,389,397,398,406,407,389,396,393,390,386,396}

Pearson:0.20617640237659246

1 个答案:

答案 0 :(得分:0)

只有当两种股票都以相同的货币报价时,您的实施才有效,因为它们都是通过相同的汇率时间序列(代码中的buf3)转换的。当它们都是本国货币时的相关性可能与它们转换为另一种货币时的相关性不同,因为外汇汇率不是恒定的。例如,如果两只股票均为欧元,并且您计算了它们与美元的相关性,那么您正在计算美元投资者在将美元兑换成欧元,然后购买股票后所经历的相关性。时间序列波动的部分原因是欧元兑美元汇率的变动,而不仅仅是股票价格。相比之下,两种资产中基于欧元的投资者只会经历股票价格的相关性,并且不会接触外汇汇率。

这实际上取决于你在这里想要完成的事情。例如,您的代码不会处理以不同货币本地报价的股票。如果是这种情况,只要外汇汇率保持一致并且假设完美精确,转换为普通货币后的资产价格之间的相关性将是相同的,无论您计算相关性的货币如何。如果asset1以EUR报价且asset2以美元报价,则在以下情况下您将获得相同的相关性:

  1. Correl(资产1美元,资产2)
  2. Correl(资产1,资产2,欧元)
  3. Correl(资产1在瑞典克朗,资产2在瑞典克朗)
  4. 即使比较不同货币的资产,也可以合理地计算其相关性而无需外汇汇率,即资产1欧元兑资产2美元。这将导致与上述不同的相关性。