17 using MathNet.Numerics.Statistics;
20 using System.Collections.Generic;
40 public static Series<DateTime, double> Beta(SortedList<DateTime, double> performancePoints, SortedList<DateTime, double> benchmarkPoints,
int windowSize = 132)
46 if (benchmarkPoints.Count != 0)
48 var benchmarkReturnsDictionary =
StatisticsBuilder.
CreateBenchmarkDifferences(benchmarkPoints.Select(x =>
new KeyValuePair<DateTime, decimal>(x.Key, (decimal)x.Value)), benchmarkPoints.Keys.First(), benchmarkPoints.Keys.Last());
56 var returns = Frame.CreateEmpty<DateTime,
string>();
57 returns[
"strategy"] = dailyReturnsSeries;
58 returns = returns.Join(
"benchmark", benchmarkReturns)
59 .FillMissing(Direction.Forward)
62 var correlation = returns
64 .SelectValues(x => Correlation.Pearson(x[
"strategy"].Values, x[
"benchmark"].Values));
66 var portfolioStandardDeviation = dailyReturnsSeries.Window(windowSize).SelectValues(s => s.StdDev());
67 var benchmarkStandardDeviation = benchmarkReturns.Window(windowSize).SelectValues(s => s.StdDev());
69 return (correlation * (portfolioStandardDeviation / benchmarkStandardDeviation))
70 .FillMissing(Direction.Forward)
83 var riskFreeRate = (double)_interestRateProvider.GetAverageRiskFreeRate(equityCurve.Keys);
84 if (equityCurve.IsEmpty)
89 var dailyReturns = equityCurve.ResampleEquivalence(date => date.Date, s => s.LastValue())
92 var rollingSharpeData =
new List<KeyValuePair<DateTime, double>>();
93 var firstDate = equityCurve.FirstKey();
95 foreach (var date
in equityCurve.Keys)
97 var nMonthsAgo = date.AddMonths(-months);
98 if (nMonthsAgo < firstDate)
103 var algoPerformanceLookback = dailyReturns.Between(nMonthsAgo, date);
104 rollingSharpeData.Add(
105 new KeyValuePair<DateTime, double>(
107 Statistics.Statistics.
SharpeRatio(algoPerformanceLookback.Values.ToList(), riskFreeRate, tradingDayPerYear)
112 return new Series<DateTime, double>(rollingSharpeData.Select(kvp => kvp.Key), rollingSharpeData.Select(kvp => kvp.Value));