不同货币的皮尔逊相关系数不同?
Pearson Correlation Coefficient different for different currencies?
我对皮尔逊相关系数的这个问题感到非常沮丧。
我有一个程序可以在指定的时间段内每天输出两个代码的股票值,我还绘制了这些值的图表。您可以在 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 获取的,这些循环获取这些值并将它们添加到数组列表中。
//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是后面发送给上述方法计算的列表
输出样本:
美元
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 }
皮尔逊:0.1439484634863799
瑞典克朗
Currency rate = {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.09872, 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.62343, 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=15, 5}[3.420]
给予
ticker1List = {4562, 4485, 4532, 4600, 4634, 4722, 4808, 4826, 4781, 4841, 4777, 4859, 4752, 4752, 4839, 4751, 4902, 4826, 4898, 4, 4 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,4468,4461,4543,4543,4476,4476,4492,4525,4525,4525,4482,4482,4520,4520,4520,4520 4687、4587、4557、4561、4499、4576、4548、4497、4455、4446、4470、4470、4528、4528、4541、4541、4585、4610、4594 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, 401, 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}
皮尔逊:0.20617640237659246
只有当两只股票都以相同的货币报价时,您的实施才有效,因为它们都通过相同的外汇汇率时间序列(您的代码中的 buf3)进行转换。当它们都以本国货币表示时的相关性可能不同于它们都转换为另一种货币时的相关性,因为外汇汇率不是恒定的。例如,如果两只股票都以欧元计价,而您以美元计算它们的相关性,那么您计算的是美元投资者在将美元兑换成欧元然后购买股票后所经历的相关性。时间序列的部分波动是由于 EURUSD 汇率的变动,而不仅仅是股票价格。相比之下,这两种资产的欧元投资者只会体验到股票价格的相关性,而不会受到外汇汇率的影响。
这实际上取决于您要在这里完成的任务。例如,您的代码不会处理以不同货币本地报价的股票。如果是这种情况,只要外汇汇率保持一致并假设完美精确,转换为通用货币后资产价格之间的相关性将是相同的,无论您计算相关性的货币是什么。如果资产 1 以欧元报价,资产 2 以美元报价,在以下情况下您将获得相同的相关性:
- 相关性(美元资产 1,资产 2)
- 相关性(欧元资产 1、资产 2)
- 相关性(瑞典克朗资产 1,瑞典克朗资产 2)
即使在比较不同货币的资产时,也有一些合理的应用来计算它们之间的相关性,而根本没有汇率,即 asset1 以欧元计价 asset2 以美元计价。这将导致与上述不同的相关性。
我对皮尔逊相关系数的这个问题感到非常沮丧。 我有一个程序可以在指定的时间段内每天输出两个代码的股票值,我还绘制了这些值的图表。您可以在 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 获取的,这些循环获取这些值并将它们添加到数组列表中。
//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是后面发送给上述方法计算的列表
输出样本:
美元
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 }
皮尔逊:0.1439484634863799
瑞典克朗
Currency rate = {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.09872, 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.62343, 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=15, 5}[3.420]
给予
ticker1List = {4562, 4485, 4532, 4600, 4634, 4722, 4808, 4826, 4781, 4841, 4777, 4859, 4752, 4752, 4839, 4751, 4902, 4826, 4898, 4, 4 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,4468,4461,4543,4543,4476,4476,4492,4525,4525,4525,4482,4482,4520,4520,4520,4520 4687、4587、4557、4561、4499、4576、4548、4497、4455、4446、4470、4470、4528、4528、4541、4541、4585、4610、4594 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, 401, 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}
皮尔逊:0.20617640237659246
只有当两只股票都以相同的货币报价时,您的实施才有效,因为它们都通过相同的外汇汇率时间序列(您的代码中的 buf3)进行转换。当它们都以本国货币表示时的相关性可能不同于它们都转换为另一种货币时的相关性,因为外汇汇率不是恒定的。例如,如果两只股票都以欧元计价,而您以美元计算它们的相关性,那么您计算的是美元投资者在将美元兑换成欧元然后购买股票后所经历的相关性。时间序列的部分波动是由于 EURUSD 汇率的变动,而不仅仅是股票价格。相比之下,这两种资产的欧元投资者只会体验到股票价格的相关性,而不会受到外汇汇率的影响。
这实际上取决于您要在这里完成的任务。例如,您的代码不会处理以不同货币本地报价的股票。如果是这种情况,只要外汇汇率保持一致并假设完美精确,转换为通用货币后资产价格之间的相关性将是相同的,无论您计算相关性的货币是什么。如果资产 1 以欧元报价,资产 2 以美元报价,在以下情况下您将获得相同的相关性:
- 相关性(美元资产 1,资产 2)
- 相关性(欧元资产 1、资产 2)
- 相关性(瑞典克朗资产 1,瑞典克朗资产 2)
即使在比较不同货币的资产时,也有一些合理的应用来计算它们之间的相关性,而根本没有汇率,即 asset1 以欧元计价 asset2 以美元计价。这将导致与上述不同的相关性。