简单的迭代器循环会产生意想不到的结果

时间:2022-09-06 18:55:01

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

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

But I never get

但我永远不会

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

YHOO - GOOG - 相关性......等等。

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.

如果我需要提供更多信息,请告诉我。

1 个解决方案

#1


3  

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:

顺便说一句,如果你使用Java 1.5或更高版本,使用扩展for循环比使用迭代器显式更简单。你这样做:

for (String code1 : codes) {
    // ...
    for (String code2 : codes) {
        // ...
    }
}

#1


3  

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:

顺便说一句,如果你使用Java 1.5或更高版本,使用扩展for循环比使用迭代器显式更简单。你这样做:

for (String code1 : codes) {
    // ...
    for (String code2 : codes) {
        // ...
    }
}