public void ChiSquareDistribution()
        {
            int B = 8;
            Random rng = new Random(0);
            //BinomialDistribution d = new BinomialDistribution(0.25, 6);
            //DiscreteUniformDistribution d = new DiscreteUniformDistribution(0, 4);
            //BernoulliDistribution d = new BernoulliDistribution(0.25);
            DiscreteDistribution d = new PoissonDistribution(2.0);

            Sample s = new Sample();
            ChiSquaredDistribution nullDistribution = null;
            for (int i = 0; i < 512; i++) {

                Histogram h = new Histogram(B);
                for (int j = 0; j < 1024; j++) {
                    int k = d.GetRandomValue(rng);
                    h.Add(k);
                    //if (k < h.Count) h[k].Increment();
                    //h[d.GetRandomValue(rng)].Increment();
                }
                TestResult cr = h.ChiSquaredTest(d);
                nullDistribution = (ChiSquaredDistribution) cr.Distribution;
                //Console.WriteLine(((ChiSquaredDistribution) cr.Distribution).DegreesOfFreedom);
                s.Add(cr.Statistic);

            }

            Console.WriteLine(nullDistribution.DegreesOfFreedom);
            TestResult kr = s.KuiperTest(nullDistribution);
            Console.WriteLine(kr.LeftProbability);
        }
        public void TwoSampleKS2()
        {
            int n = 2 * 3 * 3; int m = 2 * 2 * 3;
            Random rng = new Random(0);
            NormalDistribution d = new NormalDistribution();

            Histogram h = new Histogram((int) AdvancedIntegerMath.LCM(n, m) + 1);

            //int[] h = new int[AdvancedIntegerMath.LCM(n, m) + 1];

            int count = 1000;
            for (int i = 0; i < count; i++) {

                Sample A = new Sample();
                for (int j = 0; j < n; j++) A.Add(d.GetRandomValue(rng));
                Sample B = new Sample();
                for (int j = 0; j < m; j++) B.Add(d.GetRandomValue(rng));

                TestResult r = Sample.KolmogorovSmirnovTest(A, B);
                int k = (int) Math.Round(r.Statistic * AdvancedIntegerMath.LCM(n, m));
                //Console.WriteLine("{0} {1}", r.Statistic, k);
                h[k].Increment();
                //h[k] = h[k] + 1;

            }

            KolmogorovTwoSampleExactDistribution ks = new KolmogorovTwoSampleExactDistribution(n, m);
            double chi2 = 0.0; int dof = 0;
            for (int i = 0; i < h.Count; i++) {
                double ne = ks.ProbabilityMass(i) * count;
                Console.WriteLine("{0} {1} {2}", i, h[i].Counts, ne);
                if (ne > 4) {
                    chi2 += MoreMath.Sqr(h[i].Counts - ne) / ne;
                    dof++;
                }
            }
            Console.WriteLine("chi^2={0} dof={1}", chi2, dof);

            TestResult r2 = h.ChiSquaredTest(ks);
            ChiSquaredDistribution rd = (ChiSquaredDistribution) r2.Distribution;
            Console.WriteLine("chi^2={0} dof={1} P={2}", r2.Statistic, rd.DegreesOfFreedom, r2.RightProbability);
        }
Exemple #3
0
        /// <summary>
        /// Meetod kõigi sisendite (ehk eelduste) välja arvutamiseks
        /// </summary>
        /// <param name="finDataAdapter">Adapter kõigi finantsandmete kättesaamise jaoks</param>
        /// <param name="dcfInput">DCF arvutuste eelduste hoidja</param>
        public static void CalculateInput(FinDataAdapter finDataAdapter, DcfInput dcfInput)
        {
            CalculateCorrelation(finDataAdapter, dcfInput);
            int requiredPeriodsForMean = 1;
            List<FinData> finDatas = finDataAdapter.FinDataDao.FinDatas;
            int k = 0;
            Sample sampleRevenue = new Sample();
            Sample sampleTax = new Sample();
            Sample sampleInterest = new Sample();

            Sample sampleTotalAssets = new Sample();
            Sample sampleTotalLiabilities = new Sample();
            Sample sampleTotalCurrentAssets = new Sample();
            Sample sampleTotalCurrentLiabilities = new Sample();
            Sample sampleAllCosts = new Sample();
            Sample sampleEbitda = new Sample();
            Sample sampleDepreciation = new Sample();
            Sample sampleEbit = new Sample();

            for (int i = finDatas.Count - 1; i >= 4; i--)
            {
                if (k < 12)
                {
                    if (k == 0)
                    {
                        try
                        {
                            dcfInput.SharesOutstanding = (double)finDatas[i].BsCommonSharesOutstanding;
                        }
                        catch (InvalidOperationException) { }
                    }
                    try
                    {
                        double RevenueGrowth = (double)(finDatas[i].IsRevenue / finDatas[i - 4].IsRevenue) - 1;
                        sampleRevenue.Add(RevenueGrowth);
                    }
                    catch (InvalidOperationException) { }

                    try
                    {
                        double taxRate = (double)((finDatas[i].IsPretaxIncome - finDatas[i].IsIncomeAfterTax) / finDatas[i].IsPretaxIncome);
                        sampleTax.Add(taxRate);
                    }
                    catch (InvalidOperationException) { }

                    double debt = 0;
                    if (finDatas[i].BsCurrentPortionOfLongTermDebt != null)
                    {
                        debt += (double)finDatas[i].BsCurrentPortionOfLongTermDebt;
                    }
                    if (finDatas[i].BsTotalLongTermDebt != null)
                    {
                        debt += (double)finDatas[i].BsTotalLongTermDebt;
                    }
                    if (finDatas[i].FrInterestExpense != null && debt != 0.0)
                    {
                        double interest = (double)(finDatas[i].FrInterestExpense) / debt;
                        if (interest < 0.2)
                        {
                            sampleInterest.Add(interest);
                        }
                    }

                    if (finDatas[i].IsRevenue != null && finDatas[i].IsRevenue != 0.0 && finDatas[i].BsTotalAssets != null)
                    {
                        double temp = (double)(finDatas[i].BsTotalAssets / finDatas[i].IsRevenue);
                        if (temp > 0.5 && temp < 50)
                        {
                            sampleTotalAssets.Add(temp);
                        }
                    }
                    if (finDatas[i].IsRevenue != null && finDatas[i].IsRevenue != 0.0 && finDatas[i].BsTotalLiabilities != null)
                    {
                        double temp = (double)(finDatas[i].BsTotalLiabilities / finDatas[i].IsRevenue);
                        if (temp > 0.5 && temp < 50)
                        {
                            sampleTotalLiabilities.Add(temp);
                        }
                    }

                    if (finDatas[i].IsRevenue != null && finDatas[i].IsRevenue != 0.0 && finDatas[i].BsTotalCurrentAssets != null)
                    {
                        double temp = (double)(finDatas[i].BsTotalCurrentAssets / finDatas[i].IsRevenue);
                        if (temp > 0.5 && temp < 50)
                        {
                            sampleTotalCurrentAssets.Add(temp);
                        }
                    }
                    if (finDatas[i].IsRevenue != null && finDatas[i].IsRevenue != 0.0 && finDatas[i].BsTotalCurrentLiabilities != null)
                    {
                        double temp = (double)(finDatas[i].BsTotalCurrentLiabilities / finDatas[i].IsRevenue);
                        if (temp > 0.5 && temp < 50)
                        {
                            sampleTotalCurrentLiabilities.Add(temp);
                        }
                    }

                    double allCosts = 0.0;
                    if (finDatas[i].IsTotalOperatingExpenses != null)
                    {
                        allCosts += (double)finDatas[i].IsTotalOperatingExpenses;
                    }
                    if (finDatas[i].IsDepreciationAmortization != null)
                    {
                        allCosts -= (double)finDatas[i].IsDepreciationAmortization;
                    }
                    if (finDatas[i].IsRevenue != null && finDatas[i].IsRevenue != 0.0 && allCosts != 0.0)
                    {
                        double temp = (double)(allCosts / finDatas[i].IsRevenue);
                        if (temp > 0.1 && temp < 1)
                        {
                            sampleAllCosts.Add(temp);
                        }
                    }

                    if (finDatas[i].IsRevenue != null && finDatas[i].IsRevenue != 0.0 && finDatas[i].FrEbitda != null)
                    {
                        double temp = (double)(finDatas[i].FrEbitda / finDatas[i].IsRevenue);
                        if (temp > -1.0 && temp < 1.0)
                        {
                            sampleEbitda.Add(temp);
                        }
                    }

                    if (finDatas[i].IsRevenue != null && finDatas[i].IsRevenue != 0.0 && finDatas[i].IsDepreciationAmortization != null)
                    {
                        double temp = (double)(finDatas[i].IsDepreciationAmortization / finDatas[i].IsRevenue);
                        if (temp > -1.0 && temp < 1.0)
                        {
                            sampleDepreciation.Add(temp);
                        }
                    }
                    if (finDatas[i].IsRevenue != null && finDatas[i].IsRevenue != 0.0 && finDatas[i].FrEbit != null)
                    {
                        double temp = (double)(finDatas[i].FrEbit / finDatas[i].IsRevenue);
                        if (temp > -1.0 && temp < 1.0)
                        {
                            sampleEbit.Add(temp);
                        }
                    }

                }

                k++;
            }

            if (sampleRevenue.Count >= requiredPeriodsForMean)
            {
                dcfInput.GrowthRatePrognosis = sampleRevenue.Mean;
            }
            if (sampleTax.Count >= requiredPeriodsForMean)
            {
                dcfInput.TaxRate = sampleTax.Mean;
            }
            if (sampleInterest.Count >= requiredPeriodsForMean)
            {
                dcfInput.CostOfDebt = sampleInterest.Mean;
            }

            if (sampleTotalAssets.Count >= requiredPeriodsForMean)
            {
                dcfInput.TotalAssetsPrcRevenue = sampleTotalAssets.Mean;
            }
            if (sampleTotalLiabilities.Count >= requiredPeriodsForMean)
            {
                dcfInput.TotalLiabilitiesPrcRevenue = sampleTotalLiabilities.Mean;
            }
            if (sampleTotalCurrentAssets.Count >= requiredPeriodsForMean)
            {
                dcfInput.TotalCurrentAssetsPrcRevenue = sampleTotalCurrentAssets.Mean;
            }
            if (sampleTotalCurrentLiabilities.Count >= requiredPeriodsForMean)
            {
                dcfInput.TotalCurrentLiabilitiesPrcRevenue = sampleTotalCurrentLiabilities.Mean;
            }
            if (sampleAllCosts.Count >= requiredPeriodsForMean)
            {
                dcfInput.AllCostsPrcRevenue = sampleAllCosts.Mean;
            }
            if (sampleEbitda.Count >= requiredPeriodsForMean)
            {
                dcfInput.EbitdaPrcRevenue = sampleEbitda.Mean;
            }
            if (sampleDepreciation.Count >= requiredPeriodsForMean)
            {
                dcfInput.DepreciationPrcRevenue = sampleDepreciation.Mean;
            }
            if (sampleEbit.Count >= requiredPeriodsForMean)
            {
                dcfInput.EbitPrcRevenue = sampleEbit.Mean;
            }
        }
        /// <summary>
        /// Computes the polynomial of given degree which best fits the data.
        /// </summary>
        /// <param name="m">The degree, which must be non-negative.</param>
        /// <returns>The fit result.</returns>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="m"/> is negative.</exception>
        /// <exception cref="InsufficientDataException">There are fewer data points than coefficients to be fit.</exception>
        public PolynomialRegressionResult PolynomialRegression(int m)
        {
            if (m < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(m));
            }

            int n = Count;

            if (n < (m + 1))
            {
                throw new InsufficientDataException();
            }

            // Construct the n X m design matrix X_{ij} = x_{i}^{j}
            RectangularMatrix X = new RectangularMatrix(n, m + 1);
            ColumnVector      y = new ColumnVector(n);

            for (int i = 0; i < n; i++)
            {
                double x = xData[i];
                X[i, 0] = 1.0;
                for (int j = 1; j <= m; j++)
                {
                    X[i, j] = X[i, j - 1] * x;
                }
                y[i] = yData[i];
            }

            // Use X = QR to solve X b = y and compute C
            ColumnVector    b;
            SymmetricMatrix C;

            QRDecomposition.SolveLinearSystem(X, y, out b, out C);

            // Compute residuals
            double       SSR       = 0.0;
            double       SSF       = 0.0;
            ColumnVector yHat      = X * b;
            Sample       residuals = new Sample();

            for (int i = 0; i < n; i++)
            {
                double z = yData[i] - yHat[i];
                residuals.Add(z);
                SSR += z * z;
                SSF += MoreMath.Sqr(yHat[i] - yData.Mean);
            }
            double sigma2 = SSR / (n - (m + 1));

            // Scale up C by \sigma^2
            // (It sure would be great to be able to overload *=.)
            for (int i = 0; i <= m; i++)
            {
                for (int j = i; j <= m; j++)
                {
                    C[i, j] = C[i, j] * sigma2;
                }
            }

            // Compute remaing sums-of-squares
            double SST = yData.Variance * n;

            // Use sums-of-squares to do ANOVA
            AnovaRow          fit      = new AnovaRow(SSF, m);
            AnovaRow          residual = new AnovaRow(SSR, n - (m + 1));
            AnovaRow          total    = new AnovaRow(SST, n - 1);
            OneWayAnovaResult anova    = new OneWayAnovaResult(fit, residual, total);

            string[] names = new string[m + 1];
            names[0] = "1";
            if (m > 0)
            {
                names[1] = "x";
            }
            for (int i = 2; i <= m; i++)
            {
                names[i] = $"x^{i}";
            }
            ParameterCollection parameters = new ParameterCollection(names, b, C);

            return(new PolynomialRegressionResult(parameters, anova, residuals));
        }
        /// <summary>
        /// Computes the best-fit linear regression from the data.
        /// </summary>
        /// <returns>The result of the fit.</returns>
        /// <remarks>
        /// <para>Linear regression assumes that the data have been generated by a function y = a + b x + e, where e is
        /// normally distributed noise, and determines the values of a and b that best fit the data. It also
        /// determines an error matrix on the parameters a and b, and does an F-test to</para>
        /// <para>The fit result is two-dimensional. The first parameter is the intercept a, the second is the slope b.
        /// The goodness-of-fit test is a F-test comparing the variance accounted for by the model to the remaining,
        /// unexplained variance.</para>
        /// </remarks>
        /// <exception cref="InsufficientDataException">There are fewer than three data points.</exception>
        public LinearRegressionResult LinearRegression()
        {
            int n = this.Count;

            if (n < 3)
            {
                throw new InsufficientDataException();
            }

            // The means and covariances are the inputs to most of the regression formulas.
            double mx  = xData.Mean;
            double my  = yData.Mean;
            double cxx = xData.Variance;
            double cyy = yData.Variance;
            double cxy = this.Covariance;

            Debug.Assert(cxx >= 0.0);
            Debug.Assert(cyy >= 0.0);

            // Compute the best-fit parameters
            double b = cxy / cxx;
            double a = my - b * mx;
            // Since cov(x,y) = (n S_xy - S_x S_y)/n^2 and var(x) = (n S_xx - S_x^2) / n^2,
            // these formulas are equivilent to the
            // to the usual formulas for a and b involving sums, but it is more stable against round-off
            ColumnVector v = new ColumnVector(a, b);

            v.IsReadOnly = true;

            // Compute Pearson r value
            double     r     = cxy / Math.Sqrt(cxx * cyy);
            TestResult rTest = new TestResult("r", r, TestType.TwoTailed, new Distributions.PearsonRDistribution(n));

            // Compute residuals and other sum-of-squares
            double SSR       = 0.0;
            double SSF       = 0.0;
            Sample residuals = new Sample();

            foreach (XY point in this)
            {
                double y = a + b * point.X;
                double z = point.Y - y;
                SSR += z * z;
                residuals.Add(z);
                SSF += MoreMath.Sqr(y - my);
            }
            double SST = cyy * n;
            // Note SST = SSF + SSR because \sum_{i} ( y_i - \bar{y})^2 = \sum_i (y_i - f_i)^2 + \sum_i (f_i - \bar{y})^2

            // Use sums-of-squares to do ANOVA
            AnovaRow          fit      = new AnovaRow(SSF, 1);
            AnovaRow          residual = new AnovaRow(SSR, n - 2);
            AnovaRow          total    = new AnovaRow(SST, n - 1);
            OneWayAnovaResult anova    = new OneWayAnovaResult(fit, residual, total);

            // Compute covariance of parameters matrix
            double s2  = SSR / (n - 2);
            double cbb = s2 / cxx / n;
            double cab = -mx * cbb;
            double caa = (cxx + mx * mx) * cbb;

            SymmetricMatrix C = new SymmetricMatrix(2);

            C[0, 0]      = caa;
            C[1, 1]      = cbb;
            C[0, 1]      = cab;
            C.IsReadOnly = true;

            // Package the parameters
            ParameterCollection parameters = new ParameterCollection(
                new string[] { "Intercept", "Slope" }, v, C
                );

            // Prepare the prediction function
            Func <double, UncertainValue> predict = (double x) => {
                double y = a + b * x;
                return(new UncertainValue(y, Math.Sqrt(s2 * (1.0 + (1.0 + MoreMath.Sqr(x - mx) / cxx) / n))));
            };

            return(new LinearRegressionResult(parameters, rTest, anova, residuals, predict));
        }
        // the internal linear regression routine, which assumes inputs are entirely valid

        private MultiLinearRegressionResult LinearRegression_Internal(int outputIndex)
        {
            // To do a fit, we need more data than parameters.
            if (Count < Dimension)
            {
                throw new InsufficientDataException();
            }

            // Compute the design matrix X.
            int n = Count;
            int m = Dimension;
            RectangularMatrix X = new RectangularMatrix(n, m);
            ColumnVector      y = new ColumnVector(n);

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (j == outputIndex)
                    {
                        X[i, j] = 1.0;
                    }
                    else
                    {
                        X[i, j] = storage[j][i];
                    }
                }
                y[i] = storage[outputIndex][i];
            }

            // Use X = QR to solve X b = y and compute C.
            ColumnVector    b;
            SymmetricMatrix C;

            QRDecomposition.SolveLinearSystem(X, y, out b, out C);

            // Compute residuals
            double       SSR       = 0.0;
            double       SSF       = 0.0;
            ColumnVector yHat      = X * b;
            Sample       residuals = new Sample();

            for (int i = 0; i < n; i++)
            {
                double z = storage[outputIndex][i] - yHat[i];
                residuals.Add(z);
                SSR += z * z;
                SSF += MoreMath.Sqr(yHat[i] - storage[outputIndex].Mean);
            }
            double sigma2 = SSR / (n - m);


            // Scale up C by \sigma^2
            // (It sure would be great to be able to overload *=.)
            for (int i = 0; i < m; i++)
            {
                for (int j = i; j < m; j++)
                {
                    C[i, j] = C[i, j] * sigma2;
                }
            }

            // Compute remaing sums-of-squares
            double SST = storage[outputIndex].Variance * n;

            // Use sums-of-squares to do ANOVA
            AnovaRow          fit      = new AnovaRow(SSF, m - 1);
            AnovaRow          residual = new AnovaRow(SSR, n - m);
            AnovaRow          total    = new AnovaRow(SST, n - 1);
            OneWayAnovaResult anova    = new OneWayAnovaResult(fit, residual, total);

            string[] names = new string[m];
            for (int j = 0; j < m; j++)
            {
                if (j == outputIndex)
                {
                    names[j] = "Intercept";
                }
                else
                {
                    names[j] = $"[{j}]";
                }
            }
            ParameterCollection parameters = new ParameterCollection(names, b, C);

            return(new MultiLinearRegressionResult(parameters, anova, residuals));
        }