private void GetCorrelation() { try { if (navByDate == null) { return; } navByDate = navByDate.OrderBy(x => x.Key.Year).ThenBy(x => x.Key.Month).ThenBy(x => x.Key.Day). ToDictionary(x => x.Key, x => x.Value); var sensexReturn = Sensex.GetCloseByDate(); List <Tuple <double, double> > pairForCorrelation = new List <Tuple <double, double> >(); StringBuilder sb = new StringBuilder(); foreach (var date in navByDate.Keys) { bool found = false; var preDate = this.FindPreviousDate(date, sensexReturn.Keys.ToList(), out found); if (!found) { continue; } if (!sensexReturn.ContainsKey(preDate)) { continue; } pairForCorrelation.Add(Tuple.Create(navByDate[date], sensexReturn[preDate])); sb.AppendFormat("{0}\t{1}\t{2}\n", date, navByDate[date], sensexReturn[preDate]); } //this.Correlation = Sensex.Correlation(pairForCorrelation.Select(p => p.Item1).ToArray(), pairForCorrelation.Select(p => p.Item2).ToArray()); } catch { } }
private static double GetCorrelation(Dictionary <DateTime, double> r1, Dictionary <DateTime, double> r2) { List <double> retList1 = new List <double>(); List <double> retList2 = new List <double>(); bool updated = false; foreach (var date in r1.Keys) { if (!r2.ContainsKey(date)) { continue; } retList1.Add(r1[date]); retList2.Add(r2[date]); if (retList1.Count != retList2.Count) { } updated = true; } if (!updated) { return(0); } return(Sensex.Correlation(retList1.ToArray(), retList2.ToArray())); }
private double SensexReturn(out double standardDeviation) { try { List <DateTime> dates = new List <DateTime>(investmnetByDate.Keys); var returns = Sensex.GetReturnByDate(dates, true); int days = (Sensex.lastRecord.Item1 - Sensex.firstRecord.Item1).Days; double ret = Sensex.lastRecord.Item2 / Sensex.firstRecord.Item2; //ret = (Math.Pow(ret, (double) (1.0 / days)) - 1)*(365.0/days); ret = Math.Pow(ret, 365.0 / days) - 1; standardDeviation = this.GetStandardDeviation(returns.Values.Select(r => r).ToList()); //standardDeviation /= 100; return(ret * 100); } catch { Console.WriteLine("Error in fetching/reading sensex data!"); } finally { standardDeviation = 0; } return(0); }