// BETA COEFFICIENT public static IEnumerable <BetaResult> GetBeta( IEnumerable <Quote> historyMarket, IEnumerable <Quote> historyEval, int lookbackPeriod) { // clean quotes historyMarket = Cleaners.PrepareHistory(historyMarket); historyEval = Cleaners.PrepareHistory(historyEval); // initialize results List <BetaResult> results = new List <BetaResult>(); // get prerequisite data IEnumerable <CorrResult> correlation = GetCorrelation(historyMarket, historyEval, lookbackPeriod); // roll through history for interim data foreach (Quote e in historyEval) { BetaResult result = new BetaResult { Index = (int)e.Index, Date = e.Date }; // calculate beta, if available CorrResult c = correlation.Where(x => x.Date == e.Date).FirstOrDefault(); if (c.Covariance != null && c.VarianceA != null && c.VarianceA != 0) { result.Beta = c.Covariance / c.VarianceA; } results.Add(result); } return(results); }
// BETA COEFFICIENT public static IEnumerable <BetaResult> GetBeta( IEnumerable <Quote> historyMarket, IEnumerable <Quote> historyEval, int lookbackPeriod) { // clean quotes historyMarket = Cleaners.PrepareHistory(historyMarket); historyEval = Cleaners.PrepareHistory(historyEval); // check exceptions int qtyHistory = historyEval.Count(); int minHistory = lookbackPeriod; if (qtyHistory < minHistory) { throw new BadHistoryException("Insufficient history provided for Beta. " + string.Format("You provided {0} periods of history when {1} is required." , qtyHistory, minHistory)); } // initialize results List <BetaResult> results = new List <BetaResult>(); // get prerequisite data IEnumerable <CorrResult> correlation = GetCorrelation(historyMarket, historyEval, lookbackPeriod); // roll through history for interim data foreach (Quote e in historyEval) { BetaResult result = new BetaResult { Index = (int)e.Index, Date = e.Date }; // calculate beta, if available CorrResult c = correlation.Where(x => x.Date == e.Date).FirstOrDefault(); if (c.Covariance != null && c.VarianceA != null && c.VarianceA != 0) { result.Beta = c.Covariance / c.VarianceA; } results.Add(result); } return(results); }
// BETA COEFFICIENT public static IEnumerable <BetaResult> GetBeta <TQuote>( IEnumerable <TQuote> historyMarket, IEnumerable <TQuote> historyEval, int lookbackPeriod) where TQuote : IQuote { // clean quotes List <TQuote> historyEvalList = historyEval.Sort(); // validate parameters ValidateBeta(historyMarket, historyEval, lookbackPeriod); // initialize results List <BetaResult> results = new List <BetaResult>(); // get prerequisite data List <CorrResult> correlation = GetCorrelation(historyMarket, historyEval, lookbackPeriod).ToList(); // roll through history for interim data for (int i = 0; i < historyEvalList.Count; i++) { TQuote e = historyEvalList[i]; BetaResult result = new BetaResult { Date = e.Date }; // calculate beta, if available CorrResult c = correlation[i]; if (c.Covariance != null && c.VarianceA != null && c.VarianceA != 0) { result.Beta = c.Covariance / c.VarianceA; } results.Add(result); } return(results); }