0

I am getting very frustrated with this one problem I have about the pearson correlation coefficient. I have a program that outputs stock values for two tickers for each day for a specified period of time and I also draw a graph of those values. You can choose between USD, EUR and SEK for output. However, my pearson correlation coefficient seems to be different depending on what currency I choose? I can't figure out if that is correct or if there's an error somewhere? The actual function that does the calculation seems to be OK, so I don't understand...

Should the coefficient be different or should it be the same depending on currency? EUR and SEK currency values for the stocks are calculated by multiplying the USD value of the stock with the close value for either EUR or SEK for that same day.

This method caculates the coefficient:

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;


}

The stock close values are fetched from yahoo finance through .csv files and these loops get the values and add them to an 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++;
}

And. buf3 is a stringbuffer reading the currency rates 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 and ticker2List are later the lists sent to the above method for calculation.

OUTPUT samples:

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

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, 8.42055 }

gives

ticker1List = {4562, 4485, 4532, 4600, 4634, 4722, 4808, 4826, 4781, 4841, 4777, 4859, 4752, 4752, 4839, 4751, 4902, 4826, 4917, 4848, 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, 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}

Pearson: 0.20617640237659246

4
  • Please add some code, at least the conversion code - and preferably also the pearson calculation one
    – amit
    Mar 21, 2017 at 17:55
  • Code added. Any ideas?
    – Matheos
    Mar 21, 2017 at 18:34
  • Do you have some output samples showing the differences and the magnitude of the differences? One would expect that the currency used to measure a stock would be a simple transformation from one to another hence would not affect Pearson's. However are the stock prices actually the same, just in a different currency, or are they from different markets hence if the stock prices were converted to a standard unit of currency, the stock prices for the same stock would be different because they come from different markets? Mar 21, 2017 at 18:43
  • Output sample provided for the time span 20.2.2015-29.7.2015 where ticker1 = googl and ticker2 = msft all values are fetched from yahoo finance and for the same time period
    – Matheos
    Mar 21, 2017 at 20:08

1 Answer 1

1

Your implementation works only if both stocks are quoted in the same currency, as they are both being converted via the same FX rate time series (buf3 in your code). The correlation when they are both in their native currency can differ from when they are both converted to another currency because the FX rate is not constant. For example, if both stocks are in EUR, and you calculated their correlation in USD, you are calculating the correlation that a USD-based investor would experience after converting dollars to euros, then purchasing the stocks. Part of the fluctuation in the time series is due to the movement in the EURUSD exchange rate, not just the stock price. By comparison, the EUR-based investor in both assets would only experience the correlation of the stock prices, and would have no exposure to the FX rate.

It really depends on what you are trying to accomplish here. Your code won't handle stocks which are quoted natively in different currencies, for example. If that were the case, the correlation between the asset prices after conversion to a common currency would be the same, regardless of the currency in which you calculate the correlation, as long as the FX rates are consistent and assuming perfect precision. If asset1 is quoted in EUR and asset2 is quoted in USD, you would get the same correlation in the following cases:

  1. Correl(asset1 in USD, asset2)
  2. Correl(asset1, asset2 in EUR)
  3. Correl(asset1 in SEK, asset2 in SEK)

Even when comparing assets in different currencies, there are reasonable applications of computing their correlations without FX rates at all, i.e. asset1 in EUR vs asset2 in USD. That would result in a different correlation from those above.

3
  • What I am calculating at all times Are correlation between stocks in same currencies. Never two different currencies. For ex if I select my currency to be USD, I simply send the USD close values of both stocks to the method and get a result. If I select SEK, I firstly get the USD close values for both stocks, and then I mutiply every single value in both stock value lists by the exchange rate associated with that day. In other words, index1 in both lists are mutiplied by the same value, index2 in both are mutiplied with another value and so on. As u say. the pearsons dnt match, shld they?
    – Matheos
    Mar 21, 2017 at 21:51
  • the stock values are ALWAYS by default, in USD. In other words, for SEK and EUR, conversion is needed but nothing to be done if USD.
    – Matheos
    Mar 21, 2017 at 22:01
  • 1
    @Hackermaan Understood. The correlation between two asset price time series in the same native quote currency (USD, in your case) should not be the same as the correlation between the same time series pair after converting to another currency. The FX rates that you use will alter the correlation. The only exception is if the FX rate does not change over the time series (i.e. you use one FX rate). Mar 22, 2017 at 2:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.