Ejemplo n.º 1
0
        public static double[,] ToCorrelation(this CovarianceMatrix covariancematrix)
        {
            var normalized = new double[covariancematrix.RowCount, covariancematrix.ColumnCount];

            for (int r = 0; r < covariancematrix.RowCount; r++)
            {
                for (int c = 0; c < covariancematrix.ColumnCount; c++)
                {
                    normalized[r, c] = covariancematrix[r, c] / (System.Math.Sqrt(covariancematrix[r, r]) * System.Math.Sqrt(covariancematrix[c, c]));
                }
            }
            return(normalized);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Create a covariance matrix from a delimited file
        /// </summary>
        /// <param name="filename">The name of the file from which to read</param>
        /// <param name="delimiter">The delimiter to use - default ','</param>
        /// <returns></returns>
        public static CovarianceMatrix Create(string filename, char delimiter = ',')
        {
            CsvFileDescription inputFileDescription = new CsvFileDescription
            {
                SeparatorChar           = delimiter, // comma delimited
                FirstLineHasColumnNames = false      // column names in first record
            };

            CsvContext cc  = new CsvContext();
            var        cov = cc.Read <CsvDataRow>(filename, inputFileDescription);

            var heading = from line in cov
                          from item in line
                          where item.LineNbr == 1
                          select item.Value;

            var tickers = from t in heading.Skip(1)
                          select t;

            var covdict = new Dictionary <string, Dictionary <string, double> >();

            foreach (var row in cov.Skip(1))
            {
                var ticker = row.First().Value;
                var values = row.Skip(1);

                int count    = 0;
                var tempdict = new Dictionary <string, double>();
                foreach (var v in values)
                {
                    tempdict.Add(tickers.ToArray()[count], Convert.ToDouble(v.Value));
                    count++;
                }

                covdict.Add(ticker, tempdict);
            }

            return(CovarianceMatrix.Create(covdict));
        }