/// <summary> /// Event fired each time the we add/remove securities from the data feed /// </summary> /// <param name="algorithm">The algorithm instance that experienced the change in securities</param> /// <param name="changes">The security additions and removals from the algorithm</param> public override void OnSecuritiesChanged(QCAlgorithm algorithm, SecurityChanges changes) { NotifiedSecurityChanges.UpdateCollection(Securities, changes); var symbols = Securities.Select(x => x.Symbol).ToArray(); var history = algorithm.History(symbols, _lookback, _resolution); var vectors = GetPriceVectors(history); if (vectors.LongLength == 0) { algorithm.Debug($"PearsonCorrelationPairsTradingAlphaModel.OnSecuritiesChanged(): The requested historical data does not have series of prices with the same date/time. Please consider increasing the looback period. Current lookback: {_lookback}"); } else { var pearsonMatrix = Correlation.PearsonMatrix(vectors).UpperTriangle(); var maxValue = pearsonMatrix.Enumerate().Where(x => Math.Abs(x) < 1).Max(); if (maxValue >= _minimumCorrelation) { var maxTuple = pearsonMatrix.Find(x => x == maxValue); _bestPair = Tuple.Create(symbols[maxTuple.Item1], symbols[maxTuple.Item2]); } } base.OnSecuritiesChanged(algorithm, changes); }
/// <summary> /// Builds a correlation matrix for an array of columns /// </summary> /// <param name="columns">Array of columns for correlation</param> /// <param name="algorithm">Algorithm enumeration of Pearson and Spearman (default)</param> /// <returns>Returns the correlation matrix</returns> public static Matrix <double> CorrelationMatrix(this double[][] columns, Algorithm algorithm = Algorithm.Spearman) { switch (algorithm) { case Algorithm.Pearson: return(Correlation.PearsonMatrix(columns)); case Algorithm.Spearman: return(Correlation.SpearmanMatrix(columns)); default: return(Correlation.SpearmanMatrix(columns)); } }
private void clusterCovariance_Click(object sender, EventArgs e) { debugData.Columns.Clear(); double[][] v = Correlation.PearsonMatrix(analyzer.GetClusterOfInput((int)ClusterBox.SelectedItem).ToArray()).ToRowArrays(); foreach (double item in v[0]) { debugData.Columns.Add("", ""); } // debugData.Columns[0].Width = 0; debugData.Rows.Add(v.Count()); for (int i = 0; i < v.Count(); i++) { double[] row = v[i]; for (int j = 0; j < row.Count(); j++) { debugData.Rows[i].Cells[j].Value = v[i][j]; // debugData.Text += item.Select(x => new String(x.ToString().Take(4).ToArray())).Aggregate((a, b) => a + " | " + b); //debugData.Text += '\n'; } } }
/// <summary> /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized. /// </summary> public override void Initialize() { SetStartDate(2013, 10, 07); //Set Start Date SetEndDate(2013, 10, 11); //Set End Date SetCash(100000); //Set Strategy Cash // Find more symbols here: http://quantconnect.com/data AddEquity("SPY", Resolution.Daily); AddEquity("AIG", Resolution.Daily); AddEquity("BAC", Resolution.Daily); AddEquity("IBM", Resolution.Daily); var allHistoryBars = new List <double[]>(); SymbolDataList = new List <SymbolData>(); foreach (var security in Securities) { var history = History(security.Key, TimeSpan.FromDays(365)); allHistoryBars.Add(history.Select(x => (double)x.Value).ToArray()); SymbolDataList.Add(new SymbolData(security.Key, history)); } // Diagonal Matrix with each security risk (standard deviation) var S = Matrix <double> .Build.DenseOfDiagonalArray(SymbolDataList.Select(x => (double)x.Risk).ToArray()); // Computes Correlation Matrix (using Math.NET Numerics Statistics) var R = Correlation.PearsonMatrix(allHistoryBars); // Computes Covariance Matrix (using Math.NET Numerics Linear Algebra) Sigma = S * R * S; ComputeLagrangeMultiplier(); ComputeWeights(); ComputePortfolioRisk(); Log($"Lagrange Multiplier: {_lagrangeMultiplier.ToStringInvariant("7:F4")}"); Log($"Portfolio Risk: {_portfolioRisk.ToStringInvariant("7:P2")} "); }
private void runNeighbourhoodModel(double[][] utilityMatrix, Dictionary <int, int> userIndexMap) { var correlation = Correlation.PearsonMatrix(utilityMatrix); int k = (int)Math.Floor(Math.Sqrt(userIndexMap.Count - 1)); for (var i = 0; i < userIndexMap.Count; i++) { var current_user = correlation.Column(i); List <int> neighbours = new List <int>(); List <double> similarity = new List <double>(); //set the cross corrs = -1 so they are not considered neighbours current_user[i] = -1; //add the top k neighbours to the neibourhood table for (var n = 0; n < k; n++) { int top = current_user.MaximumIndex(); similarity.Add(current_user[top]); neighbours.Add(userIndexMap[top]); current_user[top] = -1; } unc.AddUserNeighbourhood(userIndexMap[i], neighbours, similarity); } }