Пример #1
0
 private void TestAdiComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     _test            = FTest.Select(TestAdiComboBox.SelectedValue.ToString());
     Sure.Text        = _test.Sure.ToString();
     SoruSayisi.Text  = _test.SoruSayisi.ToString();
     CevapSayisi.Text = _test.CevapSayisi.ToString();
 }
Пример #2
0
 public KullaniciSayfasi(string testAdi)
 {
     InitializeComponent();
     _colors  = Global.Colors();
     _icons   = Global.Icons();
     _testAdi = testAdi;
     _test    = FTest.Select(_testAdi); // Boş Yapma
 }
Пример #3
0
        protected override void EndProcessing()
        {
            var hypo = TestingHelper.GetTwoSampleHypothesis(Alternate);

            var test = new FTest(Variance1, Variance2, DegreesOfFreedom1, DegreesOfFreedom2, hypo)
            {
                Size = Size
            };

            WriteObject(test);
        }
Пример #4
0
 void TestAdiDoldur()
 {
     TestCombobox.Items.Clear();
     testler = new List <Test>();
     testler = FTest.SelectAll(Global.ServerKullanici.KullaniciId);
     if (testler == null)
     {
         return;
     }
     foreach (var test in testler)
     {
         TestCombobox.Items.Add(test.TestAdi);
     }
 }
Пример #5
0
        static void Main(string[] args)
        {
            city   a         = new city();
            FTest  ReadGeoF  = new FTest(a.readGeo);
            mega   b         = new mega();
            STest  ReadGeoS  = new STest(b.readGeo);
            STest  ReadNameS = new STest(b.readName);
            region c         = new region();
            TTest  ReadGeoT  = new TTest(c.readGeo);
            TTest  ReadNameT = new TTest(c.readName);
            TTest  Sum;

            Sum = ReadGeoT + ReadNameT;
        }
Пример #6
0
        public void FTestConstructorTest2()
        {
            // The following example has been based on the page "F-Test for Equality
            // of Two Variances", from NIST/SEMATECH e-Handbook of Statistical Methods:
            //
            //  http://www.itl.nist.gov/div898/handbook/eda/section3/eda359.htm
            //

            // Consider a data set containing 480 ceramic strength
            // measurements for two batches of material. The summary
            // statistics for each batch are shown below:

            // BATCH 1:
            int numberOfObservations1 = 240;
            // double mean1 = 688.9987;
            double stdDev1 = 65.54909;
            double var1    = stdDev1 * stdDev1;

            // BATCH 2:
            int numberOfObservations2 = 240;
            // double mean2 = 611.1559;
            double stdDev2 = 61.85425;
            double var2    = stdDev2 * stdDev2;

            // Here, we will be testing the null hypothesis that
            // the variances for the two batches are equal.

            int degreesOfFreedom1 = numberOfObservations1 - 1;
            int degreesOfFreedom2 = numberOfObservations2 - 1;

            // Now we can create a F-Test to test the difference between variances
            var ftest = new FTest(var1, var2, degreesOfFreedom1, degreesOfFreedom2);

            double statistic   = ftest.Statistic;   // 1.123037
            double pvalue      = ftest.PValue;      // 0.185191
            bool   significant = ftest.Significant; // false

            // The F test indicates that there is not enough evidence
            // to reject the null hypothesis that the two batch variances
            // are equal at the 0.05 significance level.

            Assert.AreEqual(1.1230374607443194, statistic);
            Assert.AreEqual(0.18519157993853722, pvalue);
            Assert.IsFalse(significant);
        }
        /// <summary>
        /// Calculates the N Value for each protein based on its intensity values in two conditions
        /// </summary>
        /// //need explanation on what an N value is
        public void GetNValueUsingTTest(List <double> proteinFirstConditionIntensityValues, List <double> proteinSecondConditionIntensityValues,
                                        List <double> actualNValues, List <double> actualPValues, List <double> actualLogFoldChange, double sOValue)
        {
            //why are these being cloned?
            double[] firstConditionIntensityValues  = new double[proteinFirstConditionIntensityValues.Count];
            double[] secondConditionIntensityValues = new double[proteinSecondConditionIntensityValues.Count];

            for (int i = 0; i < proteinFirstConditionIntensityValues.Count; i++)
            {
                firstConditionIntensityValues[i] = proteinFirstConditionIntensityValues[i];
            }

            for (int i = 0; i < proteinSecondConditionIntensityValues.Count; i++)
            {
                secondConditionIntensityValues[i] = proteinSecondConditionIntensityValues[i];
            }

            //sometimes less is more with variable names. For example, "mean1" vs "firstConditionIntensityMean" improves readability without diminishing clarity
            double firstConditionIntensityMean         = CalculateProteinMeanIntensityValue(firstConditionIntensityValues);
            double secondConditionIntensityMean        = CalculateProteinMeanIntensityValue(secondConditionIntensityValues);
            double firstConditionIntensityStandardDev  = CalculateProteinIntensityValuesStandardDeviation(firstConditionIntensityValues, firstConditionIntensityMean);
            double secondConditionIntensityStandardDev = CalculateProteinIntensityValuesStandardDeviation(secondConditionIntensityValues, secondConditionIntensityMean);
            double firstConditionIntensityVariance     = firstConditionIntensityStandardDev * firstConditionIntensityStandardDev;
            double secondConditionIntensityVariance    = secondConditionIntensityStandardDev * secondConditionIntensityStandardDev;

            //don't need to save "ftest" if you're never going to use it again
            //write a note here what the purpose of the f-test is. It's not as well known as the t-test
            var  ftest       = new FTest(firstConditionIntensityVariance, secondConditionIntensityVariance, proteinFirstConditionIntensityValues.Count - 1, proteinSecondConditionIntensityValues.Count - 1);
            bool significant = ftest.Significant; // gets whether null hypothesis can be rejected

            // Create two tailed t test to get p values
            TwoSampleTTest ttest         = new TwoSampleTTest(firstConditionIntensityValues, secondConditionIntensityValues, !significant);
            double         pValue        = ttest.PValue;
            double         logpValue     = -Math.Log10(pValue);
            double         logfoldChange = secondConditionIntensityMean - firstConditionIntensityMean;
            //need a note on where this came from
            double nValue = (logpValue * (logfoldChange * logfoldChange - sOValue * sOValue)) / ((logfoldChange) * (logfoldChange));

            //these are interesting names that imply the existence of fake nvalues, foldchanges, and pvalues.
            //how about "allNValues"?
            actualNValues.Add(nValue);
            actualLogFoldChange.Add(logfoldChange);
            actualPValues.Add(pValue);
        }
        /// <summary>
        /// Calculates the N Value for each protein based on its intensity values in two conditions.
        /// the permutationTesting boolean is used to determine if we are caluclating permuted or observed N Values.
        ///
        /// The N value is a metric which combines the pValue and the magnitude change (through the LogFoldChange) of
        /// the protein amongst the two conditions in order to enable significance classifcation of the protein based
        /// on the same principle of Volcano Plots (Volcano plots allow to identify signifcantly different proteins in large data
        /// sets comprising of replicate data by plotting the signifance statistic on the y axis, which is the pValue here,
        /// and the magnitude chanbge on the x axis, which is the logFoldChange here)
        /// </summary>
        public List <double> GetNValueUsingTTest(List <double> proteinFirstConditionIntensityValues, List <double> proteinSecondConditionIntensityValues,
                                                 double sOValue, bool permutationTesting)
        {
            double mean1     = proteinFirstConditionIntensityValues.Average();
            double mean2     = proteinSecondConditionIntensityValues.Average();
            double stdDev1   = CalculateProteinIntensityValuesStandardDeviation(proteinFirstConditionIntensityValues, mean1);
            double stdDev2   = CalculateProteinIntensityValuesStandardDeviation(proteinSecondConditionIntensityValues, mean2);
            double variance1 = stdDev1 * stdDev1;
            double variance2 = stdDev2 * stdDev2;

            // the f-test is used to determine whether the variance of the two intensityvalue popluations are equal
            // the significant variable gets whether the null hypothesis can be rejected
            bool significant = new FTest(variance1, variance2, proteinFirstConditionIntensityValues.Count - 1, proteinSecondConditionIntensityValues.Count - 1).Significant;

            // Create two tailed t test to get p values
            TwoSampleTTest ttest = new TwoSampleTTest(mean1, variance1, proteinFirstConditionIntensityValues.Count,
                                                      mean2, variance2, proteinSecondConditionIntensityValues.Count, !significant);

            double pValue        = ttest.PValue;
            double logpValue     = -Math.Log10(pValue);
            double logfoldChange = mean2 - mean1;

            // compute N-Value for protein
            double nValue = (logpValue * (logfoldChange * logfoldChange - sOValue * sOValue)) / ((logfoldChange) * (logfoldChange));

            if (!permutationTesting)
            {
                List <double> proteinStatistics = new List <double>();
                proteinStatistics.Add(nValue);
                proteinStatistics.Add(pValue);
                proteinStatistics.Add(logfoldChange);
                return(proteinStatistics);
                //nValues.Add(nValue);
                ///logFoldChange.Add(logfoldChange);
                //pValues.Add(pValue);
            }
            else
            {
                List <double> proteinStatistics = new List <double>();
                proteinStatistics.Add(nValue);
                return(proteinStatistics);
                //nValues.Add(nValue);
            }
        }
Пример #9
0
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            testler = new List <Test>();
            if (Global.ServerKullanici == null)
            {
                return;
            }

            testler = FTest.SelectAll(Global.ServerKullanici.KullaniciId);

            if (testler == null)
            {
                return;
            }
            foreach (var test in testler)
            {
                TestAdiComboBox.Items.Add(test.TestAdi);
            }
        }
Пример #10
0
        public void FTestConstructorTest()
        {
            double var1 = 1.05766555271071;
            double var2 = 1.16570834301777;
            int    d1   = 49;
            int    d2   = 49;

            FTest oneGreater = new FTest(var1, var2, d1, d2, TwoSampleHypothesis.FirstValueIsGreaterThanSecond);
            FTest oneSmaller = new FTest(var1, var2, d1, d2, TwoSampleHypothesis.FirstValueIsSmallerThanSecond);
            FTest twoTail    = new FTest(var1, var2, d1, d2, TwoSampleHypothesis.ValuesAreDifferent);

            Assert.AreEqual(0.632, oneGreater.PValue, 1e-3);
            Assert.AreEqual(0.367, oneSmaller.PValue, 1e-3);
            Assert.AreEqual(0.734, twoTail.PValue, 1e-3);

            Assert.IsFalse(Double.IsNaN(oneGreater.PValue));
            Assert.IsFalse(Double.IsNaN(oneSmaller.PValue));
            Assert.IsFalse(Double.IsNaN(twoTail.PValue));
        }
        public static void FirstDoesNotHaveHigherVariance(AggregateMetrics first, AggregateMetrics second)
        {
            double[] firstSamples  = first.Runs.Select(run => run.ElapsedNanos).ToArray();
            double[] secondSamples = second.Runs.Select(run => run.ElapsedNanos).ToArray();

            // Fisher's F-test (also known as Snedecor)
            var firstVariance  = Measures.Variance(firstSamples);
            var secondVariance = Measures.Variance(secondSamples);
            var fishersFTest   = new FTest(
                firstVariance,
                secondVariance,
                firstSamples.Length - 1,
                secondSamples.Length - 1,
                TwoSampleHypothesis.FirstValueIsGreaterThanSecond);

            Trace.WriteLine(
                "FTest Var(s1) > Var(s2):  " + fishersFTest.PValue +
                " - Significant: " + fishersFTest.Significant + " - Hyp: " + fishersFTest.Hypothesis);

            PerfAssertContext.AssertIsFalse(fishersFTest.Significant);
        }
Пример #12
0
        private void compute(double[][] x, double[] y)
        {
            int n = x.Length;
            int p = NumberOfInputs;

            SSt             = 0;
            SSe             = 0;
            outputMean      = 0.0;
            NumberOfSamples = x.Length;

            // Compute the regression
            OrdinaryLeastSquares.Token = Token;
            regression        = OrdinaryLeastSquares.Learn(x, y);
            informationMatrix = OrdinaryLeastSquares.GetInformationMatrix();

            // Calculate mean of the expected outputs
            outputMean = y.Mean();

            // Calculate actual outputs (results)
#pragma warning disable 612, 618
            results = regression.Transform(x);

            // Calculate SSe and SSt
            for (int i = 0; i < x.Length; i++)
            {
                double d;
                d    = y[i] - results[i];
                SSe += d * d;

                d    = y[i] - outputMean;
                SSt += d * d;
            }


            // Calculate SSr
            SSr = SSt - SSe;

            // Calculate R-Squared
            rSquared = (SSt != 0) ? 1.0 - (SSe / SSt) : 1.0;

            // Calculated Adjusted R-Squared
            if (rSquared == 1)
            {
                rAdjusted = 1;
            }
            else
            {
                if (n - p == 1)
                {
                    rAdjusted = double.NaN;
                }
                else
                {
                    rAdjusted = 1.0 - (1.0 - rSquared) * ((n - 1.0) / (n - p - 1.0));
                }
            }

            // Calculate Degrees of Freedom
            DFr = p;
            DFe = n - (p + 1);
            DFt = DFr + DFe;

            // Calculate Sum of Squares Mean
            MSe = SSe / DFe;
            MSr = SSr / DFr;
            MSt = SSt / DFt;

            // Calculate the F statistic
            ftest    = new FTest(MSr / MSe, DFr, DFe);
            stdError = Math.Sqrt(MSe);

            // Create the ANOVA table
            List <AnovaVariationSource> table = new List <AnovaVariationSource>();
            table.Add(new AnovaVariationSource(this, "Regression", SSr, DFr, MSr, ftest));
            table.Add(new AnovaVariationSource(this, "Error", SSe, DFe, MSe, null));
            table.Add(new AnovaVariationSource(this, "Total", SSt, DFt, MSt, null));
            this.anovaTable = new AnovaSourceCollection(table);

            // Compute coefficient standard errors;
            standardErrors = new double[NumberOfInputs + 1];
            for (int i = 0; i < informationMatrix.Length; i++)
            {
                standardErrors[i] = Math.Sqrt(MSe * informationMatrix[i][i]);
            }

            // Compute coefficient tests
            for (int i = 0; i < CoefficientValues.Length; i++)
            {
                double tStatistic = CoefficientValues[i] / standardErrors[i];

                ttests[i] = new TTest(estimatedValue: CoefficientValues[i],
                                      standardError: standardErrors[i], degreesOfFreedom: DFe);

                ftests[i] = new FTest(tStatistic * tStatistic, 1, DFe);

                confidences[i] = ttests[i].GetConfidenceInterval(confidencePercent);
            }


            // Compute model performance tests
            ttest         = new TTest(results, outputMean);
            ztest         = new ZTest(results, outputMean);
            chiSquareTest = new ChiSquareTest(y, results, n - p - 1);
#pragma warning restore 612, 618
        }
        /// <summary>
        /// Generates fake N Value's for each protein based on its intensity values in two conditions using Permutation Testing
        /// </summary>
        /// //alright, you win, I guess there are fake nvalues. Maybe a better nomenclature would be observedNValues vs permutedNValues?
        /// There's a lot of code duplication between this method and GetNValueUsingTTest. Try creating permutations and then calling GetNValueUsingTTest for each one.
        public void GetNValueUsingPermutationtests(List <double> proteinFirstConditionIntensityValues, List <double> proteinSecondConditionIntensityValues,
                                                   List <double> permutedNValues, double sOValue)
        {
            //why are these being cloned?
            double[] firstConditionIntensityValues  = new double[proteinFirstConditionIntensityValues.Count];
            double[] secondConditionIntensityValues = new double[proteinSecondConditionIntensityValues.Count];

            for (int i = 0; i < proteinFirstConditionIntensityValues.Count; i++)
            {
                firstConditionIntensityValues[i] = proteinFirstConditionIntensityValues[i];
            }

            for (int i = 0; i < proteinSecondConditionIntensityValues.Count; i++)
            {
                secondConditionIntensityValues[i] = proteinSecondConditionIntensityValues[i];
            }


            int[] indicesOfFirstConditionIntensityValues  = new int[firstConditionIntensityValues.Length];
            int[] indicesOfSecondConditionIntensityValues = new int[secondConditionIntensityValues.Length];
            for (int i = 0; i < proteinFirstConditionIntensityValues.Count; i++)
            {
                indicesOfFirstConditionIntensityValues[i] = i;
            }
            for (int i = 0; i < proteinSecondConditionIntensityValues.Count; i++)
            {
                indicesOfSecondConditionIntensityValues[i] = i;
            }

            List <List <int> > allTwoIndiciesCombinationsFromFirstCondition  = GenerateAllCombinationsOfTwoIndices(indicesOfFirstConditionIntensityValues);
            List <List <int> > allTwoIndiciesCombinationsFromSecondCondition = GenerateAllCombinationsOfTwoIndices(indicesOfSecondConditionIntensityValues);

            int count = 0;

            foreach (var twoIndiciesCombinationEntryFromFirstCondition in allTwoIndiciesCombinationsFromFirstCondition)
            {
                foreach (var twoIndiciesCombinationEntryFromSecondCondition in allTwoIndiciesCombinationsFromSecondCondition)
                {
                    // these the new arrays which will be made after swapping intensity values between the two conditions
                    double[] swappedFirstConditionIntensityValues  = new double[firstConditionIntensityValues.Length];
                    double[] swappedSecondConditionIntensityValues = new double[secondConditionIntensityValues.Length];
                    int      swappedFirstConditionArrayTracker     = 0;
                    int      swappedSecondConditionArrayTracker    = 0;

                    int[] indiciesToSwapFromFirstCondition     = new int[2];
                    int[] indiciesToSwapFromSecondCondition    = new int[2];
                    int   removeIndiciesFirstConditionTracker  = 0;
                    int   removeIndiciesSecondConditionTracker = 0;

                    // store the indices, corresponding to intensity values, to be swapped from first condition
                    foreach (var index in twoIndiciesCombinationEntryFromFirstCondition)
                    {
                        indiciesToSwapFromFirstCondition[removeIndiciesFirstConditionTracker] = index;
                        removeIndiciesFirstConditionTracker++;
                    }

                    // store the indices, corresponding to intensity values, to be swapped from second condition
                    foreach (var index in twoIndiciesCombinationEntryFromSecondCondition)
                    {
                        indiciesToSwapFromSecondCondition[removeIndiciesSecondConditionTracker] = index;
                        removeIndiciesSecondConditionTracker++;
                    }

                    // add the intensity values to be swapped from first condition the second condition
                    for (int j = 0; j < indiciesToSwapFromFirstCondition.Count(); j++)
                    {
                        swappedSecondConditionIntensityValues[swappedSecondConditionArrayTracker] = firstConditionIntensityValues[indiciesToSwapFromFirstCondition[j]];
                        swappedSecondConditionArrayTracker++;
                    }


                    // add the intensity values to be swapped from second condition the first condition
                    for (int j = 0; j < indiciesToSwapFromSecondCondition.Count(); j++)
                    {
                        swappedFirstConditionIntensityValues[swappedFirstConditionArrayTracker] = secondConditionIntensityValues[indiciesToSwapFromSecondCondition[j]];
                        swappedFirstConditionArrayTracker++;
                    }

                    // now we add the remaining intensity values from the first condition to the swappedFirstCondition Array
                    for (int j = 0; j < firstConditionIntensityValues.Count(); j++)
                    {
                        if (indiciesToSwapFromFirstCondition.Contains(j))
                        {
                            continue;
                        }
                        swappedFirstConditionIntensityValues[swappedFirstConditionArrayTracker] = firstConditionIntensityValues[j];
                        swappedFirstConditionArrayTracker++;
                    }

                    // now we add the remaining intensity values from the second condition to the swappedSecondCondition Array
                    for (int j = 0; j < secondConditionIntensityValues.Count(); j++)
                    {
                        if (indiciesToSwapFromSecondCondition.Contains(j))
                        {
                            continue;
                        }
                        swappedSecondConditionIntensityValues[swappedSecondConditionArrayTracker] = secondConditionIntensityValues[j];
                        swappedSecondConditionArrayTracker++;
                    }


                    // at this stage we have the newly made swapped arrays with mixture of groups.
                    // need to proceed with T tests for these groups to generate fake p values.

                    double firstConditionIntensityMean         = CalculateProteinMeanIntensityValue(swappedFirstConditionIntensityValues);
                    double secondConditionIntensityMean        = CalculateProteinMeanIntensityValue(swappedSecondConditionIntensityValues);
                    double firstConditionIntensityStandardDev  = CalculateProteinIntensityValuesStandardDeviation(swappedFirstConditionIntensityValues, firstConditionIntensityMean);
                    double secondConditionIntensityStandardDev = CalculateProteinIntensityValuesStandardDeviation(swappedSecondConditionIntensityValues, secondConditionIntensityMean);
                    double firstConditionIntensityVariance     = firstConditionIntensityStandardDev * firstConditionIntensityStandardDev;
                    double secondConditionIntensityVariance    = secondConditionIntensityStandardDev * secondConditionIntensityStandardDev;

                    var  ftest       = new FTest(firstConditionIntensityVariance, secondConditionIntensityVariance, swappedFirstConditionIntensityValues.Length - 1, swappedSecondConditionIntensityValues.Length - 1);
                    bool significant = ftest.Significant; // gets whether null hypothesis can be rejected

                    // Create two tailed t test to get p values
                    TwoSampleTTest ttest         = new TwoSampleTTest(swappedFirstConditionIntensityValues, swappedSecondConditionIntensityValues, !significant);
                    double         pValue        = ttest.PValue;
                    double         logpValue     = -Math.Log10(pValue);
                    double         logfoldChange = secondConditionIntensityMean - firstConditionIntensityMean;

                    permutedNValues.Add((logpValue * (logfoldChange * logfoldChange - sOValue * sOValue)) / ((logfoldChange) * (logfoldChange)));
                }
                count++;
                if (count == 2)
                {
                    break;
                }
            }
        }
Пример #14
0
        public void ComputeTest2()
        {
            // Consider the following data. An experimenter would
            // like to infer a relationship between two variables
            // A and B and a corresponding outcome variable R.

            double[][] example =
            {
                //                A    B      R
                new double[] { 6.41, 10.11, 26.1 },
                new double[] { 6.61, 22.61, 33.8 },
                new double[] { 8.45, 11.11, 52.7 },
                new double[] { 1.22, 18.11, 16.2 },
                new double[] { 7.42, 12.81, 87.3 },
                new double[] { 4.42, 10.21, 12.5 },
                new double[] { 8.61, 11.94, 77.5 },
                new double[] { 1.73, 13.13, 12.1 },
                new double[] { 7.47, 17.11, 86.5 },
                new double[] { 6.11, 15.13, 62.8 },
                new double[] { 1.42, 16.11, 17.5 },
            };

            // For this, we first extract the input and output
            // pairs. The first two columns have values for the
            // input variables, and the last for the output:

            double[][] inputs = example.GetColumns(0, 1);
            double[]   output = example.GetColumn(2);

            // Next, we can create a new multiple linear regression for the variables
            var regression = new MultipleLinearRegressionAnalysis(inputs, output, intercept: true);

            regression.Compute(); // compute the analysis

            // Now we can show a summary of analysis
            // Accord.Controls.DataGridBox.Show(regression.Coefficients);

            // We can also show a summary ANOVA
            // Accord.Controls.DataGridBox.Show(regression.Table);


            // And also extract other useful information, such
            // as the linear coefficients' values and std errors:
            double[] coef = regression.CoefficientValues;
            double[] stde = regression.StandardErrors;

            // Coefficients of performance, such as r²
            double rsquared = regression.RSquared;

            // Hypothesis tests for the whole model
            ZTest ztest = regression.ZTest;
            FTest ftest = regression.FTest;

            // and for individual coefficients
            TTest ttest0 = regression.Coefficients[0].TTest;
            TTest ttest1 = regression.Coefficients[1].TTest;

            // and also extract confidence intervals
            DoubleRange ci = regression.Coefficients[0].Confidence;

            Assert.AreEqual(3, coef.Length);
            Assert.AreEqual(8.7405051051757816, coef[0]);
            Assert.AreEqual(1.1198079243314365, coef[1], 1e-10);
            Assert.AreEqual(-19.604474518407862, coef[2], 1e-10);
            Assert.IsFalse(coef.HasNaN());

            Assert.AreEqual(2.375916659234715, stde[0], 1e-10);
            Assert.AreEqual(1.7268508921418664, stde[1], 1e-10);
            Assert.AreEqual(30.989640986710953, stde[2], 1e-10);
            Assert.IsFalse(coef.HasNaN());

            Assert.AreEqual(0.62879941171298936, rsquared);

            Assert.AreEqual(0.99999999999999822, ztest.PValue);
            Assert.AreEqual(0.018986050133298293, ftest.PValue, 1e-10);

            Assert.AreEqual(0.0062299844256985537, ttest0.PValue);
            Assert.AreEqual(0.53484850318449118, ttest1.PValue, 1e-14);
            Assert.IsFalse(Double.IsNaN(ttest1.PValue));

            Assert.AreEqual(3.2616314640800566, ci.Min);
            Assert.AreEqual(14.219378746271506, ci.Max);
        }
        public void learn_Test()
        {
            #region doc_learn_part1
            // Consider the following data. An experimenter would
            // like to infer a relationship between two variables
            // A and B and a corresponding outcome variable R.

            double[][] example =
            {
                //                A    B      R
                new double[] { 6.41, 10.11, 26.1 },
                new double[] { 6.61, 22.61, 33.8 },
                new double[] { 8.45, 11.11, 52.7 },
                new double[] { 1.22, 18.11, 16.2 },
                new double[] { 7.42, 12.81, 87.3 },
                new double[] { 4.42, 10.21, 12.5 },
                new double[] { 8.61, 11.94, 77.5 },
                new double[] { 1.73, 13.13, 12.1 },
                new double[] { 7.47, 17.11, 86.5 },
                new double[] { 6.11, 15.13, 62.8 },
                new double[] { 1.42, 16.11, 17.5 },
            };

            // For this, we first extract the input and output
            // pairs. The first two columns have values for the
            // input variables, and the last for the output:

            double[][] inputs = example.GetColumns(new[] { 0, 1 });
            double[]   output = example.GetColumn(2);

            // We can create a new multiple linear analysis for the variables
            var mlra = new MultipleLinearRegressionAnalysis(intercept: true);

            // Compute the analysis and obtain the estimated regression
            MultipleLinearRegression regression = mlra.Learn(inputs, output);
            #endregion

            // We can also show a summary ANOVA
            // Accord.Controls.DataGridBox.Show(regression.Table);

            #region doc_learn_part2
            // And also extract other useful information, such
            // as the linear coefficients' values and std errors:
            double[] coef = mlra.CoefficientValues;
            double[] stde = mlra.StandardErrors;

            // Coefficients of performance, such as r²
            double rsquared = mlra.RSquared; // 0.62879

            // Hypothesis tests for the whole model
            ZTest ztest = mlra.ZTest; // 0.99999
            FTest ftest = mlra.FTest; // 0.01898

            // and for individual coefficients
            TTest ttest0 = mlra.Coefficients[0].TTest; // 0.00622
            TTest ttest1 = mlra.Coefficients[1].TTest; // 0.53484

            // and also extract confidence intervals
            DoubleRange ci = mlra.Coefficients[0].Confidence; // [3.2616, 14.2193]

            // We can use the analysis to predict an output for a sample
            double y = mlra.Regression.Transform(new double[] { 10, 15 });

            // We can also obtain confidence intervals for the prediction:
            DoubleRange pci = mlra.GetConfidenceInterval(new double[] { 10, 15 });

            // and also prediction intervals for the same prediction:
            DoubleRange ppi = mlra.GetPredictionInterval(new double[] { 10, 15 });
            #endregion


            Assert.AreEqual(3, coef.Length);
            Assert.AreEqual(8.7405051051757816, coef[0]);
            Assert.AreEqual(1.1198079243314365, coef[1], 1e-10);
            Assert.AreEqual(-19.604474518407862, coef[2], 1e-10);
            Assert.IsFalse(coef.HasNaN());

            Assert.AreEqual(2.375916659234715, stde[0], 1e-10);
            Assert.AreEqual(1.7268508921418664, stde[1], 1e-10);
            Assert.AreEqual(30.989640986710953, stde[2], 1e-10);
            Assert.IsFalse(coef.HasNaN());

            Assert.AreEqual(0.62879941171298936, rsquared, 1e-10);

            Assert.AreEqual(0.99999999999999822, ztest.PValue, 1e-10);
            Assert.AreEqual(0.018986050133298293, ftest.PValue, 1e-10);

            Assert.AreEqual(0.0062299844256985537, ttest0.PValue, 1e-10);
            Assert.AreEqual(0.53484850318449118, ttest1.PValue, 1e-14);
            Assert.IsFalse(Double.IsNaN(ttest1.PValue));

            Assert.AreEqual(3.2616314640800566, ci.Min, 1e-10);
            Assert.AreEqual(14.219378746271506, ci.Max, 1e-10);



            double[][]  im   = mlra.InformationMatrix;
            double      mse  = regression.GetStandardError(inputs, output);
            DoubleRange epci = regression.GetConfidenceInterval(new double[] { 10, 15 }, mse, inputs.Length, im);

            Assert.AreEqual(epci.Min, pci.Min, 1e-10);
            Assert.AreEqual(epci.Max, pci.Max, 1e-10);

            Assert.AreEqual(55.27840511658215, pci.Min, 1e-10);
            Assert.AreEqual(113.91698568006086, pci.Max, 1e-10);

            Assert.AreEqual(28.783074454641557, ppi.Min, 1e-10);
            Assert.AreEqual(140.41231634200145, ppi.Max, 1e-10);
        }
Пример #16
0
 /// <summary>
 /// Crea amb una descripció, un grup i una funció de prova.
 /// Es considera que el test ha funcionat si la funció torna true.
 /// Es considera que el test ha fallat si torna false o 
 /// </summary>
 /// <param name="descripcio">La descripció del test</param>
 /// <param name="grup">El grup a què pertanyerà aquest test</param>
 /// <param name="test">El cos de la funció de prova</param>
 public Test(string descripcio, GrupTest grup, FTest test)
     : base(descripcio, grup)
 {
     resultat = new RTest();
     try
     {
         test(resultat);
     }
     catch (Exception ex)
     {
         resultat.Error("No s'ha completat ({0}) [{1}]", ex.Message, ex.StackTrace);
     }
 }
Пример #17
0
 private void ToolStripMenuItemTestClick(object sender, EventArgs e)
 {
     var ftest = new FTest();
     ftest.ShowDialog();
 }
Пример #18
0
    private static void Main()
    //****************************************************************************80
    //
    //  Purpose:
    //
    //    test_values_test() tests test_values().
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    04 January 2019
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        Console.WriteLine("");
        Console.WriteLine("test_values_test():");
        Console.WriteLine("  Test test_values().");
        AbramsTest.abram0_values_test();
        AbramsTest.abram1_values_test();
        AbramsTest.abram2_values_test();
        AGMTest.agm_values_test();
        AiryTest.airy_ai_values_test();
        AiryTest.airy_ai_int_values_test();
        AiryTest.airy_ai_prime_values_test();
        AiryTest.airy_bi_values_test();
        AiryTest.airy_bi_int_values_test();
        AiryTest.airy_bi_prime_values_test();
        AiryTest.airy_cai_values_test();
        AiryTest.airy_cbi_values_test();
        AiryTest.airy_gi_values_test();
        AiryTest.airy_hi_values_test();
        ArcTest.arccos_values_test();
        ArcTest.arccosh_values_test();
        ArcTest.arcsin_values_test();
        ArcTest.arcsinh_values_test();
        ArcTest.arctan_values_test();
        ArcTest.arctan_int_values_test();
        ArcTest.arctanh_values_test();
        BeiBellTest.bei0_values_test();
        BeiBellTest.bei1_values_test();
        BeiBellTest.bell_values_test();
        BerTest.ber0_values_test();
        BerTest.ber1_values_test();
        BernoulliTest.bernoulli_number_values_test();
        BernoulliTest.bernoulli_poly_values_test();
        BernsteinTest.bernstein_poly_01_values_test();
        BesselTest.bessel_i0_values_test();
        BesselTest.bessel_i0_int_values_test();
        BesselTest.bessel_i0_spherical_values_test();
        BesselTest.bessel_i1_values_test();
        BesselTest.bessel_i1_spherical_values_test();
        BesselTest.bessel_in_values_test();
        BesselTest.bessel_ix_values_test();
        BesselTest.bessel_j_spherical_values_test();
        BesselTest.bessel_j0_values_test();
        BesselTest.bessel_j0_int_values_test();
        BesselTest.bessel_j0_spherical_values_test();
        BesselTest.bessel_j0_zero_values_test();
        BesselTest.bessel_j1_values_test();
        BesselTest.bessel_j1_spherical_values_test();
        BesselTest.bessel_jn_values_test();
        BesselTest.bessel_jx_values_test();
        BesselTest.bessel_k0_values_test();
        BesselTest.bessel_k0_int_values_test();
        BesselTest.bessel_k1_values_test();
        BesselTest.bessel_kn_values_test();
        BesselTest.bessel_kx_values_test();
        BesselTest.bessel_y0_values_test();
        BesselTest.bessel_y0_int_values_test();
        BesselTest.bessel_y0_spherical_values_test();
        BesselTest.bessel_y1_values_test();
        BesselTest.bessel_y1_spherical_values_test();
        BesselTest.bessel_yn_values_test();
        BesselTest.bessel_yx_values_test();
        BetaTest.beta_values_test();
        BetaTest.beta_cdf_values_test();
        BetaTest.beta_inc_values_test();
        BetaTest.beta_log_values_test();
        BetaTest.beta_noncentral_cdf_values_test();
        BetaTest.beta_pdf_values_test();
        BinomialTest.binomial_values_test();
        BinomialTest.binomial_cdf_values_test();
        BinomialTest.binomial_pdf_values_test();
        BivariateTest.bivariate_normal_cdf_values_test();
        ComplexTest.c8_log_values_test();
        CatalanTest.catalan_values_test();
        CauchyTest.cauchy_cdf_values_test();
        CubeRootTest.cbrt_values_test();
        ChebyshevTest.cheby_t_poly_values_test();
        ChebyshevTest.cheby_t01_poly_values_test();
        ChebyshevTest.cheby_u_poly_values_test();
        ChebyshevTest.cheby_u01_poly_values_test();
        ChebyshevTest.cheby_v_poly_values_test();
        ChebyshevTest.cheby_v01_poly_values_test();
        ChebyshevTest.cheby_w_poly_values_test();
        ChebyshevTest.cheby_w01_poly_values_test();
        ChiTest.chi_values_test();
        ChiTest.chi_square_cdf_values_test();
        ChiTest.chi_square_pdf_values_test();
        ChiTest.chi_square_noncentral_cdf_values_test();
        CosineTest.ci_values_test();
        CosineTest.cin_values_test();
        CosineTest.cinh_values_test();
        ClausenTest.clausen_values_test();
        ClebschGordanTest.clebsch_gordan_values_test();
        CollatzTest.collatz_count_values_test();
        CosineTest.cos_values_test();
        CosineTest.cos_degree_values_test();
        CosineTest.cos_power_int_values_test();
        CosineTest.cosh_values_test();
        CotTest.cot_values_test();
        ConstPressureTest.cp_values_test();
        DateTest.datenum_values_test();
        DawsonTest.dawson_values_test();
        DebyeTest.debye1_values_test();
        DebyeTest.debye2_values_test();
        DebyeTest.debye3_values_test();
        DebyeTest.debye4_values_test();
        DedekindTest.dedekind_sum_values_test();
        DielectricTest.dielectric_values_test();
        DilogarithmTest.dilogarithm_values_test();
        ExpIntegralTest.e1_values_test();
        DateTest.easter_gregorian_values_test();
        DateTest.easter_julian_values_test();
        ExpIntegralTest.ei_values_test();
        EllipticTest.elliptic_ea_values_test();
        EllipticTest.elliptic_ek_values_test();
        EllipticTest.elliptic_em_values_test();
        EllipticTest.elliptic_fa_values_test();
        EllipticTest.elliptic_fk_values_test();
        EllipticTest.elliptic_fm_values_test();
        EllipticTest.elliptic_inc_ea_values_test();
        EllipticTest.elliptic_inc_ek_values_test();
        EllipticTest.elliptic_inc_em_values_test();
        EllipticTest.elliptic_inc_fa_values_test();
        EllipticTest.elliptic_inc_fk_values_test();
        EllipticTest.elliptic_inc_fm_values_test();
        EllipticTest.elliptic_inc_pia_values_test();
        EllipticTest.elliptic_inc_pik_values_test();
        EllipticTest.elliptic_inc_pim_values_test();
        EllipticTest.elliptic_pia_values_test();
        EllipticTest.elliptic_pik_values_test();
        EllipticTest.elliptic_pim_values_test();
        ErrorFuncTest.erf_values_test();
        ErrorFuncTest.erfc_values_test();
        EulerTest.euler_number_values_test();
        EulerTest.euler_poly_values_test();
        ExponentialTest.exp_values_test();
        ExponentialTest.exp3_int_values_test();
        ExponentialTest.exponential_01_pdf_values_test();
        ExponentialTest.exponential_cdf_values_test();
        ExponentialTest.exponential_pdf_values_test();
        ExtremeTest.extreme_values_cdf_values_test();
        FTest.f_cdf_values_test();
        FTest.f_noncentral_cdf_values_test();
        FresnelTest.fresnel_cos_values_test();
        FresnelTest.fresnel_sin_values_test();
        FrobeniusTest.frobenius_number_data_values_test();
        FrobeniusTest.frobenius_number_order_values_test();
        FrobeniusTest.frobenius_number_order2_values_test();
        GammaTest.gamma_values_test();
        GammaTest.gamma_01_pdf_values_test();
        GammaTest.gamma_cdf_values_test();
        GammaTest.gamma_inc_values_test();
        GammaTest.gamma_inc_p_values_test();
        GammaTest.gamma_inc_q_values_test();
        GammaTest.gamma_inc_tricomi_values_test();
        GammaTest.gamma_log_values_test();
        GammaTest.gamma_pdf_values_test();
        GegenbauerTest.gegenbauer_poly_values_test();
        GeometricTest.geometric_cdf_values_test();
        GoodwinTest.goodwin_values_test();
        GudermannianTest.gud_values_test();
        HermiteTest.hermite_function_values_test();
        HermiteTest.hermite_poly_phys_values_test();
        HermiteTest.hermite_poly_prob_values_test();
        HyperTest.hyper_1f1_values_test();
        HyperTest.hyper_2f1_values_test();
        HyperTest.hypergeometric_cdf_values_test();
        HyperTest.hypergeometric_pdf_values_test();
        HyperTest.hypergeometric_u_values_test();
        ITest.i0ml0_values_test();
        ITest.i1ml1_values_test();
        FactorialTest.i4_factorial_values_test();
        FactorialTest.i4_factorial2_values_test();
        FactorialTest.i4_fall_values_test();
        FactorialTest.i4_rise_values_test();
        IntgrTest.int_values_test();
        ChiTest.inverse_chi_square_pdf_values_test();
        GammaTest.inverse_gamma_pdf_values_test();
        JacobiTest.jacobi_cn_values_test();
        JacobiTest.jacobi_dn_values_test();
        JacobiTest.jacobi_poly_values_test();
        JacobiTest.jacobi_sn_values_test();
        DateTest.jed_ce_values_test();
        DateTest.jed_mjd_values_test();
        DateTest.jed_rd_values_test();
        DateTest.jed_weekday_values_test();
        KelvinTest.kei0_values_test();
        KelvinTest.kei1_values_test();
        KelvinTest.ker0_values_test();
        KelvinTest.ker1_values_test();
        LaguerreTest.laguerre_associated_values_test();
        LaguerreTest.laguerre_general_values_test();
        LaguerreTest.laguerre_polynomial_values_test();
        LambertTest.lambert_w_values_test();
        LaplaceTest.laplace_cdf_values_test();
        LegendreTest.legendre_associated_values_test();
        LegendreTest.legendre_associated_normalized_values_test();
        LegendreTest.legendre_associated_normalized_sphere_values_test();
        LegendreTest.legendre_function_q_values_test();
        LegendreTest.legendre_normalized_polynomial_values_test();
        LegendreTest.legendre_polynomial_values_test();
        LegendreTest.legendre_shifted_polynomial_values_test();
        LerchTest.lerch_values_test();
        LobachevskyTest.lobachevsky_values_test();
        LobattoTest.lobatto_polynomial_values_test();
        LobattoTest.lobatto_polynomial_derivatives_test();
        LogarithmTest.log_values_test();
        LogarithmTest.log_normal_cdf_values_test();
        LogarithmTest.log_series_cdf_values_test();
        LogarithmTest.log10_values_test();
        LogarithmTest.logarithmic_integral_values_test();
        LogisticTest.logistic_cdf_values_test();
        MertensTest.mertens_values_test();
        MittagLefflerTest.mittag_leffler_ea_values_test();
        MoebiusTest.moebius_values_test();
        MultinomialTest.multinomial_pdf_values_test();
        BinomialTest.negative_binomial_cdf_values_test();
        WignerTest.nine_j_values_test();
        NormalTest.normal_01_cdf_values_test();
        NormalTest.normal_01_pdf_values_test();
        NormalTest.normal_cdf_values_test();
        NormalTest.normal_pdf_values_test();
        OmegaTest.omega_values_test();
        OwenTest.owen_values_test();
        PartitionTest.partition_count_values_test();
        PartitionTest.partition_distinct_count_values_test();
        PhiTest.phi_values_test();
        PiTest.pi_values_test();
        PoissionTest.poisson_cdf_values_test();
        LogarithmTest.polylogarithm_values_test();
        PolyominoTest.polyomino_chiral_count_values_test();
        PolyominoTest.polyomino_fixed_count_values_test();
        PolyominoTest.polyomino_free_count_values_test();
        PrandtlTest.prandtl_values_test();
        PrimeTest.prime_values_test();
        PsatTest.psat_values_test();
        PsiTest.psi_values_test();
        FactorialTest.r8_factorial_values_test();
        FactorialTest.r8_factorial_log_values_test();
        FactorialTest.r8_factorial2_values_test();
        FactorialTest.r8_fall_values_test();
        FactorialTest.r8_rise_values_test();
        RayleighTest.rayleigh_cdf_values_test();
        ChiTest.scaled_inverse_chi_square_pdf_values_test();
        SecVirTest.secvir_values_test();
        ShiTest.shi_values_test();
        SineTest.si_values_test();
        SigmaTest.sigma_values_test();
        SineTest.sin_values_test();
        SineTest.sin_degree_values_test();
        SineTest.sin_power_int_values_test();
        SineTest.sinh_values_test();
        WignerTest.six_j_values_test();
        SoundTest.sound_values_test();
        SphereTest.sphere_unit_area_values_test();
        SphereTest.sphere_unit_volume_values_test();
        SphericalHarmonicTest.spherical_harmonic_values_test();
        SqrtTest.sqrt_values_test();
        StirlingTest.stirling1_values_test();
        StirlingTest.stirling2_values_test();
        StromgenTest.stromgen_values_test();
        StruveTest.struve_h0_values_test();
        StruveTest.struve_h1_values_test();
        StruveTest.struve_l0_values_test();
        StruveTest.struve_l1_values_test();
        StudentTest.student_cdf_values_test();
        StudentTest.student_noncentral_cdf_values_test();
        FactorialTest.subfactorial_values_test();
        SurfTensionTest.surten_values_test();
        SynchTest.synch1_values_test();
        SynchTest.synch2_values_test();
        TangentTest.tan_values_test();
        TangentTest.tanh_values_test();
        TauTest.tau_values_test();
        ThermCondTest.thercon_values_test();
        WignerTest.three_j_values_test();
        TransportationTest.tran02_values_test();
        TransportationTest.tran03_values_test();
        TransportationTest.tran04_values_test();
        TransportationTest.tran05_values_test();
        TransportationTest.tran06_values_test();
        TransportationTest.tran07_values_test();
        TransportationTest.tran08_values_test();
        TransportationTest.tran09_values_test();
        TrigammaTest.trigamma_values_test();
        TruncatedTest.truncated_normal_ab_cdf_test();
        TruncatedTest.truncated_normal_ab_pdf_test();
        TruncatedTest.truncated_normal_a_cdf_test();
        TruncatedTest.truncated_normal_a_pdf_test();
        TruncatedTest.truncated_normal_b_cdf_test();
        TruncatedTest.truncated_normal_b_pdf_test();
        TsatTest.tsat_values_test();
        VanDerCorputTest.van_der_corput_values_test();
        ViscosityTest.viscosity_values_test();
        VonMisesTest.von_mises_cdf_values_test();
        DateTest.weekday_values_test();
        WeibullTest.weibull_cdf_values_test();
        WrightOmegaTest.wright_omega_values_test();
        ZetaTest.zeta_values_test();
        ZetaTest.zeta_m1_values_test();

        Console.WriteLine("");
        Console.WriteLine("test_values_test():");
        Console.WriteLine("  Normal end of execution.");
        Console.WriteLine("");
    }
        /// <summary>
        ///   Computes the Multiple Linear Regression Analysis.
        /// </summary>
        ///
        public void Compute()
        {
            int n = inputData.Length;
            int p = inputCount;

            SSt = SSe = outputMean = 0.0;

            // Compute the regression
            double[,] informationMatrix;
            regression.Regress(inputData, outputData,
                               out informationMatrix);


            // Calculate mean of the expected outputs
            for (int i = 0; i < outputData.Length; i++)
            {
                outputMean += outputData[i];
            }
            outputMean /= outputData.Length;

            // Calculate actual outputs (results)
            results = new double[inputData.Length];
            for (int i = 0; i < inputData.Length; i++)
            {
                results[i] = regression.Compute(inputData[i]);
            }

            // Calculate SSe and SSt
            for (int i = 0; i < inputData.Length; i++)
            {
                double d;

                d    = outputData[i] - results[i];
                SSe += d * d;

                d    = outputData[i] - outputMean;
                SSt += d * d;
            }

            // Calculate SSr
            SSr = SSt - SSe;

            // Calculate R-Squared
            rSquared = (SSt != 0) ? 1.0 - (SSe / SSt) : 1.0;

            // Calculated Adjusted R-Squared
            if (rSquared == 1)
            {
                rAdjusted = 1;
            }
            else
            {
                if (n - p == 1)
                {
                    rAdjusted = double.NaN;
                }
                else
                {
                    rAdjusted = 1.0 - (1.0 - rSquared) * ((n - 1.0) / (n - p - 1.0));
                }
            }

            // Calculate Degrees of Freedom
            DFr = p;
            DFe = n - (p + 1);
            DFt = DFr + DFe;

            // Calculate Sum of Squares Mean
            MSe = SSe / DFe;
            MSr = SSr / DFr;
            MSt = SSt / DFt;

            // Calculate the F statistic
            ftest    = new FTest(MSr / MSe, DFr, DFe);
            stdError = Math.Sqrt(MSe);


            // Create the ANOVA table
            List <AnovaVariationSource> table = new List <AnovaVariationSource>();

            table.Add(new AnovaVariationSource(this, "Regression", SSr, DFr, MSr, ftest));
            table.Add(new AnovaVariationSource(this, "Error", SSe, DFe, MSe, null));
            table.Add(new AnovaVariationSource(this, "Total", SSt, DFt, MSt, null));
            this.anovaTable = new AnovaSourceCollection(table);


            // Compute coefficient standard errors;
            standardErrors = new double[coefficientCount];
            for (int i = 0; i < standardErrors.Length; i++)
            {
                standardErrors[i] = Math.Sqrt(MSe * informationMatrix[i, i]);
            }


            // Compute coefficient tests
            for (int i = 0; i < regression.Coefficients.Length; i++)
            {
                double tStatistic = regression.Coefficients[i] / standardErrors[i];

                ttests[i] = new TTest(estimatedValue: regression.Coefficients[i],
                                      standardError: standardErrors[i], degreesOfFreedom: DFe);

                ftests[i] = new FTest(tStatistic * tStatistic, 1, DFe);

                confidences[i] = ttests[i].GetConfidenceInterval(confidencePercent);
            }


            // Compute model performance tests
            ttest         = new TTest(results, outputMean);
            ztest         = new ZTest(results, outputMean);
            chiSquareTest = new ChiSquareTest(outputData, results, n - p - 1);
        }
Пример #20
0
 /// <summary>
 /// Afegeix un test final a aquest grup.
 /// </summary>
 /// <param name="descripcio">La descripció del test final</param>
 /// <param name="test">El cos del test</param>
 public void NouTest(string descripcio, FTest test)
 {
     Test nou = new Test(descripcio, this, test);
     membres.Add(nou);
     mostrador.NouTest(nou);
 }