/// <summary>
        /// Generate neural ideal data for the specified index.
        /// </summary>
        /// <param name="index">The index to generate for.</param>
        /// <returns>The neural data generated.</returns>
        public virtual BasicNeuralData GenerateOutputNeuralData(int index)
        {
            if (index + this.predictWindowSize > this.points.Count)
            {
                throw new TemporalError("Can't generate prediction temporal data "
                                        + "beyond the end of provided data.");
            }

            BasicNeuralData result      = new BasicNeuralData(this.outputNeuronCount);
            int             resultIndex = 0;

            for (int i = 0; i < this.predictWindowSize; i++)
            {
                int descriptionIndex = 0;

                foreach (TemporalDataDescription desc in this.descriptions)
                {
                    if (desc.IsPredict)
                    {
                        result[resultIndex++] = this.FormatData(desc, index
                                                                + i);
                    }
                    descriptionIndex++;
                }
            }
            return(result);
        }
Пример #2
0
        public PredicResults Predict()
        {
            double[] present      = new double[INPUT_TUPLES * INDEXES_TO_CONSIDER];
            double[] actualOutput = new double[OUTPUT_SIZE];
            int      index        = 0;

            index = _manager.Samples.Count;
            var result = new PredicResults();

            _manager.GetInputData(index - INPUT_TUPLES, present);
            //_manager.GetOutputData(index - INPUT_TUPLES, actualOutput);
            var data    = new BasicNeuralData(present);
            var predict = _network.Compute(data);

            //result.ActualClose = actualOutput[0] * (_manager.GetMax((int)PredicInputIndexe.CloseIndex) - _manager.GetMin((int)PredicInputIndexe.CloseIndex)) + _manager.GetMin((int)PredicInputIndexe.CloseIndex);
            result.PredictedClose = predict[0] * (_manager.GetMax((int)PredicInputIndexe.CloseIndex) - _manager.GetMin((int)PredicInputIndexe.CloseIndex)) + _manager.GetMin((int)PredicInputIndexe.CloseIndex);
            //result.ActualPir = actualOutput[1] * (_manager.MaxPrimeRate - _manager.MinPrimeRate) + _manager.MinPrimeRate;
            //result.PredictedPir = predict[1] * (_manager.MaxPrimeRate - _manager.MinPrimeRate) + _manager.MinPrimeRate;
            //result.ActualDow = actualOutput[2] * (_manager.MaxDow - _manager.MinDow) + _manager.MinDow;
            //result.PredictedDow = predict[2] * (_manager.MaxDow - _manager.MinDow) + _manager.MinDow;
            //result.ActualNasdaq = actualOutput[3] * (_manager.MaxNasdaq - _manager.MinNasdaq) + _manager.MinNasdaq;
            //result.PredictedNasdaq = predict[3] * (_manager.MaxNasdaq - _manager.MinNasdaq) + _manager.MinNasdaq;
            result.Date = _manager.GetDateTime(index - 1).AddDays(3);
            //ErrorCalculation error = new ErrorCalculation();
            //error.UpdateError(actualOutput, predict);
            //result.Error = error.CalculateRMS();
            result.Error = 0;


            return(result);
        }
        /// <summary>
        /// Write an array.
        /// </summary>
        /// <param name="data">The data to write.</param>
        /// <param name="inputCount">How much of the data is input.</param>
        public void Write(double[] data, int inputCount)
        {
            if (this.idealCount == 0)
            {
                BasicNeuralData inputData = new BasicNeuralData(data);
                this.dataset.Add(inputData);
            }
            else
            {
                BasicNeuralData inputData = new BasicNeuralData(
                    this.inputCount);
                BasicNeuralData idealData = new BasicNeuralData(
                    this.idealCount);

                int index = 0;
                for (int i = 0; i < this.inputCount; i++)
                {
                    inputData[i] = data[index++];
                }

                for (int i = 0; i < this.idealCount; i++)
                {
                    idealData[i] = data[index++];
                }

                this.dataset.Add(inputData, idealData);
            }
        }
Пример #4
0
        /// <inheritdoc/>
        public void Compute(double[] input, double[] output)
        {
            BasicNeuralData input2  = new BasicNeuralData(input);
            INeuralData     output2 = this.Compute(input2);

            EngineArray.ArrayCopy(output2.Data, output);
        }
Пример #5
0
            /// <summary>
            /// Move to the next object.
            /// </summary>
            /// <returns>True if there is a next object.</returns>
            public bool MoveNext()
            {
                if (!this.results.NextResult())
                {
                    return(false);
                }
                INeuralData input = new BasicNeuralData(owner.inputSize);
                INeuralData ideal = null;

                for (int i = 1; i <= owner.inputSize; i++)
                {
                    input[i - 1] = this.results.GetDouble(i);
                }

                if (owner.idealSize > 0)
                {
                    ideal =
                        new BasicNeuralData(owner.idealSize);
                    for (int i = 1; i <= owner.idealSize; i++)
                    {
                        ideal[i - 1] =
                            this.results.GetDouble(i + owner.inputSize);
                    }
                }

                this.current = new BasicNeuralDataPair(input, ideal);
                return(true);
            }
Пример #6
0
        /// <summary>
        /// Generate random training into a training set.
        /// </summary>
        /// <param name="training">The training set to generate into.</param>
        /// <param name="seed">The seed to use.</param>
        /// <param name="count">How much data to generate.</param>
        /// <param name="min">The low random value.</param>
        /// <param name="max">The high random value.</param>
        public static void Generate(INeuralDataSet training,
                                    long seed,
                                    int count,
                                    double min, double max)
        {
            LinearCongruentialGenerator rand
                = new LinearCongruentialGenerator(seed);

            int inputCount = training.InputSize;
            int idealCount = training.IdealSize;

            for (int i = 0; i < count; i++)
            {
                INeuralData inputData = new BasicNeuralData(inputCount);

                for (int j = 0; j < inputCount; j++)
                {
                    inputData[j] = rand.Range(min, max);
                }

                INeuralData idealData = new BasicNeuralData(idealCount);

                for (int j = 0; j < idealCount; j++)
                {
                    idealData[j] = rand.Range(min, max);
                }

                BasicNeuralDataPair pair = new BasicNeuralDataPair(inputData,
                                                                   idealData);
                training.Add(pair);
            }
        }
Пример #7
0
        /// <summary>
        /// Generate the input to the neural network to predict.  It will look at the current
        /// date as well as the number of days leading up to it specified by EvalWindow.
        /// This method is used both internally and externally.
        /// </summary>
        /// <param name="marketData">The market data to use.</param>
        /// <param name="marketDataIndex">The point that we want to predict from.</param>
        /// <returns></returns>
        public INeuralData CreateData(
            List <LoadedMarketData> marketData,
            int marketDataIndex)
        {
            INeuralData neuralData    = new BasicNeuralData(14);
            int         totalPatterns = 0;
            var         patternCount  = new int[14];

            for (var i = 0; i < EvalWindow; i++)
            {
                var data = marketData[(marketDataIndex - EvalWindow) + i];

                var candle = new IdentifyCandleStick();
                candle.SetStats(data);
                var pattern = candle.DeterminePattern();
                if (pattern == IdentifyCandleStick.UNKNOWN)
                {
                    continue;
                }
                totalPatterns++;
                patternCount[pattern]++;
            }

            if (totalPatterns == 0)
            {
                return(null);
            }

            for (var i = 0; i < 14; i++)
            {
                neuralData[i] = patternCount[i] / ((double)totalPatterns);
            }

            return(neuralData);
        }
Пример #8
0
        /// <summary>
        /// Generate a random training set.
        /// </summary>
        /// <param name="seed">The seed value to use, the same seed value will always produce
        /// the same results.</param>
        /// <param name="count">How many training items to generate.</param>
        /// <param name="inputCount">How many input numbers.</param>
        /// <param name="idealCount">How many ideal numbers.</param>
        /// <param name="min">The minimum random number.</param>
        /// <param name="max">The maximum random number.</param>
        /// <returns>The random training set.</returns>
        public static BasicNeuralDataSet Generate(long seed,
                                                  int count, int inputCount,
                                                  int idealCount, double min, double max)
        {
            LinearCongruentialGenerator rand =
                new LinearCongruentialGenerator(seed);

            BasicNeuralDataSet result = new BasicNeuralDataSet();

            for (int i = 0; i < count; i++)
            {
                INeuralData inputData = new BasicNeuralData(inputCount);

                for (int j = 0; j < inputCount; j++)
                {
                    inputData.Data[j] = rand.Range(min, max);
                }

                INeuralData idealData = new BasicNeuralData(idealCount);

                for (int j = 0; j < idealCount; j++)
                {
                    idealData[j] = rand.Range(min, max);
                }

                BasicNeuralDataPair pair = new BasicNeuralDataPair(inputData,
                                                                   idealData);
                result.Add(pair);
            }
            return(result);
        }
Пример #9
0
        /// <summary>
        /// Compute the output for a given input to the neural network. This method
        /// provides a parameter to specify an output holder to use.  This holder
        /// allows propagation training to track the output from each layer.
        /// If you do not need this holder pass null, or use the other
        /// compare method.
        /// </summary>
        /// <param name="input">The input provide to the neural network.</param>
        /// <param name="useHolder">Allows a holder to be specified, this allows
        /// propagation training to check the output of each layer.</param>
        /// <returns>The results from the output neurons.</returns>
        public virtual INeuralData Compute(INeuralData input,
                                           NeuralOutputHolder useHolder)
        {
            NeuralOutputHolder holder;

            ILayer inputLayer = this.network.GetLayer(BasicNetwork.TAG_INPUT);

#if logging
            if (FeedforwardLogic.logger.IsDebugEnabled)
            {
                FeedforwardLogic.logger.Debug("Pattern " + input.ToString()
                                              + " presented to neural network");
            }
#endif

            if (useHolder == null && this.network.Structure.Flat != null)
            {
                this.network.Structure.UpdateFlatNetwork();
                INeuralData result = new BasicNeuralData(this.network.Structure.Flat.OutputCount);
                this.network.Structure.Flat.Compute(input.Data, result.Data);
                return(result);
            }

            if (useHolder == null)
            {
                holder = new NeuralOutputHolder();
            }
            else
            {
                holder = useHolder;
            }

            Compute(holder, inputLayer, input, null);
            return(holder.Output);
        }
Пример #10
0
        /// <summary>
        /// Compute the values before sending output to the next layer.
        /// This function allows the activation functions to be called.
        /// </summary>
        /// <param name="pattern">The incoming Project.</param>
        /// <returns>The output from this layer.</returns>
        public override INeuralData Compute(INeuralData pattern)
        {
            INeuralData result = new BasicNeuralData(NeuronCount);

            for (int i = 0; i < NeuronCount; i++)
            {
                if (this.radialBasisFunction[i] == null)
                {
                    String str =
                        "Error, must define radial functions for each neuron";
#if logging
                    if (RadialBasisFunctionLayer.logger.IsErrorEnabled)
                    {
                        RadialBasisFunctionLayer.logger.Error(str);
                    }
#endif
                    throw new NeuralNetworkError(str);
                }

                IRadialBasisFunction f = this.radialBasisFunction[i];

                if (pattern.Data.Length != f.Dimensions)
                {
                    throw new Exception("Inputs must equal the number of dimensions.");
                }

                result[i] = f.Calculate(pattern.Data);
            }

            return(result);
        }
Пример #11
0
        public List <PredictionResults> Predict(DateTime predictFrom, DateTime predictTo)
        {
            List <PredictionResults> results = new List <PredictionResults>();

            double[] present      = new double[InputTuples * IndexesToConsider];
            double[] actualOutput = new double[OutputSize];
            int      index        = 0;

            foreach (var sample in _manager.Samples)
            {
                if (sample.Date.CompareTo(predictFrom) > 0 && sample.Date.CompareTo(predictTo) < 0)
                {
                    var result = new PredictionResults();
                    _manager.GetInputData(index - InputTuples, present);
                    _manager.GetOutputData(index - InputTuples, actualOutput);
                    var data    = new BasicNeuralData(present);
                    var predict = _network.Compute(data);
                    result.ActualLotos    = actualOutput[0] * (_manager.MaxLotos - _manager.MinLotos) + _manager.MinLotos;
                    result.PredictedLotos = predict[0] * (_manager.MaxLotos - _manager.MinLotos) + _manager.MinLotos;
                    result.ActualPir      = actualOutput[1] * (_manager.MaxPrimeRate - _manager.MinPrimeRate) + _manager.MinPrimeRate;
                    result.PredictedPir   = predict[1] * (_manager.MaxPrimeRate - _manager.MinPrimeRate) + _manager.MinPrimeRate;
                    result.ActualOrlen    = actualOutput[2] * (_manager.MaxOrlen - _manager.MinOrlen) + _manager.MinOrlen;
                    result.PredictedOrlen = predict[2] * (_manager.MaxOrlen - _manager.MinOrlen) + _manager.MinOrlen;
                    result.Date           = sample.Date;
                    var error = new ErrorCalculation();
                    error.UpdateError(actualOutput, predict.Data);
                    result.Error = error.CalculateRMS();
                    results.Add(result);
                }
                index++;
            }
            return(results);
        }
Пример #12
0
        /// <summary>
        /// Construct the LMA object.
        /// </summary>
        /// <param name="network">The network to train. Must have a single output neuron.</param>
        /// <param name="training">The training data to use. Must be indexable.</param>
        public SVDTraining(BasicNetwork network, INeuralDataSet training)
        {
            ILayer outputLayer = network.GetLayer(BasicNetwork.TAG_OUTPUT);

            if (outputLayer == null)
            {
                throw new TrainingError("SVD requires an output layer.");
            }

            if (outputLayer.NeuronCount != 1)
            {
                throw new TrainingError("SVD requires an output layer with a single neuron.");
            }

            if (network.GetLayer(RadialBasisPattern.RBF_LAYER) == null)
            {
                throw new TrainingError("SVD is only tested to work on radial basis function networks.");
            }

            rbfLayer = (RadialBasisFunctionLayer)network.GetLayer(RadialBasisPattern.RBF_LAYER);

            this.Training       = training;
            this.network        = network;
            this.trainingLength = (int)this.Training.InputSize;

            BasicNeuralData input = new BasicNeuralData(this.Training.InputSize);
            BasicNeuralData ideal = new BasicNeuralData(this.Training.IdealSize);

            this.pair = new BasicNeuralDataPair(input, ideal);
        }
Пример #13
0
            /// <summary>
            /// Internal function to obtain the next training set item.
            /// </summary>
            /// <returns>True if one was found.</returns>
            private bool ObtainNext()
            {
                if (!this.reader.FindTag(this.owner.PairXML, true))
                {
                    return(false);
                }

                INeuralData input = new BasicNeuralData(
                    this.owner.InputSize);
                INeuralData ideal = new BasicNeuralData(
                    this.owner.IdealSize);

                if (!this.reader.FindTag(owner.InputXML, true))
                {
                    InvalidError();
                }

                for (int i = 0; i < owner.InputSize; i++)
                {
                    if (!this.reader.FindTag(owner.ValueXML, true))
                    {
                        InvalidError();
                    }
                    String str = this.reader.ReadTextToTag();
                    input[i] = double.Parse(str);
                }

                if (owner.IdealSize > 0)
                {
                    if (!this.reader.FindTag(owner.IdealXML, true))
                    {
                        InvalidError();
                    }

                    for (int i = 0; i < owner.IdealSize; i++)
                    {
                        if (!this.reader.FindTag(owner.ValueXML, true))
                        {
                            InvalidError();
                        }
                        String str = this.reader.ReadTextToTag();
                        ideal[i] = double.Parse(str);
                    }
                }

                if (ideal != null)
                {
                    this.nextPair = new BasicNeuralDataPair(input, ideal);
                }
                else
                {
                    this.nextPair = new BasicNeuralDataPair(input);
                }

                return(true);
            }
Пример #14
0
        /// <summary>
        /// Build "input data for a neural network" based on the input values
        /// provided.  This allows  input for a neural network to be normalized.
        /// This is typically used when data is to be presented to a trained
        /// neural network.
        /// </summary>
        /// <param name="data">The input values to be normalized.</param>
        /// <returns>The data to be sent to the neural network.</returns>
        public IMLData BuildForNetworkInput(double[] data)
        {
            // feed the input fields
            int index = 0;

            foreach (IInputField field in _inputFields)
            {
                if (field.UsedForNetworkInput)
                {
                    if (index >= data.Length)
                    {
                        throw new NormalizationError(
                                  "Can't build data, input fields used for neural input, must match provided data("
                                  + data.Length + ").");
                    }
                    field.CurrentValue = data[index++];
                }
            }

            // count the output fields
            int outputCount = 0;

            foreach (IOutputField ofield in _outputFields)
            {
                if (!ofield.Ideal)
                {
                    for (int sub = 0; sub < ofield.SubfieldCount; sub++)
                    {
                        outputCount++;
                    }
                }
            }

            // process the output fields

            InitForOutput();

            var result = new BasicNeuralData(outputCount);

            // write the value
            int outputIndex = 0;

            foreach (IOutputField ofield in _outputFields)
            {
                if (!ofield.Ideal)
                {
                    for (int sub = 0; sub < ofield.SubfieldCount; sub++)
                    {
                        result[outputIndex++] = ofield.Calculate(sub);
                    }
                }
            }

            return(result);
        }
Пример #15
0
    public void testNetwork(double[][] networkOutputs)
    {
      for (int i = 0; i < networkOutputs.Length; i++)
      {
        //INeuralData data = new BasicNeuralData(networkInput[i]);
        //networkOutputs[i] = (double[])network.Compute(data).Data.Clone();

        BasicNeuralData inputs = new BasicNeuralData(networkInput[i]);
        INeuralData output = network.Compute(inputs);
        EngineArray.ArrayCopy(output.Data, networkOutputs[i]);
      }
    }
Пример #16
0
    /// <summary>
    /// Tests the network using defined inputs and ideal outputs. Returns a 2D matrix of the predicted
    /// values, which can then somewhere else be compared against the idealoutputs.
    /// </summary>
    /// <returns></returns>
    public double[][] testNetwork()
    {
      double[][] networkOutputs = new double[networkInput.Length][];

      for (int i = 0; i < networkOutputs.Length; i++)
      {
        INeuralData data = new BasicNeuralData(networkInput[i]);
        networkOutputs[i] = (double[])network.Compute(data).Data.Clone();
      }

      return networkOutputs;
    }
Пример #17
0
        /// <summary>
        /// Compute the output for the given input.
        /// </summary>
        /// <param name="input">The input to the SVM.</param>
        /// <returns>The results from the SVM.</returns>
        public override INeuralData Compute(INeuralData input)
        {
            INeuralData result = new BasicNeuralData(this.outputCount);

            svm_node[] formattedInput = MakeSparse(input);

            for (int i = 0; i < this.outputCount; i++)
            {
                double d = svm.svm_predict(this.models[i], formattedInput);
                result[i] = d;
            }
            return(result);
        }
Пример #18
0
        private BasicNeuralData GenerateInputNeuralData(List <Quant> inputQuants)
        {
            var result      = new BasicNeuralData(inputQuants.Count);
            int resultIndex = 0;

            for (int i = 0; i < inputQuants.Count; i++)
            {
                result[resultIndex++] = i == 0
                    ? 0.0
                    : (inputQuants[i].Close - inputQuants[i - 1].Close) / inputQuants[i - 1].Close;
            }
            return(result);
        }
Пример #19
0
        /// <summary>
        /// Generate the training sets.
        /// </summary>
        public virtual void Generate()
        {
            SortPoints();
            int start   = CalculateStartIndex() + 1;
            int setSize = CalculateActualSetSize();
            int range   = start
                          + (setSize - _predictWindowSize - _inputWindowSize);

            for (int i = start; i < range; i++)
            {
                BasicNeuralData input = GenerateInputNeuralData(i);
                BasicNeuralData ideal = GenerateOutputNeuralData(i + _inputWindowSize);
                var             pair  = new BasicNeuralDataPair(input, ideal);
                base.Add(pair);
            }
        }
Пример #20
0
        /// <summary>
        /// Construct the chain rule calculation.
        /// </summary>
        /// <param name="network">The network to use.</param>
        /// <param name="indexableTraining">The training set to use.</param>
        public JacobianChainRule(BasicNetwork network,
                                 IIndexable indexableTraining)
        {
            this.indexableTraining = indexableTraining;
            this.network           = network;
            this.parameterSize     = network.Structure.CalculateSize();
            this.inputLength       = (int)this.indexableTraining.Count;
            this.jacobian          = EngineArray.AllocateDouble2D(this.inputLength, this.parameterSize);
            this.rowErrors         = new double[this.inputLength];

            BasicNeuralData input = new BasicNeuralData(
                this.indexableTraining.InputSize);
            BasicNeuralData ideal = new BasicNeuralData(
                this.indexableTraining.IdealSize);

            this.pair = new BasicNeuralDataPair(input, ideal);
        }
Пример #21
0
        /// <summary>
        /// Compute the weightless output from this synapse. Each neuron
        /// in the from layer has a weightless connection to each of the
        /// neurons in the next layer.
        /// </summary>
        /// <param name="input">The input from the synapse.</param>
        /// <returns>The output from this synapse.</returns>
        public override INeuralData Compute(INeuralData input)
        {
            INeuralData result = new BasicNeuralData(this.ToNeuronCount);
            // just sum the input
            double sum = 0;

            for (int i = 0; i < input.Count; i++)
            {
                sum += input[i];
            }

            for (int i = 0; i < this.ToNeuronCount; i++)
            {
                result[i] = sum;
            }
            return(result);
        }
Пример #22
0
        /// <summary>
        /// Construct the LMA object.
        /// </summary>
        /// <param name="network">The network to train. Must have a single output neuron.</param>
        /// <param name="training">The training data to use. Must be indexable.</param>
        public LevenbergMarquardtTraining(BasicNetwork network,
                                          INeuralDataSet training)
        {
            if (!(training is IIndexable))
            {
                throw new TrainingError(
                          "Levenberg Marquardt requires an indexable training set.");
            }

            ILayer outputLayer = network.GetLayer(BasicNetwork.TAG_OUTPUT);

            if (outputLayer == null)
            {
                throw new TrainingError(
                          "Levenberg Marquardt requires an output layer.");
            }

            if (outputLayer.NeuronCount != 1)
            {
                throw new TrainingError(
                          "Levenberg Marquardt requires an output layer with a single neuron.");
            }

            this.Training          = training;
            this.indexableTraining = (IIndexable)Training;
            this.network           = network;
            this.trainingLength    = (int)this.indexableTraining.Count;
            this.parametersLength  = this.network.Structure.CalculateSize();
            this.hessianMatrix     = new Matrix(this.parametersLength,
                                                this.parametersLength);
            this.hessian  = this.hessianMatrix.Data;
            this.alpha    = 0.0;
            this.beta     = 1.0;
            this.lambda   = 0.1;
            this.deltas   = new double[this.parametersLength];
            this.gradient = new double[this.parametersLength];
            this.diagonal = new double[this.parametersLength];

            BasicNeuralData input = new BasicNeuralData(
                this.indexableTraining.InputSize);
            BasicNeuralData ideal = new BasicNeuralData(
                this.indexableTraining.IdealSize);

            this.pair = new BasicNeuralDataPair(input, ideal);
        }
Пример #23
0
        /// <summary>
        /// Compute the weighted output from this synapse. Each neuron
        /// in the from layer has a weighted connection to each of the
        /// neurons in the next layer.
        /// </summary>
        /// <param name="input">The input from the synapse.</param>
        /// <returns>The output from this synapse.</returns>
        public override INeuralData Compute(INeuralData input)
        {
            INeuralData result = new BasicNeuralData(this.ToNeuronCount);

            double[]   inputArray  = input.Data;
            double[][] matrixArray = this.WeightMatrix.Data;
            double[]   resultArray = result.Data;

            for (int i = 0; i < this.ToNeuronCount; i++)
            {
                double sum = 0;
                for (int j = 0; j < inputArray.Length; j++)
                {
                    sum += inputArray[j] * matrixArray[j][i];
                }
                resultArray[i] = sum;
            }
            return(result);
        }
        /// <summary>
        /// Meramalkan data
        /// </summary>
        /// <param name="step">step peramalan</param>
        /// <returns>hasil peramalan</returns>
        public double[] Forecast(int step)
        {
            if (step < 1)
            {
                step = 1;
            }

            int inputsCount = this.network.InputLayer.NeuronCount;

            double[] result             = new double[step];
            double[] inputForOut        = new double[inputsCount];
            double[] inputForOutNetwork = new double[inputsCount];

            //input for forecasting
            for (int j = 0; j < inputsCount; j++)
            {
                inputForOut[inputsCount - 1 - j] = this.data[this.data.Length - 1 - j];
            }

            //forecast
            for (int i = 0; i < step; i++)
            {
                for (int j = 0; j < inputsCount; j++)
                {
                    inputForOutNetwork[j] = (inputForOut[j] - this.minData) * this.factor + this.minNormalizedData;
                }

                INeuralData neuraldata = new BasicNeuralData(inputForOutNetwork);
                // evalue the function
                result[i] = (this.network.Compute(neuraldata)[0] - this.minNormalizedData) / this.factor + this.minData;

                for (int j = 0; j < inputsCount - 1; j++)
                {
                    inputForOut[j] = inputForOut[j + 1];
                }

                inputForOut[inputsCount - 1] = result[i];
            }

            return(result);
        }
Пример #25
0
        /// <summary>
        /// Predict the results
        /// </summary>
        /// <returns>List with the prediction results</returns>
        public List <PredicResults> Predict(DateTime predictFrom, DateTime predictTo)
        {
            var results = new List <PredicResults>();

            double[] present      = new double[INPUT_TUPLES * INDEXES_TO_CONSIDER];
            double[] actualOutput = new double[OUTPUT_SIZE];
            int      index        = 0;

            foreach (var sample in _manager.Samples)
            {
                if (sample.Date.Date.CompareTo(predictFrom.Date) > 0 && sample.Date.Date.CompareTo(predictTo.Date) <= 0)
                {
                    var result = new PredicResults();
                    _manager.GetInputData(index - INPUT_TUPLES, present);
                    // _manager.GetOutputData(index - INPUT_TUPLES, actualOutput);
                    var data    = new BasicNeuralData(present);
                    var predict = _network.Compute(data);
                    //result.ActualClose = actualOutput[0] * (_manager.GetMax((int)PredicInputIndexe.CloseIndex) - _manager.GetMin((int)PredicInputIndexe.CloseIndex)) + _manager.GetMin((int)PredicInputIndexe.CloseIndex);
                    result.PredictedClose = predict[0] * (_manager.GetMax((int)PredicInputIndexe.CloseIndex) - _manager.GetMin((int)PredicInputIndexe.CloseIndex)) + _manager.GetMin((int)PredicInputIndexe.CloseIndex);
                    //result.ActualPir = actualOutput[1] * (_manager.MaxPrimeRate - _manager.MinPrimeRate) + _manager.MinPrimeRate;
                    //result.PredictedPir = predict[1] * (_manager.MaxPrimeRate - _manager.MinPrimeRate) + _manager.MinPrimeRate;
                    //result.ActualDow = actualOutput[2] * (_manager.MaxDow - _manager.MinDow) + _manager.MinDow;
                    //result.PredictedDow = predict[2] * (_manager.MaxDow - _manager.MinDow) + _manager.MinDow;
                    //result.ActualNasdaq = actualOutput[3] * (_manager.MaxNasdaq - _manager.MinNasdaq) + _manager.MinNasdaq;
                    //result.PredictedNasdaq = predict[3] * (_manager.MaxNasdaq - _manager.MinNasdaq) + _manager.MinNasdaq;
                    result.Date = sample.Date;
                    ErrorCalculation error = new ErrorCalculation();
                    //error.UpdateError(actualOutput, predict);
                    //result.Error = error.CalculateRMS();
                    //result.Error = result.ActualClose - result.PredictedClose;
                    result.Error       = 0;
                    result.ActualClose = 0;
                    results.Add(result);
                }
                index++;
            }
            return(results);
        }
Пример #26
0
        /// <summary>
        /// Create one training pair, either good or bad.
        /// </summary>
        /// <param name="data">The data to create from.</param>
        /// <param name="index">The index into the data to create from.</param>
        /// <param name="good">True if this was a good(bullish) period.</param>
        /// <returns></returns>
        public IMLDataPair CreateData(List <LoadedMarketData> data, int index, bool good)
        {
            var ideal = new BasicNeuralData(1);

            INeuralData input = CreateData(data, index);

            if (input == null)
            {
                return(null);
            }

            // ideal
            if (good)
            {
                ideal[0] = 0.9;
            }
            else
            {
                ideal[0] = 0.1;
            }

            return(new BasicMLDataPair(input, ideal));
        }
Пример #27
0
        /// <summary>
        /// Generate input neural data for the specified index.
        /// </summary>
        /// <param name="index">The index to generate neural data for.</param>
        /// <returns>The input neural data generated.</returns>
        public virtual BasicNeuralData GenerateInputNeuralData(int index)
        {
            if (index + _inputWindowSize > _points.Count)
            {
                throw new TemporalError("Can't generate input temporal data "
                                        + "beyond the end of provided data.");
            }

            var result      = new BasicNeuralData(_inputNeuronCount);
            int resultIndex = 0;

            for (int i = 0; i < _inputWindowSize; i++)
            {
                foreach (TemporalDataDescription desc in _descriptions)
                {
                    if (desc.IsInput)
                    {
                        result[resultIndex++] = FormatData(desc, index
                                                           + i);
                    }
                }
            }
            return(result);
        }
        /// <summary>
        /// Handle reading an item tag.
        /// </summary>
        /// <param name="xmlIn">The XML reader.</param>
        private void HandleItem(ReadXML xmlIn)
        {
            IDictionary <String, String> properties = xmlIn.ReadPropertyBlock();
            INeuralDataPair pair  = null;
            INeuralData     input = new BasicNeuralData(NumberList
                                                        .FromList(CSVFormat.EG_FORMAT, properties
                                                                  [BasicNeuralDataSetPersistor.TAG_INPUT]));

            if (properties.ContainsKey(BasicNeuralDataSetPersistor.TAG_IDEAL))
            {
                // supervised
                INeuralData ideal = new BasicNeuralData(NumberList
                                                        .FromList(CSVFormat.EG_FORMAT, properties
                                                                  [BasicNeuralDataSetPersistor.TAG_IDEAL]));
                pair = new BasicNeuralDataPair(input, ideal);
            }
            else
            {
                // unsupervised
                pair = new BasicNeuralDataPair(input);
            }

            this.currentDataSet.Add(pair);
        }
Пример #29
0
        public string Post(string symbol, double close, long time)
        {
            DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            var      date   = origin.AddSeconds(time);
            var      symb   = _context.Symbols.SingleOrDefault(s => s.Name == symbol);

            if (symb == null)
            {
                symb = new Symbol()
                {
                    Name = symbol
                };
                _context.Symbols.Add(symb);
                _context.SaveChanges();
            }

            var tick = new Tick()
            {
                Id       = Guid.NewGuid(),
                SymbolId = symb.Id,
                Close    = close,
                Time     = date
            };

            _context.Ticks.Add(tick);
            _context.SaveChanges();

            var quant = _context.Quants.Where(q => q.SymbolId == symb.Id).OrderByDescending(q => q.Ind).FirstOrDefault();

            if (quant == null)
            {
                _context.Quants.Add(new Quant()
                {
                    Id             = Guid.NewGuid(),
                    SymbolId       = symb.Id,
                    Close          = close,
                    Recommendation = Recommendation.CloseAll,
                    Ind            = 0,
                    TickId         = tick.Id
                });
                return("ok");
            }

            var dif = Math.Abs(close - quant.Close);

            if (dif > symb.QuantSize)
            {
                var network = _context.Networks.SingleOrDefault(n => n.SymbolId == symb.Id);
                var sign    = close < quant.Close ? -1 : 1;
                var steps   = (int)(dif / symb.QuantSize);
                for (int i = 1; i <= steps; i++)
                {
                    Recommendation recommendation = Recommendation.CloseAll;
                    var            newQuantVal    = quant.Close + symb.QuantSize * sign * i;
                    var            newQuant       = new Quant()
                    {
                        TickId         = tick.Id,
                        Close          = newQuantVal,
                        Ind            = quant.Ind + i,
                        Recommendation = recommendation
                    };

                    if (network != null)
                    {
                        network.QuantsProcessed++;

                        if (i == steps)
                        {
                            var inputQuants = _context.Quants.Where(q => q.SymbolId == symb.Id).OrderByDescending(q => q.Ind).Take(24).Reverse().ToList();
                            inputQuants.Add(newQuant);
                            if (inputQuants.Count == 25)
                            {
                                var predictor = (BasicNetwork)EncogDirectoryPersistence.LoadObject(new FileInfo(network.FileName));

                                BasicNeuralData input = GenerateInputNeuralData(inputQuants);

                                var predictData = predictor.Compute(input)[0];
                                newQuant.Recommendation = predictData < 0 ? Recommendation.Sell : Recommendation.Buy;
                            }

                            if (network.QuantsProcessed > 200)
                            {
                                _context.NetworkCalculationTasks.Add(new NetworkCalculationTask()
                                {
                                    ShouldStartAt = DateTime.Today.AddHours(18).AddMinutes(10),
                                    Status        = TaskStatus.New,
                                    SymbolId      = symb.Id
                                });
                            }
                        }
                    }

                    _context.Quants.Add(newQuant);
                    _context.SaveChanges();
                }
            }

            return("ok");
        }
        /// <summary>
        /// Mencari solusi model neural network
        /// </summary>
        private void searchSolution()
        {
            // Normalize Data
            switch (this.selectedActivationFunction)
            {
            case ActivationFunctionEnumeration.SemiLinearFunction:
                this.activationFunction = new SemiLinearFunction();
                this.normalizeData(0.1, 0.9);
                break;

            case ActivationFunctionEnumeration.SigmoidFunction:
                this.activationFunction = new SigmoidFunction();
                this.normalizeData(0.1, 0.9);
                break;

            case ActivationFunctionEnumeration.BipolarSigmoidFunction:
                this.activationFunction = new BipolarSigmoidFunction();
                this.normalizeData(-0.9, 0.9);
                break;

            case ActivationFunctionEnumeration.HyperbolicTangentFunction:
                this.activationFunction = new HyperbolicTangentFunction();
                this.normalizeData(-0.9, 0.9);
                break;

            default:
                this.activationFunction = new BipolarSigmoidFunction();
                this.normalizeData(-0.9, 0.9);
                break;
            }

            //create network
            this.network = new BasicNetwork();
            this.network.AddLayer(new FeedforwardLayer(this.activationFunction, this.inputLayerNeurons));
            this.network.AddLayer(new FeedforwardLayer(this.activationFunction, this.hiddenLayerNeurons));
            this.network.AddLayer(new FeedforwardLayer(this.activationFunction, this.outputLayerNeurons));
            this.network.Reset();

            //variable for looping
            //needToStop = false;
            double mse = 0.0, error = 0.0, mae = 0.0;
            int    iteration = 1;

            // parameters
            double msle = 0.0, mspe = 0.0, generalizationLoss = 0.0, pq = 0.0;

            double[] trainingErrors = new double[this.strip];
            for (int i = 0; i < this.strip; i++)
            {
                trainingErrors[i] = double.MaxValue / strip;
            }

            double lastMSE = double.MaxValue;

            // advanced early stopping
            int n             = this.data.Length - this.network.InputLayer.NeuronCount;
            int validationSet = (int)Math.Round(this.validationSetRatio * n);
            int trainingSet   = n - validationSet;

            double[][] networkTrainingInput  = new double[trainingSet][];
            double[][] networkTrainingOutput = new double[trainingSet][];
            for (int i = 0; i < trainingSet; i++)
            {
                networkTrainingInput[i]  = new double[this.network.InputLayer.NeuronCount];
                networkTrainingOutput[i] = new double[1];
            }
            for (int i = 0; i < trainingSet; i++)
            {
                for (int j = 0; j < this.network.InputLayer.NeuronCount; j++)
                {
                    networkTrainingInput[i][j] = this.networkInput[i][j];
                }
                networkTrainingOutput[i][0] = this.networkOutput[i][0];
            }

            // validation set
            double[] solutionValidation        = new double[validationSet];
            double[] inputForValidation        = new double[this.network.InputLayer.NeuronCount];
            double[] inputForValidationNetwork = new double[this.network.InputLayer.NeuronCount];

            // array for saving neural weights and parameters
            this.bestValidationError = double.MaxValue;
            this.bestWeightMatrix    = new double[this.network.Layers.Count - 1][, ];
            this.bestSolution        = new double[n];

            for (int i = 0; i < this.network.Layers.Count - 1; i++)
            {
                this.bestWeightMatrix[i] = new double[this.network.Layers[i].WeightMatrix.Rows, this.network.Layers[i].WeightMatrix.Cols];
            }

            //best network criterion
            double bestNetworkError = double.MaxValue, bestNetworkMSE = double.MaxValue, bestNetworkMAE = double.MaxValue;

            // build array for graph
            this.solutionData    = new double[n];
            this.predictedPoint  = new cPoint[n];
            this.validationPoint = new cPoint[validationSet];

            //initialize point for graph
            predictedDS.Samples     = predictedPoint;
            validationDS.Samples    = validationPoint;
            this.predictedDS.Active = true;


            // prepare training data
            INeuralDataSet dataset;

            if (this.useAdvanceEarlyStopping)
            {
                dataset = new BasicNeuralDataSet(networkTrainingInput, networkTrainingOutput);
            }
            else
            {
                dataset = new BasicNeuralDataSet(this.networkInput, this.networkOutput);
            }


            // initialize trainer
            this.learning = new Backpropagation(this.network, dataset, this.learningRate, this.momentum);


            //training
            while (!needToStop)
            {
                double sse  = 0.0;
                double sae  = 0.0;
                double ssle = 0.0;
                double sspe = 0.0;

                this.learning.Iteration();
                error = learning.Error;


                if (this.useAdvanceEarlyStopping)
                {
                    this.validationDS.Active = true;
                }
                else
                {
                    this.validationDS.Active = false;
                }

                for (int i = 0; i < n; i++)
                {
                    INeuralData neuraldata = new BasicNeuralData(this.networkInput[i]);

                    this.solutionData[i] = (this.network.Compute(neuraldata)[0]
                                            - this.minNormalizedData) / this.factor + this.minData;

                    this.predictedPoint[i].x = i + this.network.InputLayer.NeuronCount;
                    this.predictedPoint[i].y = (float)this.solutionData[i];

                    sse += Math.Pow(this.solutionData[i] - this.data[i + this.network.InputLayer.NeuronCount], 2);
                    sae += Math.Abs(this.solutionData[i] - this.data[i + this.network.InputLayer.NeuronCount]);

                    //calculate advance early stopping
                    if (this.useAdvanceEarlyStopping)
                    {
                        if (i < n - validationSet)
                        {
                            ssle += Math.Pow(this.solutionData[i] - this.data[i + this.network.InputLayer.NeuronCount], 2);
                        }
                        else
                        {
                            // initialize the first validation set input
                            if (i == n - validationSet)
                            {
                                for (int j = 0; j < this.network.InputLayer.NeuronCount; j++)
                                {
                                    inputForValidation[this.network.InputLayer.NeuronCount - 1 - j] = this.data[this.data.Length - (n - i) - 1 - j];
                                }
                            }

                            for (int j = 0; j < this.network.InputLayer.NeuronCount; j++)
                            {
                                inputForValidationNetwork[j] = (inputForValidation[j] - this.minData) * this.factor + this.minNormalizedData;
                            }

                            INeuralData neuraldataval = new BasicNeuralData(inputForValidationNetwork);
                            solutionValidation[i - n + validationSet] = (this.network.Compute(neuraldataval)[0] - this.minNormalizedData) / this.factor + this.minData;

                            this.validationPoint[i - n + validationSet].x = i + this.network.InputLayer.NeuronCount;
                            this.validationPoint[i - n + validationSet].y = (float)solutionValidation[i - n + validationSet];

                            sspe += Math.Pow(this.data[i + this.network.InputLayer.NeuronCount] - solutionValidation[i - n + validationSet], 2);

                            // initialize the next validation set input from the current validation set input
                            for (int j = 0; j < this.network.InputLayer.NeuronCount - 1; j++)
                            {
                                inputForValidation[j] = inputForValidation[j + 1];
                            }

                            inputForValidation[this.network.InputLayer.NeuronCount - 1] = solutionValidation[i - n + validationSet];
                        }
                    }
                }

                mse = sse / this.solutionData.Length;
                mae = sae / this.solutionData.Length;

                //Console.WriteLine(error.ToString());

                //Display it
                this.iterationBox.Text = iteration.ToString();
                this.maeBox.Text       = mae.ToString("F5");
                this.mseBox.Text       = mse.ToString("F5");
                this.errorBox.Text     = error.ToString("F5");


                seriesGraph.Refresh();

                if (this.useAdvanceEarlyStopping)
                {
                    //calculate advance early stopping 2
                    mspe = sspe / validationSet;
                    msle = ssle / (this.solutionData.Length - validationSet);

                    //save best weight
                    if (this.bestValidationError > mspe)
                    {
                        this.bestValidationError = mspe;
                        this.bestSolution        = this.solutionData;

                        // weight matrix
                        for (int i = 0; i < this.network.Layers.Count - 1; i++)
                        {
                            for (int j = 0; j < this.network.Layers[i].WeightMatrix.Rows; j++)
                            {
                                for (int k = 0; k < this.network.Layers[i].WeightMatrix.Cols; k++)
                                {
                                    this.bestWeightMatrix[i][j, k] = this.network.Layers[i].WeightMatrix[j, k];
                                }
                            }
                        }

                        bestNetworkError = error;
                        bestNetworkMAE   = mae;
                        bestNetworkMSE   = mse;
                    }
                    //calculate generalization loss &pq
                    generalizationLoss = 100 * (mspe / this.bestValidationError - 1);

                    trainingErrors[(iteration - 1) % this.strip] = msle;
                    double minStripTrainingError = double.MaxValue, sumStripTrainingError = 0.0;
                    for (int i = 0; i < this.strip; i++)
                    {
                        sumStripTrainingError += trainingErrors[i];
                        if (trainingErrors[i] < minStripTrainingError)
                        {
                            minStripTrainingError = trainingErrors[i];
                        }
                    }
                    double trainingProgress = 1000 * ((sumStripTrainingError / (this.strip * minStripTrainingError)) - 1);
                    pq = generalizationLoss / trainingProgress;



                    //display advance early stopping
                    this.learningErrorBox.Text      = msle.ToString("F5");
                    this.validationErrorBox.Text    = mspe.ToString("F5");
                    this.generalizationLossBox.Text = generalizationLoss.ToString("F5");
                    this.pqBox.Text = pq.ToString("F5");
                    this.seriesGraph.Refresh();

                    //stopping
                    switch (this.advanceStoppingMethod)
                    {
                    case AdvanceStoppingMethodEnumeration.GeneralizationLoss:
                        if (generalizationLoss > this.generalizationLossTreshold)
                        {
                            needToStop = true;
                        }
                        break;

                    case AdvanceStoppingMethodEnumeration.ProgressQuotient:
                        if (pq > this.pqTreshold)
                        {
                            needToStop = true;
                        }
                        break;
                    }
                }

                if (this.withCheckingCycle && iteration % this.checkingCycle == 0)
                {
                    switch (this.checkingMethod)
                    {
                    case CheckingMethodEnumeration.byMSEValue:
                        if (mse <= this.byMSEValueStopping)
                        {
                            needToStop = true;
                        }
                        break;

                    case CheckingMethodEnumeration.byMSEChange:
                        if (lastMSE - mse <= this.byMSEChangeStopping)
                        {
                            needToStop = true;
                        }
                        break;
                    }
                    lastMSE = mse;
                }
                if (iteration >= this.maxIteration)
                {
                    needToStop = true;
                }

                iteration++;
            }

            //restore weight
            if (this.useAdvanceEarlyStopping)
            {
                this.solutionData = this.bestSolution;

                // weight matrix

                for (int i = 0; i < this.network.Layers.Count - 1; i++)
                {
                    for (int j = 0; j < this.network.Layers[i].WeightMatrix.Rows; j++)
                    {
                        for (int k = 0; k < this.network.Layers[i].WeightMatrix.Cols; k++)
                        {
                            this.network.Layers[i].WeightMatrix[j, k] = this.bestWeightMatrix[i][j, k];
                        }
                    }
                }

                //best network criterion
                this.error = bestNetworkError;
                this.mse   = bestNetworkMSE;
                this.mae   = bestNetworkMAE;
            }
            else
            {
                this.error = error;
                this.mse   = mse;
                this.mae   = mae;
            }

            this.enableControls(true);
        }