views:

147

answers:

1

I am sure it's an obvious error with my logic, but I can't seem to work out what I am doing wrong. Quite simply I have an array list of security codes, I want to compute the correlation between each combination of security codes. My code is as follows:

private void crossCorr(ArrayList<String> codes, HashMap<String, Stock> stockMap){

 // iterate through the codes list and get all the cross correlations:

 Iterator<String> itr1 = codes.iterator();
 while(itr1.hasNext()){
  String code1 = itr1.next();
System.out.println("  -----  " +code1);
  Iterator<String> itr2 = codes.iterator();
  ArrayList<Double> corrs = new ArrayList<Double>();
  HashMap<String, Double> codeCorr = new HashMap<String, Double>();
  String code2="";
  double corr=-2;
  while(itr2.hasNext()){
   code2 = itr2.next();
   Maths calcs = new Maths();  
   corr = calcs.getCorrelation(stockMap.get(code1).getRorHistory(), stockMap.get(code2).getRorHistory());
   corrs.add(corr);  // we order this list and then use this ordered list
        // to find the codes for the stocks with the highest
        // correlation for any given stock
   codeCorr.put(code2, corr);
   System.out.println(code1+" - "+code2+" - Correlation is "+corr);
  }
  orderCorrs(6, codeCorr, corrs, code1);
 }

}

The output I get is --

----- GOOG
GOOG - GOOG - Correlation is 1.0000000000000002
GOOG - YHOO - Correlation is 0.6986623807707519
GOOG - MSFT - Correlation is 0.7275411317567286
GOOG - CSCO - Correlation is 0.8122979333663799
GOOG - AAPL - Correlation is 0.8217785260604609
GOOG - ADBE - Correlation is 0.6102679356472099
GOOG - DISH - Correlation is 0.644852624453125
GOOG - NSOL - Correlation is 0.11600387177879072
GOOG - SBUX - Correlation is 0.6694306410719489
GOOG - PSFT - Correlation is -0.09921822861087544
GOOG - XOM - Correlation is 0.6728272039489009
GOOG - WMT - Correlation is 0.4004364090315347
GOOG - IBM - Correlation is 0.7559988282095168
GOOG - JPM - Correlation is 0.7085525367336528
GOOG - DNA - Correlation is 0.13628949379947575
GOOG - HPQ - Correlation is 0.7819350018750656
GOOG - KO - Correlation is 0.5700932682157461
GOOG - VZ - Correlation is 0.737881573641585
GOOG - INTC - Correlation is 0.7654127298771953
GOOG - SPY - Correlation is 0.8042488406758052
GOOG - PEP - Correlation is 0.6297924741882344
GOOG - WFC - Correlation is 0.5064491351161948
GOOG - ABT - Correlation is 0.238752389446595
GOOG - QCOM - Correlation is 0.726356709262025
GOOG - COP - Correlation is 0.570604981251932
GOOG - MCD - Correlation is 0.5434872575410538

But I never get

YHOO - GOOG - Correlation is .... etc. etc.

I am sure this is a simple error and for some reason I am not picking it up.

I know I am doing the correlation calculations twice, I will fix this once I get this part working as intended.

Please let me know if I need to provide more info.

+3  A: 

A quick test application having removed references to code we don't know about works fine for me.

Is it possible that something's throwing an unchecked exception which you're not handling? If you could post a short but complete program demonstrating the problem, that would help immensely. Take a copy of your full project, and just remove bits (e.g. hard-coding data and return values) until you can't take anything else away without the problem going away too. At that point you may well have found the problem for yourself, of course.

By the way, if you're using Java 1.5 or greater, it's simpler to use an extended for loop than explicitly using an iterator. You'd do:

for (String code1 : codes) {
    // ...
    for (String code2 : codes) {
        // ...
    }
}
Jon Skeet
Jon, do you mean for(String code1 : codes) { ... with the extra "s". If not what is code? Sorry for my ignorance
Ankur
Oops, yes. Fixing...
Jon Skeet
Np, I checked your edit. That's probably the way to go, commenting out what is non essential, and then slowly adding each piece to isolate the issue. Thanks, I'll get there.
Ankur