public MainWindow()
        {
            InitializeComponent();

            m_Data = new Sp500History();
            m_ShareNum = 200;

            AddFunctors();
            AddSchemes();
            UpdateImage();
        }
        public override double GetSchemeProfit(StockHistorySet testData, AbstractPredictor Predictor, int today, int daysToPredict)
        {
            double profit = 0D;

            foreach (StockHistory stockHistory in testData.AllStockHistories)
            {
                double startV = stockHistory.Closes[today];
                double actualEndV = stockHistory.Closes[today + daysToPredict];

                profit += (actualEndV - startV);
            }

            return profit / testData.AllStockHistories.Count;
        }
        public override double GetSchemeProfit(StockHistorySet testData, AbstractPredictor Predictor, int today, int daysToPredict)
        {
            var stocksInOrder = testData.AllStockHistories.OrderByDescending(x => Predictor.PredictValue(x, today, daysToPredict) / x.Closes[today]).ToArray();
            var tenBest = stocksInOrder.Take(10).ToArray();

            double profit = 0D;

            foreach (StockHistory stockHistory in tenBest)
            {
                double startV = stockHistory.Closes[today];
                double actualEndV = stockHistory.Closes[today + daysToPredict];

                profit += (actualEndV - startV);
            }

            return profit / 10;
        }
        public override double GetSchemeProfit(StockHistorySet testData, AbstractPredictor Predictor, int today, int daysToPredict)
        {
            double profit = 0D;

            // If prediction is higher, buy 1 unit. Otherwise, short 1 unit
            foreach (StockHistory stockHistory in testData.AllStockHistories)
            {
                double startV = stockHistory.Closes[today];
                double predictedEndV = Predictor.PredictValue(stockHistory, today, daysToPredict);
                double actualEndV = stockHistory.Closes[today + daysToPredict];

                if (Predictor is RandomPredictor)
                {
                    ; ;
                }

                if (predictedEndV > startV)
                    profit += (actualEndV - startV);
                else
                    profit += (startV - actualEndV);
            }

            return profit / testData.AllStockHistories.Count;
        }
        public void Setup(IEnumerable<IFunctor> functors, StockHistorySet stockHistorySet)
        {
            Fns = functors.ToArray();
            m_StockHistorySet = stockHistorySet;

            if (this.Fns == null) throw new ApplicationException();
            if (!Fns.Any()) throw new ApplicationException();

            int numInput = Fns.Length;
            int numHidden = 8;
            int numOutput = 1;
            int numWeights = (numInput * numHidden) + (numHidden * numOutput) + (numHidden + numOutput);

            DebugWrite("Creating a " + numInput + "-input, " + numHidden + "-hidden, " + numOutput + "-output neural network");
            DebugWrite("Using hard-coded tanh function for hidden layer activation");
            DebugWrite("Using hard-coded log-sigmoid function for output layer activation");

            m_Bnn = new BackPropNeuralNet(numInput, numHidden, numOutput);

            DebugWrite("\nGenerating random initial weights and bias values");
            double[] initWeights = new double[numWeights];
            for (int i = 0; i < initWeights.Length; ++i)
                initWeights[i] = (rnd.NextDouble() - 0.5d) * 1.0d;
            DebugWrite("Loading neural network initial weights and biases into neural network");
            m_Bnn.SetWeights(initWeights);

            double learnRate = 0.2;  // learning rate - controls the maginitude of the increase in the change in weights.
            double momentum = 0.1; // momentum - to discourage oscillation.
            DebugWrite("Setting learning rate = " + learnRate.ToString("F2") + " and momentum = " + momentum.ToString("F2"));

            int maxEpochs = 8000000;
            double errorThresh = 0.01;
            DebugWrite("\nSetting max epochs = " + maxEpochs + " and error threshold = " + errorThresh.ToString("F6"));

            // Train

            int epoch = 0;
            double error = double.MaxValue;
            DebugWrite("\nBeginning training using back-propagation\n");

            int stocksCount = stockHistorySet.AllStockHistories.Count(); ;

            while (epoch < maxEpochs) // train
            {
                int stockNum = rnd.Next(0, stocksCount);
                var stock = stockHistorySet.AllStockHistories[stockNum];

                double realValue = stock.Closes[Today + DaysInFuture] / stock.Closes[Today];
                double updateValue = Math.Tanh(realValue);

                double predictedOutput = m_Bnn.ComputeOutputs(ComputeInputs(stock))[0];
                double predictedValue = ATanh(predictedOutput) * stock.Closes[Today];

                m_Bnn.UpdateWeights(new[] { updateValue }, learnRate, momentum);
                ++epoch;

                if (epoch % 20000 == 0)
                {
                    error = GetAverageError(true);

                    if (error < errorThresh)
                    {
                        DebugWrite("Found weights and bias values that meet the error criterion at epoch " + epoch);
                        break;
                    }
                    DebugWrite("epoch = " + epoch);
                    DebugWrite(" error = " + error + "\n");
                }
            } // train loop

            double[] finalWeights = m_Bnn.GetWeights();
            DebugWrite("");
            DebugWrite("Final neural network weights and bias values are:");
            Helpers.ShowVector(finalWeights, 5, 8, true);
        }
 /// <summary>
 /// How good the Predictor is with this purchasing scheme. Specifically, if you invested $1 in it, how much would you get back.
 /// </summary>
 /// <returns>The profit of this scheme</returns>
 public abstract double GetSchemeProfit(StockHistorySet testData, AbstractPredictor Predictor, int today, int daysToPredict);