public void ConstructorTest()
        {
            var F = new FDistribution(degrees1: 8, degrees2: 5);

            double mean   = F.Mean;                                     // 1.6666666666666667
            double median = F.Median;                                   // 1.0545096252132447
            double var    = F.Variance;                                 // 7.6388888888888893

            double cdf  = F.DistributionFunction(x: 0.27);              // 0.049463408057268315
            double pdf  = F.ProbabilityDensityFunction(x: 0.27);        // 0.45120469723580559
            double lpdf = F.LogProbabilityDensityFunction(x: 0.27);     // -0.79583416831212883

            double ccdf = F.ComplementaryDistributionFunction(x: 0.27); // 0.95053659194273166
            double icdf = F.InverseDistributionFunction(p: cdf);        // 0.27

            double hf  = F.HazardFunction(x: 0.27);                     // 0.47468419528555084
            double chf = F.CumulativeHazardFunction(x: 0.27);           // 0.050728620222091653

            string str = F.ToString(CultureInfo.InvariantCulture);      // F(x; df1 = 8, df2 = 5)

            Assert.AreEqual(1.6666666666666667, mean);
            Assert.AreEqual(1.0545096252132447, median);
            Assert.AreEqual(7.6388888888888893, var);
            Assert.AreEqual(0.050728620222091653, chf);
            Assert.AreEqual(0.049463408057268315, cdf);
            Assert.AreEqual(0.45120469723580559, pdf);
            Assert.AreEqual(-0.79583416831212883, lpdf);
            Assert.AreEqual(0.47468419528555084, hf);
            Assert.AreEqual(0.95053659194273166, ccdf);
            Assert.AreEqual(0.27, icdf);
            Assert.AreEqual("F(x; df1 = 8, df2 = 5)", str);
        }
Example #2
0
        public static double Fisher(double significance_level, int fst_degree, int snd_degree)
        {
            var fd           = new FDistribution(fst_degree, snd_degree);
            var fisher_value = 1 / fd.InverseDistributionFunction(significance_level);

            return(fisher_value);
        }
        /// <summary>
        /// Approximate Wilk's lambda by F statistics
        /// </summary>
        /// <param name="H">Hypothesis Sum of Squares and Cross Products.</param>
        /// <param name="E">Error Sum of Squares and Cross Products</param>
        /// <param name="N">number of data points</param>
        /// <param name="p">number of variables</param>
        /// <param name="g">number of groups</param>
        /// <param name="F_crit">The F critical value</param>
        /// <returns>The p-value</returns>
        public static double GetWilksLambda(double[][] H, double[][] E, int N, int p, int g, out double F_crit)
        {
            double[][] H_plus_E             = Add(H, E);
            double     E_determinant        = MatrixOp.GetDeterminant(E);
            double     H_plus_E_determinant = MatrixOp.GetDeterminant(H_plus_E);
            double     lambda = E_determinant / H_plus_E_determinant;

            double a           = N - g - (p - g + 2) / 2.0;
            int    b_threshold = p * p + (g - 1) * (g - 1) - 5;
            double b           = 1;

            if (b_threshold > 0)
            {
                b = System.Math.Sqrt((p * p * (g - 1) - 4) / b_threshold);
            }

            double c = (p * (g - 1) - 2) / 2.0;

            F_crit = ((1 - System.Math.Pow(lambda, 1 / b)) / (System.Math.Pow(lambda, 1 / b))) * ((a * b - c) / (p * (g - 1)));

            double DF1    = p * (g - 1);
            double DF2    = a * b - c;
            double pValue = 1 - FDistribution.GetPercentile(F_crit, DF1, DF2);

            return(pValue);
        }
Example #4
0
        public void MeanVarianceTest()
        {
            int[]    d    = { 1, 2, 3, 4, 5 };
            double[] mean = { double.NaN, double.NaN, 3.0000, 2.0000, 1.6667 };
            double[] var  = { double.NaN, double.NaN, double.NaN, double.NaN, 8.8889 };

            for (int i = 0; i < 5; i++)
            {
                FDistribution f = new FDistribution(d[i], d[i]);
                if (double.IsNaN(mean[i]))
                {
                    Assert.IsTrue(double.IsNaN(f.Mean));
                }
                else
                {
                    Assert.AreEqual(mean[i], f.Mean, 1e-4);
                }


                if (double.IsNaN(var[i]))
                {
                    Assert.IsTrue(double.IsNaN(f.Variance));
                }
                else
                {
                    Assert.AreEqual(var[i], f.Variance, 1e-4);
                }
            }
        }
Example #5
0
        protected override void EndProcessing()
        {
            var dist = new FDistribution(Degrees1, Degrees2);
            var obj  = DistributionHelper.AddConvinienceMethods(dist);

            WriteObject(obj);
        }
Example #6
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);
        }
Example #7
0
        /// <summary>
        ///   Creates a new F-Test for a given statistic
        ///   with given degrees of freedom.
        /// </summary>
        ///
        /// <param name="statistic">The test statistic.</param>
        /// <param name="d1">The degrees of freedom for the numerator.</param>
        /// <param name="d2">The degrees of freedom for the denominator.</param>
        ///
        public FTest(double statistic, int d1, int d2)
            : base(statistic)
        {
            base.Hypothesis = Hypothesis.OneUpper;

            StatisticDistribution = new FDistribution(d1, d2);
            PValue = 1.0 - StatisticDistribution.DistributionFunction(statistic);
        }
Example #8
0
        public void LogProbabilityDistributionFunctionTest()
        {
            FDistribution f = new FDistribution(2, 3);

            double expected = System.Math.Log(0.487139289628747);
            double actual   = f.LogProbabilityDensityFunction(0.5);

            Assert.AreEqual(expected, actual, 1e-6);
        }
Example #9
0
        /// <summary>
        /// hypothesis testing for more than two classes using ANOVA
        ///
        /// Given that:
        /// H_0 : mu_1 = mu_2 = ... mu_k (where k is the number of classes)
        /// H_A : mu != null_value
        ///
        /// p-value = Pr(> f) is the probability of at least as large a ratio between the "between" and "within" group variablity if in fact the means of all groups are equal
        /// if(p-value < significance_level) reject H_0
        /// </summary>
        /// <param name="groupedSample">The sample groupped based on the classes</param>
        /// <param name="pValue"></param>
        /// <param name="significance_level">p-value = Pr(> f) is the probability of at least as large a ratio between the "between" and "within" group variablity if in fact the means of all groups are equal</param>
        /// <returns>True if H_0 is rejected; False if H_0 is failed to be rejected</returns>
        public static bool RunANOVA(double[] totalSample, int[] grpCat, out ANOVA output, double significance_level = 0.05)
        {
            output = new ANOVA();

            Dictionary <int, List <double> > groupedSample = new Dictionary <int, List <double> >();

            for (int i = 0; i < totalSample.Length; ++i)
            {
                int           grpId     = grpCat[i];
                double        sampleVal = totalSample[i];
                List <double> grp       = null;
                if (groupedSample.ContainsKey(grpId))
                {
                    grp = groupedSample[grpId];
                }
                else
                {
                    grp = new List <double>();
                    groupedSample[grpId] = grp;
                }
                grp.Add(sampleVal);
            }
            double grand_mean;

            //Sum of squares measures the total variablity
            output.SST = GetSST(totalSample, out grand_mean); //sum of squares total
            output.SSG = GetSSG(groupedSample, grand_mean);   //sum of squares group, which is known as explained variablity (explained by the group variable)
            output.SSE = output.SST - output.SSG;             //sum of squares error, which is known as unexplained variability (unexplained by the group variable, due to other reasons)

            //Degrees of freedom
            output.dfT = totalSample.Length - 1;  //degrees of freedom total
            output.dfG = groupedSample.Count - 1; //degrees of freedom group
            output.dfE = output.dfT - output.dfG; // degrees of freedom error

            //Mean squares measures variability between and within groups, calculated as the total variability (sum of squares) scaled by the associated degrees of freedom
            output.MSG = output.SSG / output.dfG; // mean squares group : between group variability
            output.MSE = output.SSE / output.dfE; // mean squares error : within group variablity

            output.Intercepts = GetIntercepts(GetMeanWithinGroup(groupedSample));

            //f statistic: ratio of the between group variablity and within group variablity
            output.F = output.MSG / output.MSE;

            try
            {
                //p-value = Pr(> f) is the probability of at least as large a ratio between the "between" and "within" group variablity if in fact the means of all groups are equal
                output.pValue = 1 - FDistribution.GetPercentile(output.F, output.dfG, output.dfE);
            }
            catch
            {
            }

            return(output.RejectH0 = output.pValue < significance_level);
        }
Example #10
0
        public void DistributionFunctionTest()
        {
            FDistribution f = new FDistribution(2, 3);

            Assert.AreEqual(f.DegreesOfFreedom1, 2);
            Assert.AreEqual(f.DegreesOfFreedom2, 3);

            double expected = 0.350480947161671;
            double actual   = f.DistributionFunction(0.5);

            Assert.AreEqual(expected, actual, 1e-6);
        }
Example #11
0
        public void ConstructorTest()
        {
            var F = new FDistribution(degrees1: 8, degrees2: 5);

            double mean   = F.Mean;                                     // 1.6666666666666667
            double median = F.Median;                                   // 1.0545096252132447
            double var    = F.Variance;                                 // 7.6388888888888893
            double mode   = F.Mode;                                     // 0.5357142857142857

            double cdf  = F.DistributionFunction(x: 0.27);              // 0.049463408057268315
            double pdf  = F.ProbabilityDensityFunction(x: 0.27);        // 0.45120469723580559
            double lpdf = F.LogProbabilityDensityFunction(x: 0.27);     // -0.79583416831212883

            double ccdf = F.ComplementaryDistributionFunction(x: 0.27); // 0.95053659194273166
            double icdf = F.InverseDistributionFunction(p: cdf);        // 0.27

            double hf  = F.HazardFunction(x: 0.27);                     // 0.47468419528555084
            double chf = F.CumulativeHazardFunction(x: 0.27);           // 0.050728620222091653

            string str = F.ToString(CultureInfo.InvariantCulture);      // F(x; df1 = 8, df2 = 5)

            Assert.AreEqual(0, F.Support.Min);
            Assert.AreEqual(double.PositiveInfinity, F.Support.Max);

            Assert.AreEqual(F.InverseDistributionFunction(0), F.Support.Min);
            Assert.AreEqual(F.InverseDistributionFunction(1), F.Support.Max);

            Assert.AreEqual(1.6666666666666667, mean);
            Assert.AreEqual(1.0545096252132447, median);
            Assert.AreEqual(7.6388888888888893, var);
            Assert.AreEqual(0.5357142857142857, mode);
            Assert.AreEqual(0.050728620222091653, chf);
            Assert.AreEqual(0.049463408057268315, cdf);
            Assert.AreEqual(0.45120469723580559, pdf);
            Assert.AreEqual(-0.79583416831212883, lpdf);
            Assert.AreEqual(0.47468419528555084, hf);
            Assert.AreEqual(0.95053659194273166, ccdf);
            Assert.AreEqual(0.27, icdf);
            Assert.AreEqual("F(x; df1 = 8, df2 = 5)", str);

            var range1 = F.GetRange(0.95);
            var range2 = F.GetRange(0.99);
            var range3 = F.GetRange(0.01);

            Assert.AreEqual(0.27118653875813753, range1.Min);
            Assert.AreEqual(4.8183195356568689, range1.Max);
            Assert.AreEqual(0.15078805233761733, range2.Min);
            Assert.AreEqual(10.289311046135927, range2.Max);
            Assert.AreEqual(0.1507880523376173, range3.Min);
            Assert.AreEqual(10.289311046135927, range3.Max);
        }
Example #12
0
        public void ProbabilityDistributionFunctionTest2()
        {
            FDistribution f = new FDistribution(2, 2);

            Assert.AreEqual(f.ProbabilityDensityFunction(1), 0.2500, 1e-4);
            Assert.AreEqual(f.ProbabilityDensityFunction(2), 0.1111, 1e-4);
            Assert.AreEqual(f.ProbabilityDensityFunction(3), 0.0625, 1e-4);
            Assert.AreEqual(f.ProbabilityDensityFunction(4), 0.0400, 1e-4);
            Assert.AreEqual(f.ProbabilityDensityFunction(5), 0.0278, 1e-4);
            Assert.AreEqual(f.ProbabilityDensityFunction(6), 0.0204, 1e-4);

            Assert.AreEqual(new FDistribution(5, 5).ProbabilityDensityFunction(3), 0.0689, 1e-4);
            Assert.AreEqual(new FDistribution(6, 6).ProbabilityDensityFunction(3), 0.0659, 1e-4);
            Assert.AreEqual(new FDistribution(7, 7).ProbabilityDensityFunction(3), 0.0620, 1e-4);
            Assert.AreEqual(new FDistribution(8, 8).ProbabilityDensityFunction(3), 0.0577, 1e-4);
            Assert.AreEqual(new FDistribution(9, 9).ProbabilityDensityFunction(3), 0.0532, 1e-4);
            Assert.AreEqual(new FDistribution(10, 10).ProbabilityDensityFunction(3), 0.0487, 1e-4);
        }
Example #13
0
        public void InverseDistributionFunctionTest()
        {
            double[] cdf =
            {
                0.231238, 0.404508, 0.605516, 0.86038, 1.20711, 1.71825, 2.5611, 4.23607, 9.24342
            };

            FDistribution target = new FDistribution(4, 2);

            for (int i = 0; i < cdf.Length; i++)
            {
                double x        = (i + 1) / 10.0;
                double actual   = target.InverseDistributionFunction(x);
                double expected = cdf[i];

                Assert.AreEqual(expected, actual, 1e-5);
                Assert.IsFalse(double.IsNaN(actual));
            }
        }
Example #14
0
        public void DistributionFunctionTest3()
        {
            double[] cdf =
            {
                0,        0.0277778, 0.0816327, 0.140625, 0.197531, 0.25,
                0.297521,  0.340278,  0.378698, 0.413265, 0.444444
            };

            FDistribution target = new FDistribution(4, 2);

            for (int i = 0; i < 11; i++)
            {
                double x        = i / 10.0;
                double actual   = target.DistributionFunction(x);
                double expected = cdf[i];

                Assert.AreEqual(expected, actual, 1e-5);
                Assert.IsFalse(double.IsNaN(actual));
            }
        }
Example #15
0
 public void TestFDistribution()
 {
     double[][] para =
     {
         new double[] { 3, 4, 0.4183909951315492730074144, 0.6061355268760950491423940, 6.591382116425581280992118, 0.01221670581017468041546974 }
     };
     for (int i = 0; i < para.Length; i++)
     {
         var tester = new ContDistTester(para[i], delegate(double a, double b)
         {
             var ret = new FDistribution
             {
                 Alpha = (int)a,
                 Beta  = (int)b
             };
             return(ret);
         }
                                         );
         tester.Test(1e-14);
     }
 }
        public void Confirm_BetPrimeDistribution_Relative_to_F_Distribution()
        {
            double alpha = 4.0d;
            double beta  = 6.0d;

            FDistribution fdist = new FDistribution((int)alpha * 2, (int)beta * 2);
            double        fMean = fdist.Mean;
            double        fPdf  = (beta / alpha) * fdist.ProbabilityDensityFunction(4.0d);
            double        fCdf  = fdist.DistributionFunction(4.0d);

            var    betaPrimeDist = new BetaPrimeDistribution(alpha, beta);
            double bpMean        = (beta / alpha) * betaPrimeDist.Mean;
            double bpPdf         = betaPrimeDist.ProbabilityDensityFunction((alpha / beta) * 4.0d);
            double bpCdf         = betaPrimeDist.DistributionFunction((alpha / beta) * 4.0d);

            Assert.AreEqual(fMean, bpMean, 0.00000001, "mean should be equal");
            Assert.AreEqual(fPdf, bpPdf, 0.00000001, "probability density should be equal");
            Assert.AreEqual(fCdf, bpCdf, 0.00000001, "cumulative distribution should be equal");

            //Beta Prime distribution is a scaled version of Pearson Type VI, which itself is scale of F distribution
        }
Example #17
0
        public void ComplementaryDistributionFunctionTest()
        {
            double actual;
            double expected;

            int[]         nu1 = { 1, 2, 3, 4, 5 };
            int[]         nu2 = { 6, 7, 8, 9, 10 };
            double[]      x   = { 2, 3, 4, 5, 6 };
            double[]      cdf = { 0.7930, 0.8854, 0.9481, 0.9788, 0.9919 };
            FDistribution f;

            for (int i = 0; i < 5; i++)
            {
                f        = new FDistribution(nu1[i], nu2[i]);
                expected = cdf[i];

                actual = f.DistributionFunction(x[i]);
                Assert.AreEqual(expected, actual, 1e-4);

                f      = new FDistribution(nu2[i], nu1[i]);
                actual = f.ComplementaryDistributionFunction(1.0 / x[i]);
                Assert.AreEqual(expected, actual, 1e-4);
            }
        }
Example #18
0
        public void LogProbabilityDistributionFunctionTest2()
        {
            FDistribution f = new FDistribution(2, 2);
            double        actual;
            double        expected;
            double        x;

            for (int i = 1; i <= 6; i++)
            {
                x        = i;
                actual   = f.LogProbabilityDensityFunction(x);
                expected = System.Math.Log(f.ProbabilityDensityFunction(x));
                Assert.AreEqual(expected, actual, 1e-10);
            }

            for (int i = 5; i <= 10; i++)
            {
                f        = new FDistribution(i, i);
                x        = 3;
                actual   = f.LogProbabilityDensityFunction(x);
                expected = System.Math.Log(f.ProbabilityDensityFunction(x));
                Assert.AreEqual(expected, actual, 1e-10);
            }
        }
 /// <summary>
 /// Returns a new line chart plotting the specified function of the given distribution for 0.0001 &lt;= p &lt;= 0.9999.
 /// </summary>
 /// <param name="dist">The distribution.</param>
 /// <param name="function">The distribution function to plot.</param>
 /// <param name="numInterpolatedValues">The number of interpolated values.</param>
 /// <returns>A new chart.</returns>
 public static ChartControl ToChart( FDistribution dist, DistributionFunction function = DistributionFunction.PDF, int numInterpolatedValues = 100 )
 {
     ChartControl chart = GetDefaultChart();
       Update( ref chart, dist, function, numInterpolatedValues );
       return chart;
 }
 /// <summary>
 /// Shows a new chart in a default form.
 /// </summary>
 /// <param name="dist">The distribution.</param>
 /// <param name="function">The distribution function to plot.</param>
 /// <param name="numInterpolatedValues">The number of interpolated values.</param>
 /// <remarks>
 /// Equivalent to:
 /// <code>
 /// NMathStatsChart.Show( ToChart( dist, function, numInterpolatedValues ) );
 /// </code>
 /// </remarks>
 public static void Show( FDistribution dist, DistributionFunction function = DistributionFunction.PDF, int numInterpolatedValues = 100 )
 {
     Show( ToChart( dist, function, numInterpolatedValues ) );
 }
Example #21
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);
        }
Example #22
0
        /// <summary>
        /// Suppose the regression is given by y = b * x + intercept[j], where j is the group id (in other words, b is the fixed effect, intercept is the random effect)
        /// Run the ANCOVA which calculates the following:
        ///  1. the slope, b, of y = b * x + intercept[j]
        ///  2. the intercept, of y = b * x + intercept[j]
        /// </summary>
        /// <param name="x">data for the predictor variable</param>
        /// <param name="y">data for the response variable</param>
        /// <param name="grpCat">group id for each (x, y)</param>
        /// <param name="output">the result of ANCOVA</param>
        /// <param name="significance_level">alpha for the hypothesis testing, in which H_0 : y - b * x is independent of group category</param>
        public static void RunANCOVA(double[] x, double[] y, int[] grpCat, out ANCOVA output, double significance_level = 0.05)
        {
            output = new ANCOVA();

            Dictionary <int, List <double> > groupped_x = new Dictionary <int, List <double> >();
            Dictionary <int, List <double> > groupped_y = new Dictionary <int, List <double> >();

            int N = x.Length;

            for (int i = 0; i < N; ++i)
            {
                int    grpId = grpCat[i];
                double xVal  = x[i];
                double yVal  = y[i];

                List <double> group_x = null;
                List <double> group_y = null;

                if (groupped_x.ContainsKey(grpId))
                {
                    group_x = groupped_x[grpId];
                }
                else
                {
                    group_x           = new List <double>();
                    groupped_x[grpId] = group_x;
                }

                if (groupped_y.ContainsKey(grpId))
                {
                    group_y = groupped_y[grpId];
                }
                else
                {
                    group_y           = new List <double>();
                    groupped_y[grpId] = group_y;
                }

                group_x.Add(xVal);
                group_y.Add(yVal);
            }

            double grand_mean_x;
            double grand_mean_y;

            output.SSTx = GetSST(x, out grand_mean_x);
            output.SSTy = GetSST(y, out grand_mean_y);

            output.SSBGx = GetSSG(groupped_x, grand_mean_x);
            output.SSBGy = GetSSG(groupped_y, grand_mean_y);

            output.SSWGy = output.SSTy - output.SSBGy;
            output.SSWGx = output.SSTx - output.SSBGx;

            output.SCT  = GetCovariance(x, y);
            output.SCWG = GetCovarianceWithinGroup(groupped_x, groupped_y);

            output.rT  = output.SCT / System.Math.Sqrt(output.SSTx * output.SSTy);
            output.rWG = output.SCWG / System.Math.Sqrt(output.SSWGx * output.SSWGy);

            output.SSTy_adj  = output.SSTy - System.Math.Pow(output.SCT, 2) / output.SSTx;
            output.SSWGy_adj = output.SSWGy - System.Math.Pow(output.SCWG, 2) / output.SSWGx;
            output.SSBGy_adj = output.SSTy_adj - output.SSWGy_adj;

            output.dfT  = N - 2;
            output.dfBG = groupped_x.Count - 1;
            output.dfWG = N - groupped_x.Count - 1;

            output.MSBGy_adj = output.SSBGy_adj / output.dfBG;
            output.MSWGy_adj = output.SSWGy_adj / output.dfWG;

            output.Slope = output.SCWG / output.SSWGx;

            output.MeanWithinGroups_x = GetMeanWithinGroup(groupped_x);
            output.MeanWithinGroups_y = GetMeanWithinGroup(groupped_y);

            output.Intercepts = GetIntercepts(output.MeanWithinGroups_x, output.MeanWithinGroups_y, grand_mean_x, output.Slope);

            output.F = output.MSBGy_adj / output.MSWGy_adj;
            try
            {
                output.pValue = 1 - FDistribution.GetPercentile(output.F, output.dfBG, output.dfWG);
            }
            catch
            {
            }

            output.RejectH0 = output.pValue < significance_level;
        }
        /// <summary>
        /// hypothesis testing for a variable based on more than two groups using ANOVA
        ///
        /// Given that (for each of grpCat1, grpCat2, and grpCat1 * grpCat2):
        /// H_0 : mu_1 = mu_2 = ... mu_k (where k is the number of classes)
        /// H_A : mu != null_value
        ///
        /// p-value = Pr(> f) is the probability of at least as large a ratio between the "between" and "within" group variablity if in fact the means of all groups are equal
        /// if(p-value < significance_level) reject H_0
        /// </summary>
        /// <param name="groupedSample">The sample groupped based on the classes</param>
        /// <param name="pValue"></param>
        /// <param name="significance_level">p-value = Pr(> f) is the probability of at least as large a ratio between the "between" and "within" group variablity if in fact the means of all groups are equal</param>
        /// <returns></returns>
        public static void RunANOVA(double[] sample, int[] grpCat1, int[] grpCat2, out TwoWayANOVA output, double significance_level = 0.05)
        {
            output = new TwoWayANOVA();

            Dictionary <int, List <double> >    grpSample1  = new Dictionary <int, List <double> >();
            Dictionary <int, List <double> >    grpSample2  = new Dictionary <int, List <double> >();
            Dictionary <string, List <double> > grpSample12 = new Dictionary <string, List <double> >();
            int N = sample.Length;

            for (int i = 0; i < N; ++i)
            {
                List <double> grp1      = null;
                List <double> grp2      = null;
                List <double> grp12     = null;
                double        sampleVal = sample[i];
                int           grpId1    = grpCat1[i];
                int           grpId2    = grpCat2[i];
                string        grpId12   = string.Format("{0};{1}", grpId1, grpId2);

                if (grpSample1.ContainsKey(grpId1))
                {
                    grp1 = grpSample1[grpId1];
                }
                else
                {
                    grp1 = new List <double>();
                    grpSample1[grpId1] = grp1;
                }

                if (grpSample2.ContainsKey(grpId2))
                {
                    grp2 = grpSample2[grpId2];
                }
                else
                {
                    grp2 = new List <double>();
                    grpSample2[grpId2] = grp2;
                }

                if (grpSample12.ContainsKey(grpId12))
                {
                    grp12 = grpSample12[grpId12];
                }
                else
                {
                    grp12 = new List <double>();
                    grpSample12[grpId12] = grp12;
                }

                grp1.Add(sampleVal);
                grp2.Add(sampleVal);
                grp12.Add(sampleVal);
            }

            double grand_mean;

            //Sum of squares measures the total variablity
            output.SST   = GetSST(sample, out grand_mean);                        //sum of squares total
            output.SSG1  = GetSSG(grpSample1, grand_mean);                        //grpCat1: sum of squares group, which is known as explained variablity (explained by the group variable)
            output.SSG2  = GetSSG(grpSample2, grand_mean);                        //grpCat2: sum of squares group, which is known as explained variablity (explained by the group variable)
            output.SSG12 = GetSSG(grpSample12, grand_mean);                       //grpCat1 * grpCat2: sum of squares group, which is known as explained variablity (explained by the group variable)
            output.SSE   = output.SST - output.SSG1 - output.SSG2 - output.SSG12; //sum of squares error, which is known as unexplained variability (unexplained by the group variable, due to other reasons)

            //Degrees of freedom
            output.dfT   = sample.Length - 1;                                     //degrees of freedom total
            output.dfG1  = grpSample1.Count - 1;                                  //grpCat1: degrees of freedom group
            output.dfG2  = grpSample2.Count - 1;                                  //grpCat2: degrees of freedom group
            output.dfG12 = grpSample12.Count - 1;                                 //grpCat1 * grpCat2: degrees of freedom group
            output.dfE   = output.dfT - output.dfG1 - output.dfG2 - output.dfG12; // degrees of freedom error

            //Mean squares measures variability between and within groups, calculated as the total variability (sum of squares) scaled by the associated degrees of freedom
            output.MSG1  = output.SSG1 / output.dfG1;   //grpCat1: mean squares group : between group variability
            output.MSG2  = output.SSG2 / output.dfG2;   //grpCat1: mean squares group : between group variability
            output.MSG12 = output.SSG12 / output.dfG12; //grpCat12: mean squares group : between group variability
            output.MSE   = output.SSE / output.dfE;     // mean squares error : within group variablity

            //f statistic: ratio of the between group variablity and within group variablity
            output.F1  = output.MSG1 / output.MSE;
            output.F2  = output.MSG2 / output.MSE;
            output.F12 = output.MSG12 / output.MSE;

            //p-value = Pr(> f) is the probability of at least as large a ratio between the "between" and "within" group variablity if in fact the means of all groups are equal
            output.pValue1  = 1 - FDistribution.GetPercentile(output.F1, output.dfG1, output.dfE);
            output.pValue2  = 1 - FDistribution.GetPercentile(output.F2, output.dfG2, output.dfE);
            output.pValue12 = 1 - FDistribution.GetPercentile(output.F12, output.dfG12, output.dfE);

            output.RejectH0_Var1        = output.pValue1 < significance_level;
            output.RejectH0_Var2        = output.pValue2 < significance_level;
            output.RejectH0_Interaction = output.pValue12 < significance_level;
        }
Example #24
0
 public void TestFDistributionQuantile()
 {
     // N[Quantile[FRatioDistribution[3, 4], 1 - 1/20] , 25]
     Assert.AreEqual(6.591382116425581280992118, FDistribution.Quantile(0.95, 3, 4), 1E-14);
 }
Example #25
0
 public void TestFDistributionPDF()
 {
     // N[PDF[FRatioDistribution[3, 4], 1 - 1/20] , 25]
     Assert.AreEqual(0.3612251124036590033894851, FDistribution.PDF(0.95, 3, 4), 1E-14);
 }
Example #26
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);
        }
        public static string Func(double x, double deg1, double deg2, bool cumulative)
        {
            string res = new FDistribution(deg1, deg2).ProbabilityDensityFunction(x).ToString(CultureInfo.CurrentCulture);

            return($"x = {x}  \t deg1 = {deg1}\t deg2 = {deg2}\t cumulative = {cumulative}\t result = {res}");
        }
 /// <summary>
 /// Updates the given chart with the specified distribution.
 /// </summary>
 /// <param name="chart">A chart.</param>
 /// <param name="dist">The distribution.</param>
 /// <param name="function">The distribution function to plot.</param>
 /// <param name="numInterpolatedValues">The number of interpolated values.</param>
 /// <returns>A new chart.</returns>
 /// <remarks>
 /// Plots the specified function of the given distribution for 0.0001 &lt;= p &lt;= 0.9999.
 /// <br/>
 /// Titles are added only if chart does not currently contain any titles.
 /// <br/>
 /// chart.Series[0] is replaced, or added if necessary.
 /// </remarks>
 public static void Update( ref ChartControl chart, FDistribution dist, DistributionFunction function = DistributionFunction.PDF, int numInterpolatedValues = 100 )
 {
     List<string> titles = new List<string>()
       {
     "FDistribution",
     String.Format("df1={0}, df2={1}", dist.DegreesOfFreedom1, dist.DegreesOfFreedom2 )
       };
       UpdateContinuousDistribution( ref chart, dist, titles, function, numInterpolatedValues );
 }
Example #29
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);
 }
Example #30
0
        public void MedianTest()
        {
            FDistribution target = new FDistribution(2, 3);

            Assert.AreEqual(target.Median, target.InverseDistributionFunction(0.5));
        }