Ejemplo n.º 1
0
    //**************************************************************************************

    public static WekaLinearRegression CreateNew(int iParamtersID, List <ClassifierParameter> iParameters, Candlestick.Period iPeriod, int iProfitTime, List <int> iTrainingPoints, bool iEliminateColinearAttributes, bool iMinimal)
    {
        // Create WekaInfo and retrive ID
        var info = new WekaInfo(null, typeof(WekaJ48), iParamtersID, iPeriod, iProfitTime);

        WekaInfo.InsertToDB(info);

        if (info.ID is null)
        {
            throw new Exception("Could not deduct ID");
        }

        // Create LinearRegressionInfo
        var lrInfo = new WekaLinearRegressionInfo((int)info.ID, iEliminateColinearAttributes, iMinimal);

        WekaLinearRegressionInfo.UpdateDB(lrInfo);

        // Save training points
        if (iTrainingPoints != null)
        {
            SaveTrainingPoints(iTrainingPoints, "Data/TrainingPoints/Classifier_" + info.ID.ToString() + ".dat");
        }

        // Create classifier
        return(new WekaLinearRegression(info, iParameters, lrInfo));
    }
Ejemplo n.º 2
0
    //**************************************************************************************

    public override void RebuildClassifier()
    {
        OutputMessage("Rebuilding classifier");

        // Remove model
        DeleteModel(ID);

        // Remove training data
        DeleteTrainingPoints(ID);

        // Update info
        LRInfo.OnClassifierRebuild();
        WekaLinearRegressionInfo.UpdateDB(LRInfo);
    }
Ejemplo n.º 3
0
    //**************************************************************************************

    /// <summary>
    ///	Build cllasifier model and save it to a file.
    /// </summary>
    public override void Build(CandlestickCollection iCandlestick)
    {
        List <int> trainingPoints = null;

        // Build model
        if (!File.Exists(ModelFilename))
        {
            OutputMessage("Building model");
            trainingPoints = LoadTrainingPoints(iCandlestick, ID, ProfitTime);

            Model = new weka.classifiers.functions.LinearRegression();
            Model.setEliminateColinearAttributes(LRInfo.EliminateColinearAttributes);
            Model.setMinimal(LRInfo.Minimal);
            Model.buildClassifier(CreateInstances(iCandlestick, trainingPoints, Attributes, Parameters, Period, ProfitTime));
            weka.core.SerializationHelper.write(ModelFilename, Model);
            coefficients = Model.coefficients();
        }

        // Calculate coeficient of determination and profits std dev
        if (LRInfo.CoefOfDetermination == null || LRInfo.ProfitStdDev == null)
        {
            WaitTillModelReady();
            OutputMessage("Calculating coeficient of determination and profits std dev");

            if (trainingPoints is null)
            {
                trainingPoints = LoadTrainingPoints(iCandlestick, ID, ProfitTime);
            }

            var par           = CalculateParameters(Parameters, iCandlestick, trainingPoints, Period);
            var futureProfits = FullToTraining(new List <float>(CalculateFutureProfits(iCandlestick[kTrainingPeriod], ProfitTime)), trainingPoints).ToArray();
            var pred          = new List <float>();

            for (int j = 0; j < par.Count; j++)
            {
                var fp = PredictDFP(par[j]);

                var targetPriceChange = (float)Math.Exp(fp);
                pred.Add(targetPriceChange - 1.0f);
            }

            LRInfo.CoefOfDetermination = (float)Math.Pow(Statistics.CalculateCorrelation(futureProfits, pred.ToArray()), 2.0f);
            LRInfo.ProfitStdDev        = Statistics.StandardDeviation(futureProfits, pred.ToArray());
            WekaLinearRegressionInfo.UpdateDB(LRInfo);
        }
    }