Ejemplo n.º 1
0
        public static string OutputFitResults(LinearFitBySvd fit, string[] paramNames)
        {
            // Output of results

            Current.Console.WriteLine("");
            Current.Console.WriteLine("---- " + DateTime.Now.ToString() + " -----------------------");
            Current.Console.WriteLine("Multivariate regression of order {0}", fit.NumberOfParameter);

            Current.Console.WriteLine("{0,-15} {1,20} {2,20} {3,20} {4,20}",
                                      "Name", "Value", "Error", "F-Value", "Prob>F");

            for (int i = 0; i < fit.Parameter.Length; i++)
            {
                Current.Console.WriteLine("{0,-15} {1,20} {2,20} {3,20} {4,20}",
                                          paramNames == null ? string.Format("A{0}", i) : paramNames[i],
                                          fit.Parameter[i],
                                          fit.StandardErrorOfParameter(i),
                                          fit.TofParameter(i),
                                          1 - FDistribution.CDF(fit.TofParameter(i), fit.NumberOfParameter, fit.NumberOfData - 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 / fit.NumberOfParameter;
            double residualmeansquare   = fit.ResidualSumOfSquares / (fit.NumberOfData - fit.NumberOfParameter - 1);

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

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


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

                                      );

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


            return(null);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
 public void TestFDistributionCDF()
 {
     // N[CDF[FRatioDistribution[3, 4], 1 - 1/20] , 25]
     Assert.AreEqual(0.5034356763953920149093067, FDistribution.CDF(0.95, 3, 4), 1E-14);
 }