Esempio n. 1
0
        public void FinishAndProcess()
        {
            try
            {
                var priceData = new DenseMatrix(symbols.Length, numTicks);

                for (int j = 0; j < symbols.Length; j++)
                {
                    SortedList <DateTime, Tick> d = mktData[j].data.Data;
                    for (int k = 0; k < d.Count; k++)
                    {
                        priceData[j, k] = d.Values[k].BidClose;
                    }
                }

                for (int i = 0; i < symbols.Length; i++)
                {
                    for (int j = 0; j < symbols.Length; j++)
                    {
                        double[] pDatai = priceData.Row(i).ToArray();
                        double[] pDataj = priceData.Row(j).ToArray();

                        switch (cType)
                        {
                        case CovarianceType.LogReturn:
                        {
                            pDatai = priceData.Row(i).ToArray().LogRateOfReturn();
                            pDataj = priceData.Row(j).ToArray().LogRateOfReturn();
                            break;
                        }

                        case CovarianceType.RawReturn:
                        {
                            pDatai = priceData.Row(i).ToArray().RawRateOfReturn();
                            pDataj = priceData.Row(j).ToArray().RawRateOfReturn();
                            break;
                        }
                        }
                        correlation[i, j] = StatisticsExtension.Correlation(pDatai, pDataj);
                        covariance[i, j]  = StatisticsExtension.Covariance((DenseVector)priceData.Row(i),
                                                                           (DenseVector)priceData.Row(j));
                    }
                }

                Visualize.GenerateHeatMatrix(symbols, correlation, "C:\\Users\\Ethan\\Work\\QuantSysdata.html");
                Console.WriteLine("Finished Generating Correlation Matrix.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Esempio n. 2
0
        public void Predict(DenseMatrix newPredictData, double[] newPredictPrices)
        {
            double error = 0;
            int    c     = 0;

            var newNormalizedPredictData = new DenseMatrix(newPredictData.RowCount, newPredictData.ColumnCount,
                                                           double.NaN);

            for (int i = 0; i < newPredictData.RowCount; i++)
            {
                newNormalizedPredictData.SetRow(i, normalizeArrayInput[i].Process(newPredictData.Row(i).ToArray()));
            }

            double[] normalizedPrices = normalizeArrayOutput.Process(newPredictPrices);

            var d     = new DenseMatrix(2, normalizedPrices.Length + 1, double.NaN);
            int count = 0;

            for (int i = 0; i < normalizedPrices.Length; i++)
            {
                // calculate based on actual data
                IMLData input = new BasicMLData(inputs);
                for (int j = 0; j < input.Count; j++)
                {
                    input.Data[j] = newNormalizedPredictData[j, i];
                }

                IMLData output     = network.Compute(input);
                double  prediction = output.Data[0];


                error +=
                    Math.Pow(
                        (normalizeArrayOutput.Stats.DeNormalize(prediction) - newPredictPrices[i]) / newPredictPrices[i],
                        2);
                c++;
                d[0, count] = newPredictPrices[i];
                d[1, count] = normalizeArrayOutput.Stats.DeNormalize(prediction);
                count++;
            }

            /////////////////////////////////////////////////////////////////

            IMLData input1 = new BasicMLData(inputs);

            for (int j = 0; j < input1.Count; j++)
            {
                input1.Data[j] = newNormalizedPredictData[j, newNormalizedPredictData.ColumnCount - 1];
            }

            IMLData output1 = network.Compute(input1);

            d[1, count] = normalizeArrayOutput.Stats.DeNormalize(output1.Data[0]);


            /////////////////////////////////////////////////////////////////


            error /= c;
            error  = Math.Pow(error, .5);
            Console.WriteLine(error);

            string[] symbols = { "actual", "predicted" };
            Visualize.GeneratePredictionGraph(symbols, d, new DateTime(), new TimeSpan(24, 0, 0),
                                              "C:\\Sangar\\resultfinal.html");

            outputCorre =
                StatisticsExtension.Correlation(d.Row(0).ToArray().Take(d.ColumnCount - 1).ToArray().RawRateOfReturn(),
                                                d.Row(1).ToArray().Take(d.ColumnCount - 1).ToArray().RawRateOfReturn());

            Console.WriteLine("ST2 Correlation: " + outputCorre);

            outputRMSE = error;

            Console.WriteLine("Predicted return for D+1:" +
                              (d[1, d.ColumnCount - 1] - d[1, d.ColumnCount - 2]) / d[1, d.ColumnCount - 2] * 100 +
                              " percent");
        }
Esempio n. 3
0
        public void Predict(double[] newPredictData)
        {
            double error = 0;
            int    c     = 0;

            double[] newNormalizedData = normalizeArray.Process(newPredictData);

            var d     = new DenseMatrix(2, newNormalizedData.Length - WindowSize + 1, double.NaN);
            int count = 0;

            for (int i = WindowSize; i < newNormalizedData.Length; i++)
            {
                // calculate based on actual data
                IMLData input = new BasicMLData(WindowSize);
                for (int j = 0; j < input.Count; j++)
                {
                    input.Data[j] = newNormalizedData[(i - WindowSize) + j];
                }

                IMLData output     = network.Compute(input);
                double  prediction = output.Data[0];


                error += Math.Pow((normalizeArray.Stats.DeNormalize(prediction) - newPredictData[i]) / newPredictData[i],
                                  2);
                c++;
                d[0, count] = newPredictData[i];
                d[1, count] = normalizeArray.Stats.DeNormalize(prediction);
                count++;
            }

            ///////////////////////////////////////////////////////////////////////////////

            var lastData = new double[WindowSize];
            int count1   = 0;

            for (int i = newNormalizedData.Length - WindowSize; i < newNormalizedData.Length; i++)
            {
                lastData[count1++] = newNormalizedData[i];
            }
            IMLData input1 = new BasicMLData(WindowSize);

            for (int j = 0; j < input1.Count; j++)
            {
                input1.Data[j] = lastData[j];
            }


            IMLData output1 = network.Compute(input1);

            d[1, count] = normalizeArray.Stats.DeNormalize(output1.Data[0]);


            /////////////////////////////////////////////////////////////////////////////////


            error /= c;
            error  = Math.Pow(error, .5);
            Console.WriteLine(error);

            OutputData = d.Row(1).ToArray();

            string[] symbols = { "actual", "predicted" };
            Visualize.GeneratePredictionGraph(symbols, d, new DateTime(), new TimeSpan(24, 0, 0),
                                              QSConstants.DEFAULT_DATA_FILEPATH + write + ".html");

            Console.WriteLine("ST1 Correlation: " +
                              StatisticsExtension.Correlation(
                                  d.Row(0).ToArray().Take(d.ColumnCount - 1).ToArray().RawRateOfReturn(),
                                  d.Row(1).ToArray().Take(d.ColumnCount - 1).ToArray().RawRateOfReturn()));
        }