/***************************************************************************** * FUNCTION: GetPPCCoefficients * Description: * Parameters: * pItemIndex - *****************************************************************************/ public Dictionary <int, Double> GetPPCCoefficients(int pItemIndex) { Dictionary <int, Double> return_list = new Dictionary <int, Double>(); int i, hash_key; for (i = 0; i < constituents.Count(); i++) { if (i != pItemIndex && CorrelationCoefficients != null) { hash_key = Helpers.getHash(i + 1, pItemIndex + 1, this.constituents.Count()); return_list.Add(i, CorrelationCoefficients[hash_key]); } } return(return_list); }
/***************************************************************************** * FUNCTION: ShiftedPearsonProductCoefficient * * Description: Calculates the Pearson Product Coefficient (PPC) for each * Equity in pInputList based on it's HistoricalPrice list, against * every other Equity in pInputList, after shifting its HistoricalPrice * left by pShiftValue. * * The intent is to find a predictive correlation. * ie. if one price-series tends to lag another by 1 trading day * (in terms of its trend), then shifting that series left by 1 and * calculating the PPC with the other series ought to yield a strong * PPC correlation. See Helpers.PearsonProductCoefficient() for the * normal PPC calculation. * * Parameters: * pInputList - the input data, consisting of multiple Equity instances * pShiftValue - the number of HistoricalPriceDates to shift data by before * computing the PPC. *****************************************************************************/ public static Double[,] ShiftedPearsonProductCoefficient(List <Equity> pInputList, int pShiftValue) { int N, cSize, i, j, k, count, key1, key2; Double coeff, numerator, denominator; Double sumSqRange1, sumSqRange2, meanRange1, meanRange2; Double[,] CorrelationCoefficients; List <Equity> temp_list; N = pInputList.Count(); cSize = N * (N - 1) / 2; CorrelationCoefficients = new Double[cSize, 2]; if (N > 0 && pShiftValue >= 0 && pShiftValue < N) { count = 1; for (i = 0; i < N; i++) { coeff = 0; denominator = 0; meanRange1 = 0; meanRange2 = 0; temp_list = new List <Equity>(pInputList); temp_list[i].TrimDataLeft(pShiftValue); for (j = 0; j < N; j++) { temp_list[j].TrimDataRight(pShiftValue); if (j >= i) { key2 = 0; } else { key2 = 1; } if (pInputList[i].HistoricalPrice.Count() == pInputList[j].HistoricalPrice.Count()) { sumSqRange1 = 0; sumSqRange2 = 0; numerator = 0; meanRange1 = pInputList[i].avgPrice; meanRange2 = pInputList[j].avgPrice; for (k = 0; k < pInputList[j].HistoricalPrice.Count(); k++) { sumSqRange1 += Math.Pow(pInputList[i].HistoricalPrice[k], 2); sumSqRange2 += Math.Pow(pInputList[j].HistoricalPrice[k], 2); numerator += (pInputList[i].HistoricalPrice[k] * pInputList[j].HistoricalPrice[k]); } numerator -= (pInputList[j].HistoricalPrice.Count() * meanRange1 * meanRange2); denominator = Math.Pow((sumSqRange1 - (pInputList[i].HistoricalPrice.Count() * Math.Pow(meanRange1, 2))), 0.5) * Math.Pow((sumSqRange2 - (pInputList[j].HistoricalPrice.Count() * Math.Pow(meanRange2, 2))), 0.5); coeff = numerator / denominator; } key1 = Helpers.getHash(i + 1, j + 1, pInputList.Count()); CorrelationCoefficients[key1, key2] = coeff; } temp_list.Clear(); count++; } } return(CorrelationCoefficients); }