public void ValidateCumulativeDistribution(double location, double scale, double dof, double x, double p)
        {
            var dist = new StudentT(location, scale, dof);

            Assert.That(dist.CumulativeDistribution(x), Is.EqualTo(p).Within(1e-13));
            Assert.That(StudentT.CDF(location, scale, dof, x), Is.EqualTo(p).Within(1e-13));
        }
        public IList <LinearFitResult> Fit(double[] observations)
        {
            if (observations.Length != DesignMatrix.RowCount)
            {
                throw new ArgumentException("Wrong number of rows"); // Not L10N
            }
            var coefficients = QrFactorization.Solve(observations);
            var fittedValues = new double[observations.Length];
            var residuals    = new double[observations.Length];

            for (int iRow = 0; iRow < observations.Length; iRow++)
            {
                var designRow = Enumerable.Range(0, DesignMatrix.ColumnCount).Select(index => DesignMatrix[iRow, index]).ToArray();
                fittedValues[iRow] = DotProduct(designRow, coefficients);
                residuals[iRow]    = observations[iRow] - fittedValues[iRow];
            }
            double rss = DotProduct(residuals, residuals);

            int    degreesOfFreedom   = observations.Length - QrFactorization.NumberIndependentColumns;
            double resVar             = rss / degreesOfFreedom;
            double sigma              = Math.Sqrt(resVar);
            var    covarianceUnscaled = MatrixCrossproductInverse;
            var    scaledCovariance   = covarianceUnscaled.Multiply(sigma * sigma);
            var    indepColIndexes    = QrFactorization.IndependentColumnIndexes.ToArray();
            var    result             = new List <LinearFitResult>();

            foreach (var contrastRow in ContrastValues.EnumerateRows())
            {
                double standardError = 0;
                for (int iRow = 0; iRow < indepColIndexes.Length; iRow++)
                {
                    for (int iCol = 0; iCol < indepColIndexes.Length; iCol++)
                    {
                        standardError += contrastRow[indepColIndexes[iRow]] * scaledCovariance[iRow, iCol] * contrastRow[indepColIndexes[iCol]];
                    }
                }
                standardError = Math.Sqrt(standardError);
                double foldChange = DotProduct(coefficients, contrastRow);
                double tValue     = foldChange / standardError;
                double pValue;
                if (0 != degreesOfFreedom)
                {
                    var studentT = new StudentT(0, 1.0, degreesOfFreedom);
                    pValue = (1 - studentT.CumulativeDistribution(Math.Abs(tValue))) * 2;
                }
                else
                {
                    pValue = 1;
                }
                result.Add(new LinearFitResult(foldChange)
                           .SetDegreesOfFreedom(degreesOfFreedom)
                           .SetTValue(tValue)
                           .SetStandardError(standardError)
                           .SetPValue(pValue));
            }
            return(result);
        }
        public void ValidateCumulativeDistribution(
            [Values(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)] double location,
            [Values(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0)] double scale,
            [Values(1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 2.0, Double.PositiveInfinity, Double.PositiveInfinity, Double.PositiveInfinity)] double dof,
            [Values(0.0, 1.0, -1.0, 2.0, -2.0, 0.0, 1.0, -1.0, 2.0, -2.0, 0.0, 1.0, 2.0)] double x,
            [Values(0.5, 0.75, 0.25, 0.852416382349567, 0.147583617650433, 0.5, 0.788675134594813, 0.211324865405187, 0.908248290463863, 0.091751709536137, 0.5, 0.841344746068543, 0.977249868051821)] double c)
        {
            var n = new StudentT(location, scale, dof);

            AssertHelpers.AlmostEqual(c, n.CumulativeDistribution(x), 13);
        }
Exemple #4
0
        public override double Test(IList <double> sample_0, IList <double> sample_1)
        {
            double mean_1   = ToolsMathStatistics.Mean(sample_0);
            double mean_2   = ToolsMathStatistics.Mean(sample_1);
            double variance = ToolsMathStatistics.VariancePooled(sample_0, sample_1);

            double t_statistic        = (mean_1 - mean_2) / (Math.Sqrt(variance) * Math.Sqrt((1.0 / sample_0.Count) + (1.0 / sample_1.Count)));
            int    degrees_of_freedom = (sample_0.Count + sample_1.Count) - 2;

            StudentT distribution = new StudentT(0.1, 1.0, degrees_of_freedom);

            return(distribution.CumulativeDistribution(-t_statistic));
        }
        public static double TestStatic(IList <double> sample_0, IList <double> sample_1)
        {
            if (sample_0.Count != sample_1.Count)
            {
                throw new Exception("Samples not of equal size");
            }
            double[] difference         = ToolsMathCollection.SubtractElements(sample_0, sample_1);
            double   mean_difference    = ToolsMathStatistics.Mean(difference);
            double   variance           = ToolsMathStatistics.Variance(difference);
            double   t_statistic        = mean_difference / (Math.Sqrt(variance) / Math.Sqrt(sample_0.Count));
            double   degrees_of_freedom = (sample_0.Count - 1);

            StudentT distribution = new StudentT(0.1, 1.0, degrees_of_freedom);

            return(distribution.CumulativeDistribution(t_statistic));
        }
Exemple #6
0
        public override double Test(IList <double> sample_0, IList <double> sample_1)
        {
            double mean_0     = ToolsMathStatistics.Mean(sample_0);
            double mean_1     = ToolsMathStatistics.Mean(sample_1);
            double variance_0 = ToolsMathStatistics.Variance(sample_0, mean_0);
            double variance_1 = ToolsMathStatistics.Variance(sample_0, mean_1);


            double t_statistic = (mean_0 - mean_1) / Math.Sqrt((variance_0 / sample_0.Count) + (variance_1 / sample_1.Count));

            //Welch–Satterthwaite equation:
            double dof_nominator   = ToolsMath.Sqr((variance_0 / sample_0.Count) + (variance_1 / sample_1.Count));
            double dof_denominator =
                (Math.Pow(variance_0, 4) / (sample_0.Count * sample_0.Count * (sample_0.Count - 1))) +
                (Math.Pow(variance_1, 4) / (sample_1.Count * sample_1.Count * (sample_1.Count - 1)));
            double degrees_of_freedom = dof_nominator / dof_denominator;

            StudentT distribution = new StudentT(0.1, 1.0, degrees_of_freedom);

            return(distribution.CumulativeDistribution(-t_statistic));
        }
Exemple #7
0
        public void ValidateCumulativeDistribution(double location, double scale, double dof, double x, double c)
        {
            var n = new StudentT(location, scale, dof);

            AssertHelpers.AlmostEqual(c, n.CumulativeDistribution(x), 13);
        }
        /// <summary>
        /// Run example
        /// </summary>
        /// <a href="http://en.wikipedia.org/wiki/StudentT_distribution">StudentT distribution</a>
        public void Run()
        {
            // 1. Initialize the new instance of the StudentT distribution class with parameters Location = 0, Scale = 1, DegreesOfFreedom = 1
            var studentT = new StudentT();

            Console.WriteLine(@"1. Initialize the new instance of the StudentT distribution class with parameters Location = {0}, Scale = {1}, DegreesOfFreedom = {2}", studentT.Location, studentT.Scale, studentT.DegreesOfFreedom);
            Console.WriteLine();

            // 2. Distributuion properties:
            Console.WriteLine(@"2. {0} distributuion properties:", studentT);

            // Cumulative distribution function
            Console.WriteLine(@"{0} - Сumulative distribution at location '0.3'", studentT.CumulativeDistribution(0.3).ToString(" #0.00000;-#0.00000"));

            // Probability density
            Console.WriteLine(@"{0} - Probability density at location '0.3'", studentT.Density(0.3).ToString(" #0.00000;-#0.00000"));

            // Log probability density
            Console.WriteLine(@"{0} - Log probability density at location '0.3'", studentT.DensityLn(0.3).ToString(" #0.00000;-#0.00000"));

            // Entropy
            Console.WriteLine(@"{0} - Entropy", studentT.Entropy.ToString(" #0.00000;-#0.00000"));

            // Largest element in the domain
            Console.WriteLine(@"{0} - Largest element in the domain", studentT.Maximum.ToString(" #0.00000;-#0.00000"));

            // Smallest element in the domain
            Console.WriteLine(@"{0} - Smallest element in the domain", studentT.Minimum.ToString(" #0.00000;-#0.00000"));

            // Mean
            Console.WriteLine(@"{0} - Mean", studentT.Mean.ToString(" #0.00000;-#0.00000"));

            // Median
            Console.WriteLine(@"{0} - Median", studentT.Median.ToString(" #0.00000;-#0.00000"));

            // Mode
            Console.WriteLine(@"{0} - Mode", studentT.Mode.ToString(" #0.00000;-#0.00000"));

            // Variance
            Console.WriteLine(@"{0} - Variance", studentT.Variance.ToString(" #0.00000;-#0.00000"));

            // Standard deviation
            Console.WriteLine(@"{0} - Standard deviation", studentT.StdDev.ToString(" #0.00000;-#0.00000"));

            // 3. Generate 10 samples of the StudentT distribution
            Console.WriteLine(@"3. Generate 10 samples of the StudentT distribution");
            for (var i = 0; i < 10; i++)
            {
                Console.Write(studentT.Sample().ToString("N05") + @" ");
            }

            Console.WriteLine();
            Console.WriteLine();

            // 4. Generate 100000 samples of the StudentT(0, 1, 1) distribution and display histogram
            Console.WriteLine(@"4. Generate 100000 samples of the StudentT(0, 1, 1) distribution and display histogram");
            var data = new double[100000];

            for (var i = 0; i < data.Length; i++)
            {
                data[i] = studentT.Sample();
            }

            ConsoleHelper.DisplayHistogram(data);

            // 5. Generate 100000 samples of the StudentT(0, 1, 5) distribution and display histogram
            Console.WriteLine(@"5. Generate 100000 samples of the StudentT(0, 1, 5) distribution and display histogram");
            studentT.DegreesOfFreedom = 5;
            for (var i = 0; i < data.Length; i++)
            {
                data[i] = studentT.Sample();
            }

            ConsoleHelper.DisplayHistogram(data);
            Console.WriteLine();

            // 6. Generate 100000 samples of the StudentT(0, 1, 10) distribution and display histogram
            Console.WriteLine(@"6. Generate 100000 samples of the StudentT(0, 1, 10) distribution and display histogram");
            studentT.DegreesOfFreedom = 10;
            for (var i = 0; i < data.Length; i++)
            {
                data[i] = studentT.Sample();
            }

            ConsoleHelper.DisplayHistogram(data);
        }
 public void ValidateCumulativeDistribution(double location, double scale, double dof, double x, double c)
 {
     var n = new StudentT(location, scale, dof);
     AssertHelpers.AlmostEqual(c, n.CumulativeDistribution(x), 13);
 }
 public void ValidateCumulativeDistribution(
     [Values(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)] double location, 
     [Values(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0)] double scale, 
     [Values(1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 2.0, Double.PositiveInfinity, Double.PositiveInfinity, Double.PositiveInfinity)] double dof, 
     [Values(0.0, 1.0, -1.0, 2.0, -2.0, 0.0, 1.0, -1.0, 2.0, -2.0, 0.0, 1.0, 2.0)] double x, 
     [Values(0.5, 0.75, 0.25, 0.852416382349567, 0.147583617650433, 0.5, 0.788675134594813, 0.211324865405187, 0.908248290463863, 0.091751709536137, 0.5, 0.841344746068543, 0.977249868051821)] double c)
 {
     var n = new StudentT(location, scale, dof);
     AssertHelpers.AlmostEqual(c, n.CumulativeDistribution(x), 13);
 }
Exemple #11
0
 public void UpdateP(StudentT tDistribution)
 {
     this.PValue = 2.0 * tDistribution.CumulativeDistribution(-this.TestStatistics);
 }