//r = Cov(x,y)/(StandardDeviation(x)*StandardDeviation(y))
 private double computePearsonCorr(Vector x, Vector y)
 {
     int n = x.Tuples;
     Vector xy = new Vector(n);
     for (int i = 0; i < n; i++)
         xy[i] = x[i] * y[i];
     double sumxy = xy.GetSumData();
     double xbar = x.GetSumData() / (double)n;
     double ybar = y.GetSumData() / (double)n;
     double covxy = (sumxy - n * xbar * ybar) / (double)(n - 1);
     Vector x2 = x.GetDataSquare();
     Vector y2 = y.GetDataSquare();
     double varx = (n * x2.GetSumData() - Math.Pow(x.GetSumData(), 2.0)) / (double)(n * (n - 1));
     double vary = (n * y2.GetSumData() - Math.Pow(y.GetSumData(), 2.0)) / (double)(n * (n - 1));
     return covxy / Math.Sqrt(varx * vary);
 }
        /// <summary>
        /// Constructor. Menghitung nilai LjungBox Q Stat.        
        /// </summary>
        /// <param name="Y">Vektor. Vektor data yang akan dicari 
        /// nilai LjungBox Q Statistiknya. </param>
        /// <param name="Lag">Int. Jumlah lag yang di include kan</param>
        public LjungBoxQTest(Vector Y, int Lag)
        {
            double[] ljung = new double[Lag];
            double[] acfValue = new double[Lag];
            double sekuadrat;
            double sumacf = 0;
            double[] acfD;

            this.acf = new Vector(Lag);
            this.pacf = new Matrix (Lag,Lag);
            this.se = new Vector(Lag);
            this.lBox = new Vector(Lag);
            this.pValue = new Vector(Lag);
            this.pValue1 = new Vector(Lag);

            this.n = Y.Tuples;
            this.lag = Lag;
            this.y = Y.Copy();
            this.sumY = y.GetSumData();
            this.yBar = sumY / n;

            // Penghitungan nilai ACF

            for (int k = 0; k < lag; k++)
            {
                acf[k] = getRho(k+1);
            }

            double sumRho = 0;
            for (int i = 1; i <= lag; i++)
            {
                sumRho += Math.Pow(acf[i - 1], 2) / (n - i);

                lBox[i - 1] = n * (n + 2) * sumRho;

                pValue1[i - 1] = ChiSquare.PValue(lBox[i - 1], i);
                if (pValue1[i - 1] < 0) pValue[i - 1] = 0;
                else pValue[i - 1] = pValue1[i - 1];
            }

            //previous way
            //for (int i = 1; i <= lag; i++)
            //{
            //    double sumRho = 0;
            //    for (int k = 1; k <= i; k++)
            //    {
            //        sumRho += Math.Pow(acf[k - 1], 2) / (n - k);
            //    }

            //    lBox[i - 1] = n * (n + 2) * sumRho;
            //    ChiSquare chi = new ChiSquare(i);

            //    pValue1[i - 1] = chi.PValue(lBox[i - 1]);
            //    if (pValue1[i - 1] < 0) pValue[i - 1] = 0;
            //    else pValue[i - 1] = pValue1[i - 1];
            //}

            //Penghitungan nilai PACF

            pacf[0, 0] = acf[0];

            for (int i = 1; i < lag; i++)
            {
                double sumatas = 0;
                double sumbawah = 0;

                for (int k = 0; k < i; k++)
                {
                    sumatas += pacf[i - 1, k] * acf[(i - k) - 1];
                    sumbawah += pacf[i - 1, k] * acf[k];
                }

                pacf[i, i] = (acf[i] - sumatas) / (1 - sumbawah);

                for (int j = 0; j < i; j++)
                {
                    pacf[i, j] = pacf[i - 1, j] - (pacf[i, i] * pacf[i - 1, (i - j) - 1]);
                }
            }

            // previous way
            //for (int i = 1; i < lag; i++)
            //{
            //    for (int j = 0; j < i; j++)
            //    {
            //        double sumatas = 0;
            //        double sumbawah = 0;

            //        for (int k = 0; k < i; k++)
            //        {
            //            sumatas += pacf[i - 1, k] * acf[(i - k) - 1];
            //            sumbawah += pacf[i - 1, k] * acf[k];
            //        }

            //        pacf[i, i] = (acf[i] - sumatas) / (1 - sumbawah);

            //        pacf[i, j] = pacf[i - 1, j] - (pacf[i, i] * pacf[i - 1, (i - j) - 1]);

            //    }
            //}

            acfD = acf.GetData();

            sumacf = 0;
            for (int i = 0; i < lag; i++)
            {
                sekuadrat = (1 + 2 * sumacf) / y.Tuples;
                se[i] = Math.Sqrt(sekuadrat);

                sumacf += Math.Pow(acfD[i], 2);
            }

            // previous way
            //for (int i = 0; i < lag; i++)
            //{
            //    sumacf = 0;
            //    for (int j = 0; j < i; j++)
            //    {
            //        sumacf += Math.Pow(acfD[j], 2);
            //    }
            //    sekuadrat = (1 + 2 * sumacf) / y.Tuples;
            //    se[i] = Math.Sqrt(sekuadrat);
            //}
        }