Пример #1
0
        public override void Run(GraphController ctrl)
        {
            ctrl.Doc.RootLayer.IsValidIndex(ctrl.CurrentLayerNumber, out var activeLayer);

            if (!(activeLayer is XYPlotLayer))
            {
                return;
            }

            FunctionEvaluationScript script = null; //

            if (script == null)
            {
                script = new FunctionEvaluationScript();
            }

            object[] args = new object[] { script, new ScriptExecutionHandler(EhScriptExecution) };
            if (Current.Gui.ShowDialog(args, "Function script"))
            {
                ctrl.EnsureValidityOfCurrentLayerNumber();

                script = (FunctionEvaluationScript)args[0];
                var functItem = new XYFunctionPlotItem(new XYFunctionPlotData(script), new G2DPlotStyleCollection(LineScatterPlotStyleKind.Line, activeLayer.GetPropertyContext()));
                ((XYPlotLayer)activeLayer).PlotItems.Add(functItem);
            }
        }
Пример #2
0
        public void OnAfterFittingStep()
        {
            if (_view != null)
            {
                _view.SetChiSquare(this._chiSquare);
            }



            if (_doc.FitContext is Altaxo.Graph.GUI.GraphController)
            {
                // for every dependent variable in the FitEnsemble, create a function graph
                Altaxo.Graph.GUI.GraphController graph = _doc.FitContext as Altaxo.Graph.GUI.GraphController;

                int funcNumber = 0;
                for (int i = 0; i < _doc.FitEnsemble.Count; i++)
                {
                    FitElement fitEle = _doc.FitEnsemble[i];

                    for (int k = 0; k < fitEle.NumberOfDependentVariables; k++, funcNumber++)
                    {
                        if (funcNumber < _functionPlotItems.Count && _functionPlotItems[funcNumber] != null)
                        {
                            XYFunctionPlotItem plotItem = (XYFunctionPlotItem)_functionPlotItems[funcNumber];
                            FitFunctionToScalarFunctionDDWrapper wrapper = (FitFunctionToScalarFunctionDDWrapper)plotItem.Data.Function;
                            wrapper.Initialize(fitEle.FitFunction, k, 0, _doc.GetParametersForFitElement(i));
                        }
                        else
                        {
                            FitFunctionToScalarFunctionDDWrapper wrapper = new FitFunctionToScalarFunctionDDWrapper(fitEle.FitFunction, k, _doc.GetParametersForFitElement(i));
                            XYFunctionPlotData plotdata = new XYFunctionPlotData(wrapper);
                            XYFunctionPlotItem plotItem = new XYFunctionPlotItem(plotdata, new G2DPlotStyleCollection(LineScatterPlotStyleKind.Line));
                            graph.ActiveLayer.PlotItems.Add(plotItem);
                            _functionPlotItems.Add(plotItem);
                        }
                    }
                }

                // if there are more elements in _functionPlotItems, remove them from the graph
                for (int i = _functionPlotItems.Count - 1; i >= funcNumber; --i)
                {
                    if (_functionPlotItems[i] != null)
                    {
                        graph.ActiveLayer.PlotItems.Remove((IGPlotItem)_functionPlotItems[i]);
                        _functionPlotItems.RemoveAt(i);
                    }
                }
                graph.RefreshGraph();
            }
        }
Пример #3
0
        public override void Run(Altaxo.Graph.GUI.GraphController ctrl)
        {
            FunctionEvaluationScript script = null; //

            if (script == null)
            {
                script = new FunctionEvaluationScript();
            }

            object[] args = new object[] { script, new ScriptExecutionHandler(this.EhScriptExecution) };
            if (Current.Gui.ShowDialog(args, "Function script"))
            {
                ctrl.EnsureValidityOfCurrentLayerNumber();

                script = (FunctionEvaluationScript)args[0];
                XYFunctionPlotItem functItem = new XYFunctionPlotItem(new XYFunctionPlotData(script), new G2DPlotStyleCollection(LineScatterPlotStyleKind.Line));
                ctrl.Doc.Layers[ctrl.CurrentLayerNumber].PlotItems.Add(functItem);
            }
        }
Пример #4
0
        public static string Fit(Altaxo.Gui.Graph.Gdi.Viewing.IGraphController ctrl, int order, double fitCurveXmin, double fitCurveXmax, bool showFormulaOnGraph)
        {
            string error;

            error = GetActivePlotPoints(ctrl, out var xarr, out var yarr);
            int numberOfDataPoints = xarr.Length;

            if (null != error)
            {
                return(error);
            }

            string[] plotNames = GetActivePlotName(ctrl);

            int numberOfParameter = order + 1;

            double[] parameter = new double[numberOfParameter];

            var fit = LinearFitBySvd.FitPolymomialDestructive(order, xarr, yarr, null, numberOfDataPoints);

            // Output of results

            Current.Console.WriteLine("");
            Current.Console.WriteLine("---- " + DateTime.Now.ToString() + " -----------------------");
            Current.Console.WriteLine("Polynomial regression of order {0} of {1} over {2}", order, plotNames[1], plotNames[0]);

            Current.Console.WriteLine(
                "Name           Value               Error               F-Value             Prob>F");

            for (int i = 0; i < fit.Parameter.Length; i++)
            {
                Current.Console.WriteLine("A{0,-3} {1,20} {2,20} {3,20} {4,20}",
                                          i,
                                          fit.Parameter[i],
                                          fit.StandardErrorOfParameter(i),
                                          fit.TofParameter(i),
                                          1 - FDistribution.CDF(fit.TofParameter(i), numberOfParameter, numberOfDataPoints - 1)
                                          );
            }

            Current.Console.WriteLine("R²: {0}, Adjusted R²: {1}",
                                      fit.RSquared,
                                      fit.AdjustedRSquared);

            Current.Console.WriteLine("Condition number: {0}, Loss of precision (digits): {1}", fit.ConditionNumber, Math.Log10(fit.ConditionNumber));

            Current.Console.WriteLine("------------------------------------------------------------");
            Current.Console.WriteLine("Source of  Degrees of");
            Current.Console.WriteLine("variation  freedom          Sum of Squares          Mean Square          F0                   P value");

            double regressionmeansquare = fit.RegressionCorrectedSumOfSquares / numberOfParameter;
            double residualmeansquare   = fit.ResidualSumOfSquares / (numberOfDataPoints - numberOfParameter - 1);

            Current.Console.WriteLine("Regression {0,10} {1,20} {2,20} {3,20} {4,20}",
                                      numberOfParameter,
                                      fit.RegressionCorrectedSumOfSquares,
                                      fit.RegressionCorrectedSumOfSquares / numberOfParameter,
                                      regressionmeansquare / residualmeansquare,
                                      1 - FDistribution.CDF(regressionmeansquare / residualmeansquare, numberOfParameter, numberOfDataPoints - 1)
                                      );

            Current.Console.WriteLine("Residual   {0,10} {1,20} {2,20}",
                                      numberOfDataPoints - 1 - numberOfParameter,
                                      fit.ResidualSumOfSquares,
                                      residualmeansquare
                                      );

            Current.Console.WriteLine("Total      {0,10} {1,20}",
                                      numberOfDataPoints - 1,
                                      fit.TotalCorrectedSumOfSquares

                                      );

            Current.Console.WriteLine("------------------------------------------------------------");

            // add the fit curve to the graph
            IScalarFunctionDD plotfunction = new PolynomialFunction(fit.Parameter);
            var fittedCurve = new XYFunctionPlotItem(new XYFunctionPlotData(plotfunction), new G2DPlotStyleCollection(LineScatterPlotStyleKind.Line, ctrl.Doc.GetPropertyContext()));

            var xylayer = ctrl.ActiveLayer as XYPlotLayer;

            if (null != xylayer)
            {
                xylayer.PlotItems.Add(fittedCurve);
            }

            return(null);
        }
Пример #5
0
        public static string Fit(Altaxo.Graph.GUI.GraphController ctrl, int order, double fitCurveXmin, double fitCurveXmax, bool showFormulaOnGraph)
        {
            string error;

            int numberOfDataPoints;

            double[] xarr = null, yarr = null, earr = null;
            error = GetActivePlotPoints(ctrl, ref xarr, ref yarr, out numberOfDataPoints);

            if (null != error)
            {
                return(error);
            }

            string[] plotNames = GetActivePlotName(ctrl);


            // Error-Array
            earr = new double[numberOfDataPoints];
            for (int i = 0; i < earr.Length; i++)
            {
                earr[i] = 1;
            }

            int numberOfParameter = order + 1;

            double[]       parameter = new double[numberOfParameter];
            LinearFitBySvd fit       =
                new LinearFitBySvd(
                    xarr, yarr, earr, numberOfDataPoints, order + 1, new FunctionBaseEvaluator(EvaluatePolynomialBase), 1E-5);

            // Output of results

            Current.Console.WriteLine("");
            Current.Console.WriteLine("---- " + DateTime.Now.ToString() + " -----------------------");
            Current.Console.WriteLine("Polynomial regression of order {0} of {1} over {2}", order, plotNames[1], plotNames[0]);

            Current.Console.WriteLine(
                "Name           Value               Error               F-Value             Prob>F");

            for (int i = 0; i < fit.Parameter.Length; i++)
            {
                Current.Console.WriteLine("A{0,-3} {1,20} {2,20} {3,20} {4,20}",
                                          i,
                                          fit.Parameter[i],
                                          fit.StandardErrorOfParameter(i),
                                          fit.TofParameter(i),
                                          1 - FDistribution.CDF(fit.TofParameter(i), numberOfParameter, numberOfDataPoints - 1)
                                          );
            }

            Current.Console.WriteLine("R²: {0}, Adjusted R²: {1}",
                                      fit.RSquared,
                                      fit.AdjustedRSquared);

            Current.Console.WriteLine("------------------------------------------------------------");
            Current.Console.WriteLine("Source of  Degrees of");
            Current.Console.WriteLine("variation  freedom          Sum of Squares          Mean Square          F0                   P value");

            double regressionmeansquare = fit.RegressionCorrectedSumOfSquares / numberOfParameter;
            double residualmeansquare   = fit.ResidualSumOfSquares / (numberOfDataPoints - numberOfParameter - 1);

            Current.Console.WriteLine("Regression {0,10} {1,20} {2,20} {3,20} {4,20}",
                                      numberOfParameter,
                                      fit.RegressionCorrectedSumOfSquares,
                                      fit.RegressionCorrectedSumOfSquares / numberOfParameter,
                                      regressionmeansquare / residualmeansquare,
                                      1 - FDistribution.CDF(regressionmeansquare / residualmeansquare, numberOfParameter, numberOfDataPoints - 1)
                                      );

            Current.Console.WriteLine("Residual   {0,10} {1,20} {2,20}",
                                      numberOfDataPoints - 1 - numberOfParameter,
                                      fit.ResidualSumOfSquares,
                                      residualmeansquare
                                      );


            Current.Console.WriteLine("Total      {0,10} {1,20}",
                                      numberOfDataPoints - 1,
                                      fit.TotalCorrectedSumOfSquares

                                      );

            Current.Console.WriteLine("------------------------------------------------------------");


            // add the fit curve to the graph
            IScalarFunctionDD  plotfunction = new PolynomialFunction(fit.Parameter);
            XYFunctionPlotItem fittedCurve  = new XYFunctionPlotItem(new XYFunctionPlotData(plotfunction), new G2DPlotStyleCollection(LineScatterPlotStyleKind.Line));

            ctrl.ActiveLayer.PlotItems.Add(fittedCurve);

            return(null);
        }