private static ISymbolicRegressionSolution CreateSymbolicSolution(List <IRegressionModel> models, double nu, IRegressionProblemData problemData) { var symbModels = models.OfType <ISymbolicRegressionModel>(); var lowerLimit = symbModels.Min(m => m.LowerEstimationLimit); var upperLimit = symbModels.Max(m => m.UpperEstimationLimit); var interpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter(); var progRootNode = new ProgramRootSymbol().CreateTreeNode(); var startNode = new StartSymbol().CreateTreeNode(); var addNode = new Addition().CreateTreeNode(); var mulNode = new Multiplication().CreateTreeNode(); var scaleNode = (ConstantTreeNode) new Constant().CreateTreeNode(); // all models are scaled using the same nu scaleNode.Value = nu; foreach (var m in symbModels) { var relevantPart = m.SymbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0); // skip root and start addNode.AddSubtree((ISymbolicExpressionTreeNode)relevantPart.Clone()); } mulNode.AddSubtree(addNode); mulNode.AddSubtree(scaleNode); startNode.AddSubtree(mulNode); progRootNode.AddSubtree(startNode); var t = new SymbolicExpressionTree(progRootNode); var combinedModel = new SymbolicRegressionModel(problemData.TargetVariable, t, interpreter, lowerLimit, upperLimit); var sol = new SymbolicRegressionSolution(combinedModel, problemData); return(sol); }
protected override void UpdateModel(ISymbolicExpressionTree tree) { var model = new SymbolicRegressionModel(Content.ProblemData.TargetVariable, tree, Content.Model.Interpreter, Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit); model.Scale(Content.ProblemData); Content.Model = model; }
private IContent CreateModel(int idx) { idx -= 1; var rfModel = Content.Model as RandomForestModel; if (rfModel == null) { return(null); } var regProblemData = Content.ProblemData as IRegressionProblemData; var classProblemData = Content.ProblemData as IClassificationProblemData; if (idx < 0 || idx >= rfModel.NumberOfTrees) { return(null); } if (regProblemData != null) { var syModel = new SymbolicRegressionModel(regProblemData.TargetVariable, rfModel.ExtractTree(idx), new SymbolicDataAnalysisExpressionTreeLinearInterpreter()); return(syModel.CreateRegressionSolution(regProblemData)); } else if (classProblemData != null) { var syModel = new SymbolicDiscriminantFunctionClassificationModel(classProblemData.TargetVariable, rfModel.ExtractTree(idx), new SymbolicDataAnalysisExpressionTreeLinearInterpreter(), new NormalDistributionCutPointsThresholdCalculator()); syModel.RecalculateModelParameters(classProblemData, classProblemData.TrainingIndices); return(syModel.CreateClassificationSolution(classProblemData)); } else { throw new InvalidProgramException(); } }
public static ISymbolicRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData, string modelStructure, int maxIterations) { var parser = new InfixExpressionParser(); var tree = parser.Parse(modelStructure); var simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier(); if (!SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(tree)) { throw new ArgumentException("The optimizer does not support the specified model structure."); } var interpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter(); SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, tree, problemData, problemData.TrainingIndices, applyLinearScaling: false, maxIterations: maxIterations, updateVariableWeights: false, updateConstantsInTree: true); var scaledModel = new SymbolicRegressionModel(problemData.TargetVariable, tree, (ISymbolicDataAnalysisExpressionTreeInterpreter)interpreter.Clone()); scaledModel.Scale(problemData); SymbolicRegressionSolution solution = new SymbolicRegressionSolution(scaledModel, (IRegressionProblemData)problemData.Clone()); solution.Model.Name = "Regression Model"; solution.Name = "Regression Solution"; return(solution); }
public void CustomModelVariableImpactTest() { IRegressionProblemData problemData = CreateDefaultProblem(); ISymbolicExpressionTree tree = CreateCustomExpressionTree(); IRegressionModel model = new SymbolicRegressionModel(problemData.TargetVariable, tree, new SymbolicDataAnalysisExpressionTreeInterpreter()); IRegressionSolution solution = new RegressionSolution(model, (IRegressionProblemData)problemData.Clone()); Dictionary <string, double> expectedImpacts = GetExpectedValuesForCustomProblem(); CheckDefaultAsserts(solution, expectedImpacts); }
protected override void Run(CancellationToken cancellationToken) { var interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter(); var modelsWithComplexity = new List <(ISymbolicRegressionModel, int)>(); Stopwatch stopwatch = Stopwatch.StartNew(); RunFFX(out var basisFunctions, out var lambda, out var coeff, out var trainNMSE, out var testNMSE, out var intercept, out var elnetData); stopwatch.Stop(); var runtime = stopwatch.ElapsedTicks / 10000000.0; // in seconds if (Verbose) { var errorTable = NMSEGraph(coeff, lambda, trainNMSE, testNMSE); Results.Add(new Result(errorTable.Name, errorTable.Description, errorTable)); var coeffTable = CoefficientGraph(coeff, lambda, elnetData.AllowedInputVariables, elnetData.Dataset); Results.Add(new Result(coeffTable.Name, coeffTable.Description, coeffTable)); } int complexity(double[] modelCoeffs) => modelCoeffs.Count(val => val != 0) + 1; for (int row = 0; row < coeff.GetUpperBound(0); row++) { var coeffs = Utils.GetRow(coeff, row); var numBasisFuncs = complexity(coeffs); ISymbolicExpressionTree tree = Tree(basisFunctions, coeffs, intercept[row]); ISymbolicRegressionModel model = new SymbolicRegressionModel(elnetData.TargetVariable, tree, interpreter); modelsWithComplexity.Add((model, numBasisFuncs)); } // calculate the pareto front var paretoFront = Utils.NondominatedFilter(modelsWithComplexity.ToArray(), coeff, testNMSE, complexity); modelsWithComplexity = modelsWithComplexity.Distinct(new Utils.SymbolicRegressionModelSameComplexity()).ToList(); var results = new ItemCollection <IResult>(); int modelIdx = 1; foreach (var model in modelsWithComplexity) { results.Add(new Result("Model " + (modelIdx < 10 ? "0" + modelIdx : modelIdx.ToString()), model.Item1)); //results.Add(new ItemCollection<IResult>(3){ //new Result("Model Complexity", new IntValue(model.Item2)), //new Result("Model Accuracy", new RegressionSolution(model.Item1, Problem.ProblemData)) //}); modelIdx++; } Results.Add(new Result("Pareto Front", "The Pareto Front of the Models. ", results)); if (FilePath != "") { SaveInFile(paretoFront, runtime, FilePath, ","); } }
private IContent CreateModel(int idx) { idx -= 1; var rfModel = Content; var rfClassModel = rfModel as IClassificationModel; // rfModel is always a IRegressionModel and a IClassificationModel var targetVariable = rfClassModel.TargetVariable; if (rfModel == null) { return(null); } if (idx < 0 || idx >= rfModel.NumberOfTrees) { return(null); } var syModel = new SymbolicRegressionModel(targetVariable, rfModel.ExtractTree(idx), new SymbolicDataAnalysisExpressionTreeLinearInterpreter()); return(syModel); }
/// <summary> /// Fits a model to the data by optimizing the numeric constants. /// Model is specified as infix expression containing variable names and numbers. /// The starting point for the numeric constants is initialized randomly if a random number generator is specified (~N(0,1)). Otherwise the user specified constants are /// used as a starting point. /// </summary>- /// <param name="problemData">Training and test data</param> /// <param name="modelStructure">The function as infix expression</param> /// <param name="maxIterations">Number of constant optimization iterations (using Levenberg-Marquardt algorithm)</param> /// <param name="random">Optional random number generator for random initialization of numeric constants.</param> /// <returns></returns> public static ISymbolicRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData, string modelStructure, int maxIterations, bool applyLinearScaling, IRandom rand = null) { var parser = new InfixExpressionParser(); var tree = parser.Parse(modelStructure); // parser handles double and string variables equally by creating a VariableTreeNode // post-process to replace VariableTreeNodes by FactorVariableTreeNodes for all string variables var factorSymbol = new FactorVariable(); factorSymbol.VariableNames = problemData.AllowedInputVariables.Where(name => problemData.Dataset.VariableHasType <string>(name)); factorSymbol.AllVariableNames = factorSymbol.VariableNames; factorSymbol.VariableValues = factorSymbol.VariableNames.Select(name => new KeyValuePair <string, Dictionary <string, int> >(name, problemData.Dataset.GetReadOnlyStringValues(name).Distinct() .Select((n, i) => Tuple.Create(n, i)) .ToDictionary(tup => tup.Item1, tup => tup.Item2))); foreach (var parent in tree.IterateNodesPrefix().ToArray()) { for (int i = 0; i < parent.SubtreeCount; i++) { var varChild = parent.GetSubtree(i) as VariableTreeNode; var factorVarChild = parent.GetSubtree(i) as FactorVariableTreeNode; if (varChild != null && factorSymbol.VariableNames.Contains(varChild.VariableName)) { parent.RemoveSubtree(i); var factorTreeNode = (FactorVariableTreeNode)factorSymbol.CreateTreeNode(); factorTreeNode.VariableName = varChild.VariableName; factorTreeNode.Weights = factorTreeNode.Symbol.GetVariableValues(factorTreeNode.VariableName).Select(_ => 1.0).ToArray(); // weight = 1.0 for each value parent.InsertSubtree(i, factorTreeNode); } else if (factorVarChild != null && factorSymbol.VariableNames.Contains(factorVarChild.VariableName)) { if (factorSymbol.GetVariableValues(factorVarChild.VariableName).Count() != factorVarChild.Weights.Length) { throw new ArgumentException( string.Format("Factor variable {0} needs exactly {1} weights", factorVarChild.VariableName, factorSymbol.GetVariableValues(factorVarChild.VariableName).Count())); } parent.RemoveSubtree(i); var factorTreeNode = (FactorVariableTreeNode)factorSymbol.CreateTreeNode(); factorTreeNode.VariableName = factorVarChild.VariableName; factorTreeNode.Weights = factorVarChild.Weights; parent.InsertSubtree(i, factorTreeNode); } } } if (!SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(tree)) { throw new ArgumentException("The optimizer does not support the specified model structure."); } // initialize constants randomly if (rand != null) { foreach (var node in tree.IterateNodesPrefix().OfType <ConstantTreeNode>()) { double f = Math.Exp(NormalDistributedRandom.NextDouble(rand, 0, 1)); double s = rand.NextDouble() < 0.5 ? -1 : 1; node.Value = s * node.Value * f; } } var interpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter(); SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, tree, problemData, problemData.TrainingIndices, applyLinearScaling: applyLinearScaling, maxIterations: maxIterations, updateVariableWeights: false, updateConstantsInTree: true); var model = new SymbolicRegressionModel(problemData.TargetVariable, tree, (ISymbolicDataAnalysisExpressionTreeInterpreter)interpreter.Clone()); if (applyLinearScaling) { model.Scale(problemData); } SymbolicRegressionSolution solution = new SymbolicRegressionSolution(model, (IRegressionProblemData)problemData.Clone()); solution.Model.Name = "Regression Model"; solution.Name = "Regression Solution"; return(solution); }