Ejemplo n.º 1
0
        public List<Correlation> FindCorrelationsViaLinearRegression(MultivariateSample sample, int columns)
        {
            var fitResult = sample.LinearRegression(0);

            var correlations = new List<Correlation>();

            // The two loops are for going across every column:column combination
            for (var i = 0; i < columns; i++)
            {
                for (var j = i + 1; j < columns; j++)
                {
                    var coefficient = fitResult.CorrelationCoefficient(i, j);

                    // REVISIT: Can pass in correlation thresholds through configuration
                    if (coefficient >= 0.55 || coefficient <= -0.55)
                    {
                        correlations.Add(new Correlation
                        {
                            Coefficient = coefficient,
                            ColumnA = i,
                            ColumnB = j,
                            CorrelationType = coefficient >= 0.8 || coefficient <= -0.8
                                ? CorrelationType.Significant
                                : CorrelationType.Borderline
                        });
                    }
                }
            }

            return correlations;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Korrelatsiooni arvutus valitud näitajate jaoks (Total Assets, Total Liabilities, Total Current Assets, Total Current Liabilities: vs Reveneue)
        /// </summary>
        /// <param name="finDataAdapter">Adapter kõigi finantsandmete kättesaamise jaoks</param>
        /// <param name="dcfInput">DCF arvutuste eelduste hoidja</param>
        public static void CalculateCorrelation(FinDataAdapter finDataAdapter, DcfInput dcfInput)
        {
            MultivariateSample mvTA = new MultivariateSample(2);
            MultivariateSample mvTL = new MultivariateSample(2);
            MultivariateSample mvCA = new MultivariateSample(2);
            MultivariateSample mvCL = new MultivariateSample(2);
            int k = 0;
            List<FinData> finDatas = finDataAdapter.FinDataDao.FinDatas;
            for (int i = finDataAdapter.FinDataDao.FinDatas.Count - 1; i >= 0; i--)
            {
                if (k < 20)
                {
                    double revChange = 0;
                    try
                    {
                        //revChange = (double) (finDatas[i].IsRevenue/finDatas[i - 5].IsRevenue);
                        revChange = (double)finDatas[i].IsRevenue;
                    }
                    catch (InvalidOperationException) { }
                    double caChange = 0;
                    try
                    {
                        //secChange = (double)(finDatas[i].BsTotalCurrentAssets / finDatas[i - 5].BsTotalCurrentAssets);
                        caChange = (double)finDatas[i].BsTotalCurrentAssets;
                    }
                    catch (InvalidOperationException) { }

                    double clChange = 0;
                    try
                    {
                        //secChange = (double)(finDatas[i].BsTotalCurrentAssets / finDatas[i - 5].BsTotalCurrentAssets);
                        clChange = (double)finDatas[i].BsTotalCurrentLiabilities;
                    }
                    catch (InvalidOperationException) { }
                    double taChange = 0;
                    try
                    {
                        //secChange = (double)(finDatas[i].BsTotalCurrentAssets / finDatas[i - 5].BsTotalCurrentAssets);
                        taChange = (double)finDatas[i].BsTotalAssets;
                    }
                    catch (InvalidOperationException) { }
                    double tlChange = 0;
                    try
                    {
                        //secChange = (double)(finDatas[i].BsTotalCurrentAssets / finDatas[i - 5].BsTotalCurrentAssets);
                        tlChange = (double)finDatas[i].BsTotalLiabilities;
                    }
                    catch (InvalidOperationException) { }

                    //MessageBox.Show("s:" + ((double)(prevPrice / curPrice) - 1));
                    //MessageBox.Show("i:" + ((double)(prevIndex / curIndex) - 1));
                    ////bivariate.Add((double) (prevPrice/curPrice)-1,(double) (prevIndex/curIndex)-1);
                    double[] db = new double[2];
                    db[0] = caChange;
                    db[1] = revChange;
                    if (db[0] != 0 || db[1] != 0)
                        mvCA.Add(db);

                    db = new double[2];
                    db[0] = clChange;
                    db[1] = revChange;
                    if (db[0] != 0 || db[1] != 0)
                        mvCL.Add(db);

                    db = new double[2];
                    db[0] = taChange;
                    db[1] = revChange;
                    if (db[0] != 0 || db[1] != 0)
                        mvTA.Add(db);

                    db = new double[2];
                    db[0] = tlChange;
                    db[1] = revChange;
                    if (db[0] != 0 || db[1] != 0)
                        mvTL.Add(db);

                    //DateTime dt = finDataAdapter.PriceDataDao.PriceDatas[i].PriceDate;

                    //MessageBox.Show(finDataAdapter.PriceDataDao.PriceDatas[i].AdjClose + " " +
                    //                dt.ToShortDateString());
                    //MessageBox.Show(finDataAdapter.PriceDataDao.GetClosePrice(dt, finDataAdapter.PriceDataDao.IndexDatas)[0].ToString());
                }
                k++;
            }

            // peab vähemalt olema 3 vaatlust
            if (mvCA.Count > 2)
            {
                //FitResult fitResult = bivariate.LinearRegression();

                FitResult fitResult = mvCA.LinearRegression(0);

                dcfInput.TotalCurrentAssetsBeta = fitResult.Parameter(1).Value;
                dcfInput.TotalCurrentAssetsAlpha = fitResult.Parameter(0).Value;

                fitResult = mvCL.LinearRegression(0);
                dcfInput.TotalCurrentLiabilitiesBeta = fitResult.Parameter(1).Value;
                dcfInput.TotalCurrentLiabilitiesAlpha = fitResult.Parameter(0).Value;

                fitResult = mvTA.LinearRegression(0);
                dcfInput.TotalAssetsBeta = fitResult.Parameter(1).Value;
                dcfInput.TotalAssetsAlpha = fitResult.Parameter(0).Value;

                fitResult = mvTL.LinearRegression(0);
                dcfInput.TotalLiabilitiesBeta = fitResult.Parameter(1).Value;
                dcfInput.TotalLiabilitiesAlpha = fitResult.Parameter(0).Value;

                //MessageBox.Show("alfa: " + fitResult.Parameter(0).Value.ToString());
                //MessageBox.Show("beta: "+fitResult.Parameter(1).Value.ToString());
                //double[] pars = fitResult.Parameters();
                //foreach (var par in pars)
                //{
                //    MessageBox.Show(par.ToString());

                //}

                //MessageBox.Show(fitResult.CorrelationCoefficient(0,1).ToString());
                //double[] gfit = fitResult.
                //MessageBox.Show(fitResult.);

                //MessageBox.Show(fitResult.Parameter(2).ToString());
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Beta arvutus
        /// </summary>
        /// <param name="finDataAdapter">Adapter kõigi finantsandmete kättesaamise jaoks</param>
        /// <param name="dcfInput">DCF arvutuste eelduste hoidja</param>
        public static void CalculateBeta(FinDataAdapter finDataAdapter, DcfInput dcfInput)
        {
            BivariateSample bivariate = new BivariateSample();
            MultivariateSample mv = new MultivariateSample(2);
            decimal prevPrice = 0;
            double? prevIndex = null;
            decimal curPrice = 0;
            double? curIndex = null;
            int k = 0;
            for (int i = 0; i < finDataAdapter.PriceDataDao.PriceDatas.Count; i = i + 22)
            {
                if (k < 36)
                {
                    PriceData pd = finDataAdapter.PriceDataDao.PriceDatas[i];
                    curPrice = pd.AdjClose;
                    curIndex = finDataAdapter.PriceDataDao.GetClosePrice(pd.PriceDate, finDataAdapter.PriceDataDao.IndexDatas)[0];
                    if (curPrice != 0 && curIndex != null && prevPrice != 0 && prevIndex != null)
                    {
                        //MessageBox.Show("s:" + ((double)(prevPrice / curPrice) - 1));
                        //MessageBox.Show("i:" + ((double)(prevIndex / curIndex) - 1));
                        ////bivariate.Add((double) (prevPrice/curPrice)-1,(double) (prevIndex/curIndex)-1);
                        double[] db = new double[2];
                        db[0] = ((double)(prevPrice / curPrice) - 1);
                        db[1] = ((double)(prevIndex / curIndex) - 1);
                        mv.Add(db);
                    }
                    prevPrice = curPrice;
                    prevIndex = curIndex;

                    //DateTime dt = finDataAdapter.PriceDataDao.PriceDatas[i].PriceDate;

                    //MessageBox.Show(finDataAdapter.PriceDataDao.PriceDatas[i].AdjClose + " " +
                    //                dt.ToShortDateString());
                    //MessageBox.Show(finDataAdapter.PriceDataDao.GetClosePrice(dt, finDataAdapter.PriceDataDao.IndexDatas)[0].ToString());
                }
                k++;
            }

            if (mv.Count > 10)
            {
                //FitResult fitResult = bivariate.LinearRegression();
                FitResult fitResult = mv.LinearRegression(0);
                dcfInput.Beta = fitResult.Parameter(1).Value;
                List<FinData> finDatas = finDataAdapter.FinDataDao.FinDatas;

                dcfInput.CostOfEquity = dcfInput.RiskFreeRate + dcfInput.Beta * dcfInput.MarketRiskPremium;
                double debt = 0;
                if (finDatas[finDatas.Count - 1].BsCurrentPortionOfLongTermDebt != null)
                {
                    debt += (double)finDatas[finDatas.Count - 1].BsCurrentPortionOfLongTermDebt;
                }
                if (finDatas[finDatas.Count - 1].BsTotalLongTermDebt != null)
                {
                    debt += (double)finDatas[finDatas.Count - 1].BsTotalLongTermDebt;
                }
                double total = 0.0;
                if (finDatas[finDatas.Count - 1].BsShareholdersEquity1 != null)
                {
                    total += (double)(finDatas[finDatas.Count - 1].BsShareholdersEquity1);
                }
                total += debt;

                try
                {
                    dcfInput.Wacc = dcfInput.CostOfEquity *
                                    (double)(finDatas[finDatas.Count - 1].BsShareholdersEquity1 / total) +
                                    dcfInput.CostOfDebt * (double)(debt / total) * (1 - dcfInput.TaxRate);
                }
                catch (InvalidOperationException) { }
                //MessageBox.Show("beta: "+fitResult.Parameter(1).Value.ToString());
                //double[] pars = fitResult.Parameters();
                //foreach (var par in pars)
                //{
                //    MessageBox.Show(par.ToString());

                //}

                //MessageBox.Show(fitResult.CorrelationCoefficient(0,1).ToString());
                //double[] gfit = fitResult.
                //MessageBox.Show(fitResult.);

                //MessageBox.Show(fitResult.Parameter(2).ToString());
            }
        }
        public void MultivariateLinearRegressionAgreement()
        {
            Random rng = new Random(1);

            MultivariateSample SA = new MultivariateSample(2);
            for (int i = 0; i < 10; i++) {
                SA.Add(rng.NextDouble(), rng.NextDouble());
            }
            FitResult RA = SA.LinearRegression(0);
            ColumnVector PA = RA.Parameters;
            SymmetricMatrix CA = RA.CovarianceMatrix;

            MultivariateSample SB = SA.Columns(1, 0);
            FitResult RB = SB.LinearRegression(1);
            ColumnVector PB = RB.Parameters;
            SymmetricMatrix CB = RB.CovarianceMatrix;

            Assert.IsTrue(TestUtilities.IsNearlyEqual(PA[0], PB[1])); Assert.IsTrue(TestUtilities.IsNearlyEqual(PA[1], PB[0]));
            Assert.IsTrue(TestUtilities.IsNearlyEqual(CA[0, 0], CB[1, 1])); Assert.IsTrue(TestUtilities.IsNearlyEqual(CA[0, 1], CB[1, 0])); Assert.IsTrue(TestUtilities.IsNearlyEqual(CA[1, 1], CB[0, 0]));
            Assert.IsTrue(TestUtilities.IsNearlyEqual(RA.GoodnessOfFit.Statistic, RB.GoodnessOfFit.Statistic));

            BivariateSample SC = SA.TwoColumns(1, 0);
            FitResult RC = SC.LinearRegression();
            ColumnVector PC = RC.Parameters;
            SymmetricMatrix CC = RC.CovarianceMatrix;

            Assert.IsTrue(TestUtilities.IsNearlyEqual(PA, PC));
            Assert.IsTrue(TestUtilities.IsNearlyEqual(CA, CC));
            Assert.IsTrue(TestUtilities.IsNearlyEqual(RA.GoodnessOfFit.Statistic, RC.GoodnessOfFit.Statistic));
        }
        public void OldMultivariateLinearRegressionTest()
        {
            MultivariateSample sample = new MultivariateSample(3);

            sample.Add(98322, 81449, 269465);
            sample.Add(65060, 31749, 121900);
            sample.Add(36052, 14631, 37004);
            sample.Add(31829, 27732, 91400);
            sample.Add(7101, 9693, 54900);
            sample.Add(41294, 4268, 16160);
            sample.Add(16614, 4697, 21500);
            sample.Add(3449, 4233, 9306);
            sample.Add(3386, 5293, 38300);
            sample.Add(6242, 2039, 13369);
            sample.Add(14036, 7893, 29901);
            sample.Add(2636, 3345, 10930);
            sample.Add(869, 1135, 5100);
            sample.Add(452, 727, 7653);

            /*
            sample.Add(41.9, 29.1, 251.3);
            sample.Add(43.4, 29.3, 251.3);
            sample.Add(43.9, 29.5, 248.3);
            sample.Add(44.5, 29.7, 267.5);
            sample.Add(47.3, 29.9, 273.0);
            sample.Add(47.5, 30.3, 276.5);
            sample.Add(47.9, 30.5, 270.3);
            sample.Add(50.2, 30.7, 274.9);
            sample.Add(52.8, 30.8, 285.0);
            sample.Add(53.2, 30.9, 290.0);
            sample.Add(56.7, 31.5, 297.0);
            sample.Add(57.0, 31.7, 302.5);
            sample.Add(63.5, 31.9, 304.5);
            sample.Add(65.3, 32.0, 309.3);
            sample.Add(71.1, 32.1, 321.7);
            sample.Add(77.0, 32.5, 330.7);
            sample.Add(77.8, 32.9, 349.0);
            */

            Console.WriteLine(sample.Count);

            //sample.LinearRegression(0);
            sample.LinearRegression(0);
        }
        public void MultivariateLinearRegressionTest()
        {
            // define model y = a + b0 * x0 + b1 * x1 + noise
            double a = 1.0;
            double b0 = -2.0;
            double b1 = 3.0;
            Distribution noise = new NormalDistribution(0.0, 10.0);

            // draw a sample from the model
            Random rng = new Random(1);
            MultivariateSample sample = new MultivariateSample(3);
            for (int i = 0; i < 100; i++) {
                double x0 = -10.0 + 20.0 * rng.NextDouble();
                double x1 = -10.0 + 20.0 * rng.NextDouble();
                double eps = noise.InverseLeftProbability(rng.NextDouble());
                double y = a + b0 * x0 + b1 * x1 + eps;
                sample.Add(x0, x1, y);
            }

            // do a linear regression fit on the model
            FitResult result = sample.LinearRegression(2);

            // the result should have the appropriate dimension
            Assert.IsTrue(result.Dimension == 3);

            // the result should be significant
            Console.WriteLine("{0} {1}", result.GoodnessOfFit.Statistic, result.GoodnessOfFit.LeftProbability);
            Assert.IsTrue(result.GoodnessOfFit.LeftProbability > 0.95);

            // the parameters should match the model
            Console.WriteLine(result.Parameter(0));
            Assert.IsTrue(result.Parameter(0).ConfidenceInterval(0.90).ClosedContains(b0));
            Console.WriteLine(result.Parameter(1));
            Assert.IsTrue(result.Parameter(1).ConfidenceInterval(0.90).ClosedContains(b1));
            Console.WriteLine(result.Parameter(2));
            Assert.IsTrue(result.Parameter(2).ConfidenceInterval(0.90).ClosedContains(a));
        }
        public void MultivariateLinearRegressionNullDistribution()
        {
            int d = 4;

            Random rng = new Random(1);
            NormalDistribution n = new NormalDistribution();

            Sample fs = new Sample();

            for (int i = 0; i < 64; i++) {
                MultivariateSample ms = new MultivariateSample(d);
                for (int j = 0; j < 8; j++) {
                    double[] x = new double[d];
                    for (int k = 0; k < d; k++) {
                        x[k] = n.GetRandomValue(rng);
                    }
                    ms.Add(x);
                }
                FitResult r = ms.LinearRegression(0);
                fs.Add(r.GoodnessOfFit.Statistic);
            }

            // conduct a KS test to check that F follows the expected distribution
            TestResult ks = fs.KolmogorovSmirnovTest(new FisherDistribution(3, 4));
            Assert.IsTrue(ks.LeftProbability < 0.95);
        }
        public void MultivariateLinearRegressionBadInputTest()
        {
            // create a sample
            MultivariateSample sample = new MultivariateSample(3);
            sample.Add(1, 2, 3);
            sample.Add(2, 3, 4);

            // try to predict with too little data
            try {
                sample.LinearRegression(2);
                Assert.IsTrue(false);
            } catch (InvalidOperationException) {
                Assert.IsTrue(true);
            }

            // add enough data
            sample.Add(3, 4, 5);
            sample.Add(4, 5, 6);

            // try to predict a non-existent variable
            try {
                sample.LinearRegression(-1);
                Assert.IsTrue(false);
            } catch (ArgumentOutOfRangeException) {
                Assert.IsTrue(true);
            }

            try {
                sample.LinearRegression(3);
                Assert.IsTrue(false);
            } catch (ArgumentOutOfRangeException) {
                Assert.IsTrue(true);
            }
        }