Ejemplo n.º 1
0
        public void ConstructorTest()
        {
            var t = new TDistribution(degreesOfFreedom: 4.2);

            double mean   = t.Mean;     // 0.0
            double median = t.Median;   // 0.0
            double var    = t.Variance; // 1.9090909090909089
            double mode   = t.Mode;

            double cdf  = t.DistributionFunction(x: 1.4);              // 0.88456136730659074
            double pdf  = t.ProbabilityDensityFunction(x: 1.4);        // 0.13894002185341031
            double lpdf = t.LogProbabilityDensityFunction(x: 1.4);     // -1.9737129364307417

            double ccdf = t.ComplementaryDistributionFunction(x: 1.4); // 0.11543863269340926
            double icdf = t.InverseDistributionFunction(p: cdf);       // 1.4000000000000012

            double hf  = t.HazardFunction(x: 1.4);                     // 1.2035833984833988
            double chf = t.CumulativeHazardFunction(x: 1.4);           // 2.1590162088918525

            string str = t.ToString(CultureInfo.InvariantCulture);     // T(x; df = 4.2)

            Assert.AreEqual(double.NegativeInfinity, t.Support.Min);
            Assert.AreEqual(double.PositiveInfinity, t.Support.Max);

            double icdf0 = t.InverseDistributionFunction(0);
            double icdf1 = t.InverseDistributionFunction(1);

            Assert.AreEqual(icdf0, t.Support.Min);
            Assert.AreEqual(icdf1, t.Support.Max);

            Assert.AreEqual(0.0, mean);
            Assert.AreEqual(0.0, median);
            Assert.AreEqual(0.0, mode);
            Assert.AreEqual(1.9090909090909089, var);
            Assert.AreEqual(2.1590162088918525, chf);
            Assert.AreEqual(0.88456136730659074, cdf);
            Assert.AreEqual(0.13894002185341031, pdf);
            Assert.AreEqual(-1.9737129364307417, lpdf);
            Assert.AreEqual(1.2035833984833988, hf);
            Assert.AreEqual(0.11543863269340926, ccdf);
            Assert.AreEqual(1.4000000000000012, icdf);
            Assert.AreEqual("T(x; df = 4.2)", str);

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

            Assert.AreEqual(-2.1030107450099362, range1.Min);
            Assert.AreEqual(2.1030107450099362, range1.Max);
            Assert.AreEqual(-3.6502571302187774, range2.Min);
            Assert.AreEqual(3.6502571302187774, range2.Max);
            Assert.AreEqual(-3.6502571302187792, range3.Min);
            Assert.AreEqual(3.6502571302187774, range3.Max);
        }
Ejemplo n.º 2
0
        private void CalculateAndWriteContrastValues()
        {
            // Calculations
            List <double> meanValues = new List <double>();
            List <double> effects    = new List <double>();

            double[][] measurements = GetMeasurements();
            double     SSE = 0.0, Se2, t = 0.0;

            // Get alternative's measurements and calculate their mean value
            for (int i = 0; i < measurements.Length; ++i)
            {
                meanValues.Add(measurements[i].Average());
            }

            // Calculate total mean value
            double totalMeanValue = measurements.Select(x => x.Sum()).Sum() / (MeasurementsCount * AlternativesCount);

            // Calculate effects
            foreach (double meanValue in meanValues)
            {
                effects.Add(meanValue - totalMeanValue);
            }
            if (!effects.Any(x => x != 0.0))
            {
                WriteMessage("Enter measurements.");
                return;
            }
            for (int i = 0; i < AlternativesCount; ++i)
            {
                for (int j = 0; j < MeasurementsCount; ++j)
                {
                    SSE += Math.Pow(measurements[i][j] - meanValues[i], 2);
                }
            }
            Se2 = SSE / (AlternativesCount * (MeasurementsCount - 1));

            double Sc = Math.Sqrt(Se2 * 2 / (MeasurementsCount * AlternativesCount));

            if (string.IsNullOrEmpty(PercentageTextBox.Text))
            {
                WriteMessage("Percentage must be specified.");
                return;
            }
            double percentage = double.Parse(PercentageTextBox.Text);

            if (percentage >= 1.0 || percentage < 0.0 || percentage == 0.5)
            {
                WriteMessage("Percentage must be in correct format (decimal form). Must not be 100%(1.0) or 50%(0.5).");
                return;
            }

            // Find interval around Sc
            int    degOfFreedom     = AlternativesCount * (MeasurementsCount - 1);
            double percentageNumber = 1 - (1 - percentage) / 2;

            if (MeasurementsCount <= 30)
            {
                TDistribution distribution = new TDistribution(degOfFreedom);
                t = distribution.GetRange(percentageNumber).Max;
            }
            else
            {
                NormalDistribution distribution = new NormalDistribution();
                t = distribution.GetRange(percentageNumber).Max;
            }
            double constantFactor = t * Sc;
            int    index          = 1;

            for (int i = 0; i < AlternativesCount; ++i)
            {
                for (int j = i + 1; j < AlternativesCount; ++j)
                {
                    double c = effects[i] - effects[j];
                    ((TextBlock)ContrastsGrid.Children.Cast <UIElement>().First(e => Grid.GetRow(e) == 1 && Grid.GetColumn(e) == index)).Text =
                        (c - constantFactor) < 0 && (c + constantFactor) >= 0 ? "Statistically the same." : "Statistically different.";
                    index++;
                }
            }
        }