CGC_Codes

NN two stage

Jun 7th, 2017
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.63 KB | None | 0 0
  1. using System.Linq;
  2. using MathNet.Numerics.LinearAlgebra.Double;
  3. using QuantSys.Analytics;
  4. using QuantSys.Analytics.StatisticalModeling;
  5.  
  6. namespace QuantSys.MachineLearning.NeuralNetwork
  7. {
  8.     public class TwoStageNN
  9.     {
  10.         private readonly int cycles;
  11.         private readonly object[,] data;
  12.         private readonly int[] vectors;
  13.         private readonly int window;
  14.  
  15.         public double RMSE;
  16.         public double correlation;
  17.  
  18.         public TwoStageNN(int window, int cycles, object[,] data, int[] vectors)
  19.         {
  20.             this.window = window;
  21.             this.cycles = cycles;
  22.             this.data = data;
  23.             this.vectors = vectors;
  24.         }
  25.  
  26.         public void Execute()
  27.         {
  28.             var nnSet = new Stage1NeuralNetwork[vectors.Length];
  29.  
  30.             int trainLength = 2000;
  31.             int validationLength = 500;
  32.             int predictLength = 500;
  33.             int useLength = 3000;
  34.  
  35.             var totalData = new DenseMatrix(vectors.Length, useLength);
  36.             var outputData = new DenseMatrix(vectors.Length, validationLength - window);
  37.  
  38.            
  39.             var pricingData = new double[useLength];
  40.  
  41.             for (int i = 2; i < 2 + useLength; i++)
  42.             {
  43.                 pricingData[i - 2] = (double)data[useLength + 3 - i, 5];
  44.             }
  45.  
  46.  
  47.             double[] returnpricingData = pricingData.RawRateOfReturn();
  48.             for (int i = 0; i < returnpricingData.Length; i++) pricingData[i + 1] = returnpricingData[i];
  49.             pricingData[0] = 0;
  50.  
  51.  
  52.            
  53.             for (int i = 2; i < 2 + useLength; i++)
  54.             {
  55.                 for (int j = 0; j < vectors.Length; j++)
  56.                 {
  57.                     totalData[j, i - 2] = (double)data[useLength + 3 - i, vectors[j]];
  58.                 }
  59.             }
  60.  
  61.  
  62.             for (int j = 0; j < vectors.Length; j++)
  63.             {
  64.                 double[] train = totalData.Row(j).ToArray().Take(trainLength).ToArray();
  65.                 double[] validate =
  66.                     totalData.Row(j).ToArray().Skip(trainLength).ToArray().Take(validationLength).ToArray();
  67.                 nnSet[j] = new Stage1NeuralNetwork(window, cycles, train, validate);
  68.                 nnSet[j].Execute(j);
  69.                 outputData.SetRow(j, nnSet[j].OutputData);
  70.             }
  71.  
  72.             var s1 = new Stage2NeuralNetwork(vectors.Length, cycles, outputData,
  73.                 pricingData.Skip(trainLength).ToArray().Take(validationLength).ToArray().Skip(window).ToArray());
  74.             s1.Execute();
  75.  
  76.             var predictedData = new DenseMatrix(vectors.Length, predictLength - window + 1);
  77.  
  78.             var lastPredData = new double[vectors.Length];
  79.  
  80.             for (int j = 0; j < vectors.Length; j++)
  81.             {
  82.                 double[] predictData =
  83.                     totalData.Row(j)
  84.                         .ToArray()
  85.                         .Skip(trainLength + validationLength)
  86.                         .ToArray()
  87.                         .Take(predictLength)
  88.                         .ToArray();
  89.                 nnSet[j].Predict(predictData);
  90.                 predictedData.SetRow(j, nnSet[j].OutputData);
  91.                 lastPredData[j] = nnSet[j].NextPrediction;
  92.             }
  93.  
  94.             s1.Predict(predictedData,
  95.                 pricingData.ToArray()
  96.                     .Skip(trainLength + validationLength)
  97.                     .ToArray()
  98.                     .Take(predictLength)
  99.                     .ToArray()
  100.                     .Skip(window)
  101.                     .ToArray());
  102.  
  103.             correlation = s1.outputCorre;
  104.             RMSE = s1.outputRMSE;
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment