public void getMinMax_NAN_Test()
        {
            double[] inputs = new double[200];
            int      v      = 0;

            for (int i = 0; i < inputs.Length; i += 2)
            {
                inputs[i] = v++;
            }

            for (int i = 1; i < inputs.Length; i += 2)
            {
                inputs[i] = double.NaN;
            }

            Assert.AreEqual(inputs[0], 0);
            Assert.AreEqual(inputs[2], 1);
            Assert.AreEqual(inputs[4], 2);
            Assert.AreEqual(inputs[6], 3);

            Assert.AreEqual(inputs[1], double.NaN);
            Assert.AreEqual(inputs[3], double.NaN);
            Assert.AreEqual(inputs[5], double.NaN);
            Assert.AreEqual(inputs[7], double.NaN);

            double min, max;

            DistributionHelper.getMinMax(inputs, 10, out min, out max);

            Assert.AreEqual(5, min, 1);
            Assert.AreEqual(95, max, 1);
        }
        public static Image visualizeArray(double[] input, int width, int height, int lineSize = 3)
        {
            Bitmap   bmp = new Bitmap(width, height);
            Graphics g   = Graphics.FromImage(bmp);

            g.Clear(Color.LightGray);

            double stepSize = Convert.ToDouble(input.Length) / Convert.ToDouble(width);

            double min, max;

            DistributionHelper.getMinMax(input, out min, out max);

            int oldX = -1, oldY = -1;

            Color cUp   = Color.Green;
            Color cDown = Color.Blue;

            for (int x = 0; x < width; x++)
            {
                int index = Convert.ToInt32(stepSize * x);
                if (index < input.Length && double.IsNaN(input[index]) == false)
                {
                    int y = height - Convert.ToInt32((input[index] - min) / (max - min) * height);

                    if (oldX != -1)
                    {
                        for (int yOffset = -(lineSize / 2); yOffset < (lineSize / 2); yOffset++)
                        {
                            if (y + yOffset > 0 && y + yOffset < height)
                            {
                                bmp.SetPixel(x, y + yOffset, y > oldY ? cDown : cUp);
                            }
                        }
                    }

                    oldX = x;
                    oldY = y;
                }
                else
                {
                    for (int y = 0; y < height; y++)
                    {
                        bmp.SetPixel(x, y, Color.Yellow);
                    }
                }
            }

            return(bmp);
        }
        public void getMinMax_Test_SmallValues()
        {
            double[] inputs = new double[100];
            for (int i = 0; i < inputs.Length; i++)
            {
                inputs[i] = i * 0.001;
            }

            double min, max;

            DistributionHelper.getMinMax(inputs, 10, out min, out max);

            Assert.AreEqual(5 * 0.001, min, 1 * 0.001);
            Assert.AreEqual(95 * 0.001, max, 1 * 0.001);
        }
        public void getMinMax_Test_MoreComplex()
        {
            double[] inputs = new double[200];
            for (int i = 0; i < 100; i++)
            {
                inputs[i] = i;
            }

            for (int i = 100; i < 200; i++)
            {
                inputs[i] = 100;
            }

            double min, max;

            DistributionHelper.getMinMax(inputs, 10, out min, out max);
            Assert.AreEqual(10, min, 1);
            Assert.AreEqual(100, max, 1);
        }
        public LearningIndicator(WalkerIndicator indicator, double[][] prices, bool[][] outcomeCodes, double[][] outcomes, long timeframe, double targetPercent, double minPercentThreshold, int steps, bool createStatistics)
        {
            this.targetPercent = targetPercent;
            this.timeframe     = timeframe;

            double validRatio;

            double[] values = IndicatorRunner.getIndicatorValues(prices, indicator.Clone(), out validRatio);
            if (validRatio < 0.5)
            {
                throw new TooLittleValidDataException("Not enough valid values: " + validRatio);
            }

            //May be does not work properly... todo:
            double min, max, usedValuesRatio;

            //DistributionHelper.getMinMax(values, 4, out min, out max);
            DistributionHelper.getMinMax(values, out min, out max);

            outcomeCodeSamplingTable = IndicatorSampler.sampleValuesOutcomeCode(values, outcomeCodes, min, max, steps, out usedValuesRatio);
            if (usedValuesRatio < 0.5)
            {
                throw new TooLittleValidDataException("Not enough sampling for outcomeCode: " + usedValuesRatio);
            }

            outcomeSamplingTable = IndicatorSampler.sampleValuesOutcome(values, prices, outcomes, min, max, out usedValuesRatio, 40);
            if (usedValuesRatio < 0.5)
            {
                throw new TooLittleValidDataException("Not enough sampling for outcome: " + usedValuesRatio);
            }

            this.usedValues = usedValuesRatio;

            if (createStatistics)
            {
                //Predictive power calculation
                predictivePower = new double[33];
                IndicatorSampler.getStatisticsOutcomeCodes(values, outcomeCodes, out predictivePower[0], out predictivePower[1], out predictivePower[2], out predictivePower[3]);
                IndicatorSampler.getStatisticsOutcomes(values, prices, outcomes, out predictivePower[4], out predictivePower[5], out predictivePower[6], out predictivePower[7], out predictivePower[8], out predictivePower[9]);

                DistributionHelper.getSampleOutcomeCodesBuyMaxSellMax(outcomeCodeSamplingTable, minPercentThreshold, out predictivePower[10], out predictivePower[11], out predictivePower[12], out predictivePower[13]);
                DistributionHelper.getSampleOutcomesMinMax(outcomeSamplingTable, minPercentThreshold, out predictivePower[14], out predictivePower[15], out predictivePower[16], out predictivePower[17], out predictivePower[18], out predictivePower[19], out predictivePower[20], out predictivePower[21], out predictivePower[22], out predictivePower[23]);

                //Outcome Code

                List <double> buyCodesDist        = new List <double>(),
                              sellCodesDist       = new List <double>(),
                              buySellDistanceDist = new List <double>(),
                              minMaxDistanceDist  = new List <double>(),
                              minDist             = new List <double>(),
                              maxDist             = new List <double>(),
                              actualDist          = new List <double>();

                double totalCodeSamples = 0;
                foreach (double[] row in outcomeCodeSamplingTable)
                {
                    totalCodeSamples += row[(int)SampleValuesOutcomeCodesIndices.SamplesCount];
                }

                int regardedStates = 0;
                foreach (double[] row in outcomeCodeSamplingTable)
                {
                    if ((row[(int)SampleValuesOutcomeCodesIndices.SamplesCount] / totalCodeSamples) * 100 >= minPercentThreshold) //minPercentThreshold
                    {
                        buyCodesDist.Add(row[(int)SampleValuesOutcomeCodesIndices.BuyRatio]);
                        sellCodesDist.Add(row[(int)SampleValuesOutcomeCodesIndices.SellRatio]);
                        buySellDistanceDist.Add(Math.Abs(row[(int)SampleValuesOutcomeCodesIndices.BuyRatio] - row[(int)SampleValuesOutcomeCodesIndices.SellRatio]));
                        regardedStates++;
                    }
                }

                predictivePower[(int)LearningIndicatorPredictivePowerIndecies.valuesOverMinPercentRatioCode] = Convert.ToDouble(regardedStates) / outcomeCodeSamplingTable.Length;

                if (regardedStates <= 2)
                {
                    throw new TooLittleStatesException("Too little sates: " + regardedStates);
                }

                predictivePower[(int)LearningIndicatorPredictivePowerIndecies.buyCodeStD]             = buyCodesDist.StandardDeviation();
                predictivePower[(int)LearningIndicatorPredictivePowerIndecies.sellCodeStD]            = sellCodesDist.StandardDeviation();
                predictivePower[(int)LearningIndicatorPredictivePowerIndecies.buySellCodeDistanceStD] = buySellDistanceDist.StandardDeviation();

                //Outcome

                double totalSamples = 0;
                foreach (double[] row in outcomeSamplingTable)
                {
                    totalSamples += row[(int)SampleValuesOutcomeIndices.SamplesCount];
                }

                //Avgs
                regardedStates = 0;
                foreach (double[] row in outcomeSamplingTable)
                {
                    if ((row[(int)SampleValuesOutcomeIndices.SamplesCount] / totalSamples) * 100 > minPercentThreshold) //minPercentThreshold
                    {
                        maxDist.Add(row[(int)SampleValuesOutcomeIndices.MaxAvg]);
                        minDist.Add(row[(int)SampleValuesOutcomeIndices.MinAvg]);
                        minMaxDistanceDist.Add(Math.Abs(row[(int)SampleValuesOutcomeIndices.MaxAvg]) + row[(int)SampleValuesOutcomeIndices.MinAvg]);
                        actualDist.Add(row[(int)SampleValuesOutcomeIndices.ActualAvg]);
                        regardedStates++;
                    }
                }

                predictivePower[(int)LearningIndicatorPredictivePowerIndecies.valuesOverMinPercentRatioOutcome] += Convert.ToDouble(regardedStates) / outcomeSamplingTable.Length;

                //avg distances
                predictivePower[(int)LearningIndicatorPredictivePowerIndecies.maxStD]            = maxDist.StandardDeviation();
                predictivePower[(int)LearningIndicatorPredictivePowerIndecies.minStD]            = minDist.StandardDeviation();
                predictivePower[(int)LearningIndicatorPredictivePowerIndecies.minMaxDistanceStd] = minMaxDistanceDist.StandardDeviation();
                predictivePower[(int)LearningIndicatorPredictivePowerIndecies.actualStD]         = actualDist.StandardDeviation();

                if (double.IsNaN(predictivePower[(int)LearningIndicatorPredictivePowerIndecies.buyCodeStD]) ||
                    double.IsNaN(predictivePower[(int)LearningIndicatorPredictivePowerIndecies.sellCodeStD]) ||
                    double.IsNaN(predictivePower[(int)LearningIndicatorPredictivePowerIndecies.buySellCodeDistanceStD]) ||
                    double.IsNaN(predictivePower[(int)LearningIndicatorPredictivePowerIndecies.maxStD]) ||
                    double.IsNaN(predictivePower[(int)LearningIndicatorPredictivePowerIndecies.minStD]) ||
                    double.IsNaN(predictivePower[(int)LearningIndicatorPredictivePowerIndecies.minMaxDistanceStd]) ||
                    double.IsNaN(predictivePower[(int)LearningIndicatorPredictivePowerIndecies.actualStD]))
                {
                    throw new Exception("Not a valid predictive power!");
                }

                //End predictive power calculation
            }

            this.indicator = indicator;
        }