//************************************************************************************** protected override void LoadModel() { lock (this) { if (Model is null) { Model = (weka.classifiers.functions.LinearRegression)weka.core.SerializationHelper.read(ModelFilename); coefficients = Model.coefficients(); } } }
//************************************************************************************** /// <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); } }