CGC_Codes

stage 2 NN

Jun 7th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.43 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using Encog.Engine.Network.Activation;
  4. using Encog.MathUtil.RBF;
  5. using Encog.ML.Data;
  6. using Encog.ML.Data.Basic;
  7. using Encog.Neural.Networks;
  8. using Encog.Neural.Networks.Layers;
  9. using Encog.Neural.Networks.Training;
  10. using Encog.Neural.Networks.Training.Propagation.Resilient;
  11. using Encog.Neural.RBF;
  12. using Encog.Util.Arrayutil;
  13. using MathNet.Numerics.LinearAlgebra.Double;
  14. using QuantSys.Analytics;
  15. using QuantSys.Analytics.StatisticalModeling;
  16. using QuantSys.Util;
  17. using QuantSys.Visualization;
  18.  
  19. namespace QuantSys.MachineLearning.NeuralNetwork
  20. {
  21.     public class Stage2NeuralNetwork
  22.     {
  23.         private const double normalizeLo = -1;
  24.         private const double normalizeHi = 1;
  25.         private readonly int inputs;
  26.         private readonly int iterations;
  27.  
  28.         private readonly NormalizeArray[] normalizeArrayInput;
  29.         private readonly NormalizeArray normalizeArrayOutput;
  30.  
  31.         private readonly double[] predictionData;
  32.         private readonly DenseMatrix trainingData;
  33.  
  34.         public double[] OutputData;
  35.         private double[] _normalizedPredictionData;
  36.         private DenseMatrix _normalizedTrainingData;
  37.         public BasicNetwork network;
  38.  
  39.         public double outputCorre;
  40.         public double outputRMSE;
  41.  
  42.         public Stage2NeuralNetwork(int inputs, int iterations, DenseMatrix trainingData, double[] predictData)
  43.         {
  44.             this.inputs = inputs;
  45.             this.iterations = iterations;
  46.             this.trainingData = trainingData;
  47.             predictionData = predictData;
  48.             normalizeArrayInput = new NormalizeArray[trainingData.RowCount];
  49.             for (int i = 0; i < normalizeArrayInput.Length; i++)
  50.                 normalizeArrayInput[i] = new NormalizeArray { NormalizedHigh = normalizeHi, NormalizedLow = normalizeLo };
  51.             normalizeArrayOutput = new NormalizeArray { NormalizedHigh = normalizeHi, NormalizedLow = normalizeLo };
  52.         }
  53.  
  54.         public void Execute()
  55.         {
  56.             _normalizedTrainingData = NormalizeData(trainingData);
  57.             _normalizedPredictionData = NormalizeData(predictionData);
  58.             network = CreateNetwork();
  59.             IMLDataSet training = GenerateTraining();
  60.             Train(training);
  61.             Predict();
  62.         }
  63.  
  64.  
  65.         public DenseMatrix NormalizeData(DenseMatrix data)
  66.         {
  67.             var normalizedData = new DenseMatrix(data.RowCount, data.ColumnCount);
  68.  
  69.             for (int i = 0; i < data.RowCount; i++)
  70.             {
  71.                 normalizedData.SetRow(i, normalizeArrayInput[i].Process(data.Row(i).ToArray()));
  72.             }
  73.  
  74.             return normalizedData;
  75.         }
  76.  
  77.         public double[] NormalizeData(double[] data)
  78.         {
  79.             return normalizeArrayOutput.Process(data);
  80.         }
  81.  
  82.  
  83.         public BasicNetwork CreateNetwork()
  84.         {
  85.             var network = new BasicNetwork();
  86.             network.AddLayer(new BasicLayer(inputs));
  87.             network.AddLayer(new BasicLayer(new ActivationLinear(), true, inputs / 2 + 1));
  88.             network.AddLayer(new BasicLayer(1));
  89.             network.Structure.FinalizeStructure();
  90.             network.Reset();
  91.  
  92.            
  93.  
  94.             var rbfnetwork = new RBFNetwork(inputs, inputs / 2 + 1, 1, RBFEnum.Gaussian);
  95.  
  96.             return network;
  97.         }
  98.  
  99.         public IMLDataSet GenerateTraining()
  100.         {
  101.             var input = new double[trainingData.ColumnCount][];
  102.             var ideal = new double[_normalizedPredictionData.Length][];
  103.  
  104.             for (int i = 0; i < trainingData.ColumnCount; i++)
  105.             {
  106.                 input[i] = _normalizedTrainingData.Column(i).ToArray();
  107.                 double[] predict = { _normalizedPredictionData[i] };
  108.                 ideal[i] = predict;
  109.             }
  110.  
  111.             var result = new BasicMLDataSet(input, ideal);
  112.             return result;
  113.         }
  114.  
  115.  
  116.         public void Train(IMLDataSet training)
  117.         {
  118.             ITrain train = new ResilientPropagation(network, training);
  119.            
  120.  
  121.             int epoch = 1;
  122.  
  123.             do
  124.             {
  125.                 train.Iteration();
  126.                 if ((epoch) % (iterations / 10) == 0) Console.Write(".");
  127.                 epoch++;
  128.             } while (epoch < iterations * 100);
  129.         }
  130.  
  131.  
  132.         public void Predict()
  133.         {
  134.             double error = 0;
  135.             int c = 0;
  136.  
  137.             var d = new DenseMatrix(2, _normalizedPredictionData.Length);
  138.             int count = 0;
  139.             for (int i = 0; i < _normalizedPredictionData.Length; i++)
  140.             {
  141.                
  142.                 IMLData input = new BasicMLData(inputs);
  143.                 for (int j = 0; j < input.Count; j++)
  144.                 {
  145.                     input.Data[j] = _normalizedTrainingData[j, i];
  146.                 }
  147.  
  148.                 IMLData output = network.Compute(input);
  149.                 double prediction = output.Data[0];
  150.  
  151.  
  152.                 error +=
  153.                     Math.Pow(
  154.                         (normalizeArrayOutput.Stats.DeNormalize(prediction) - predictionData[i]) / predictionData[i], 2);
  155.                 c++;
  156.                 d[0, count] = predictionData[i];
  157.                 d[1, count] = normalizeArrayOutput.Stats.DeNormalize(prediction);
  158.                 count++;
  159.             }
  160.  
  161.             error /= c;
  162.             error = Math.Pow(error, .5);
  163.             Console.WriteLine(error);
  164.  
  165.             string[] symbols = { "actual", "predicted" };
  166.             Visualize.GeneratePredictionGraph(symbols, d, new DateTime(), new TimeSpan(24, 0, 0),
  167.                 "C:\\Sangar\\resultfinal.html");
  168.         }
  169.  
  170.  
  171.         public void Predict(DenseMatrix newPredictData, double[] newPredictPrices)
  172.         {
  173.             double error = 0;
  174.             int c = 0;
  175.  
  176.             var newNormalizedPredictData = new DenseMatrix(newPredictData.RowCount, newPredictData.ColumnCount,
  177.                 double.NaN);
  178.  
  179.             for (int i = 0; i < newPredictData.RowCount; i++)
  180.             {
  181.                 newNormalizedPredictData.SetRow(i, normalizeArrayInput[i].Process(newPredictData.Row(i).ToArray()));
  182.             }
  183.  
  184.             double[] normalizedPrices = normalizeArrayOutput.Process(newPredictPrices);
  185.  
  186.             var d = new DenseMatrix(2, normalizedPrices.Length + 1, double.NaN);
  187.             int count = 0;
  188.             for (int i = 0; i < normalizedPrices.Length; i++)
  189.             {
  190.                
  191.                 IMLData input = new BasicMLData(inputs);
  192.                 for (int j = 0; j < input.Count; j++)
  193.                 {
  194.                     input.Data[j] = newNormalizedPredictData[j, i];
  195.                 }
  196.  
  197.                 IMLData output = network.Compute(input);
  198.                 double prediction = output.Data[0];
  199.  
  200.  
  201.                 error +=
  202.                     Math.Pow(
  203.                         (normalizeArrayOutput.Stats.DeNormalize(prediction) - newPredictPrices[i]) / newPredictPrices[i],
  204.                         2);
  205.                 c++;
  206.                 d[0, count] = newPredictPrices[i];
  207.                 d[1, count] = normalizeArrayOutput.Stats.DeNormalize(prediction);
  208.                 count++;
  209.             }
  210.  
  211.            
  212.  
  213.             IMLData input1 = new BasicMLData(inputs);
  214.             for (int j = 0; j < input1.Count; j++)
  215.             {
  216.                 input1.Data[j] = newNormalizedPredictData[j, newNormalizedPredictData.ColumnCount - 1];
  217.             }
  218.  
  219.             IMLData output1 = network.Compute(input1);
  220.             d[1, count] = normalizeArrayOutput.Stats.DeNormalize(output1.Data[0]);
  221.  
  222.  
  223.            
  224.  
  225.  
  226.             error /= c;
  227.             error = Math.Pow(error, .5);
  228.             Console.WriteLine(error);
  229.  
  230.             string[] symbols = { "actual", "predicted" };
  231.             Visualize.GeneratePredictionGraph(symbols, d, new DateTime(), new TimeSpan(24, 0, 0),
  232.                 "C:\\Sangar\\resultfinal.html");
  233.  
  234.             outputCorre =
  235.                 StatisticsExtension.Correlation(d.Row(0).ToArray().Take(d.ColumnCount - 1).ToArray().RawRateOfReturn(),
  236.                     d.Row(1).ToArray().Take(d.ColumnCount - 1).ToArray().RawRateOfReturn());
  237.  
  238.             Console.WriteLine("ST2 Correlation: " + outputCorre);
  239.  
  240.             outputRMSE = error;
  241.  
  242.             Console.WriteLine("Predicted return for D+1:" +
  243.                               (d[1, d.ColumnCount - 1] - d[1, d.ColumnCount - 2]) / d[1, d.ColumnCount - 2] * 100 +
  244.                               " percent");
  245.         }
  246.     }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment