Ejemplo n.º 1
0
        public static double[][] ConvertToNestedArray(double[,] coeffs)
        {
            double[,] treesOfCoeffs = Haar.ListOfCoeffs(coeffs);
            double[][] Data = new double[treesOfCoeffs.GetLength(0)][];

            for (int i = 0; i < treesOfCoeffs.GetLength(0); i++)
            {
                double[] row = new double[5];
                for (int j = 0; j < treesOfCoeffs.GetLength(1); j++)
                {
                    row[j] = treesOfCoeffs[i, j];
                }
                Data[i] = row;
            }
            return(Data);
        }
Ejemplo n.º 2
0
        public static double[,] ListOfCoeffs(double[,] coeffs)
        {
            double[] Scale2 = Haar.Scale2To1DCoeff(coeffs);
            double[,] Scale1 = Haar.Scale1To1DCoeff(coeffs);
            double[,] Scale  = new double[Scale2.Length, 5];

            for (int i = 0; i < Scale2.Length; i++)
            {
                Scale[i, 0] = Scale2[i];
                Scale[i, 1] = Scale1[i, 0];
                Scale[i, 2] = Scale1[i, 1];
                Scale[i, 3] = Scale1[i, 2];
                Scale[i, 4] = Scale1[i, 3];
            }
            return(Scale);
        }
Ejemplo n.º 3
0
        public static double[] Means(double[,] coeffs, int m)
        {
            double[] mean = new double[5]; //5 node each tree
            double[,] treesOfcoeffs       = Haar.ListOfCoeffs(coeffs);
            double[,] hiddenstates        = GetHiddenStateValue(coeffs);
            double[,] treesOfHiddenStates = Haar.ListOfCoeffs(hiddenstates);

            //P(m)
            double[] rootpmf = ParentStateProbability(hiddenstates);

            //P(n|m)
            double transitionn1m1 = TransitionProbability(hiddenstates, 1, 1);
            double transitionn2m1 = TransitionProbability(hiddenstates, 2, 1);
            double transitionn1m2 = TransitionProbability(hiddenstates, 1, 2);
            double transitionn2m2 = TransitionProbability(hiddenstates, 2, 2);

            //P(n)
            double[] childpmf = ChildStateProbability(hiddenstates);

            if (m == 1)
            {
                double sum  = 0;
                double sum2 = 0;
                double sum3 = 0;
                double sum4 = 0;
                double sum5 = 0;

                // Parent Node
                for (int j = 0; j < treesOfcoeffs.GetLength(0); j++)
                {
                    sum += treesOfcoeffs[j, 0] * rootpmf[0];
                }
                double mean1 = (double)sum / ((double)treesOfcoeffs.GetLength(0) * (double)rootpmf[0]);

                // Child node 1
                for (int k = 0; k < treesOfcoeffs.GetLength(0); k++)
                {
                    sum2 += treesOfcoeffs[k, 1] * childpmf[0];
                }
                double mean2 = (double)sum2 / ((double)treesOfcoeffs.GetLength(0) * (double)childpmf[0]);

                // Child node 2
                for (int l = 0; l < treesOfcoeffs.GetLength(0); l++)
                {
                    sum3 += treesOfcoeffs[l, 2] * childpmf[0];
                }
                double mean3 = (double)sum3 / ((double)treesOfcoeffs.GetLength(0) * (double)childpmf[0]);

                // Child node 3
                for (int o = 0; o < treesOfcoeffs.GetLength(0); o++)
                {
                    sum4 += treesOfcoeffs[o, 3] * childpmf[0];
                }
                double mean4 = (double)sum4 / ((double)treesOfcoeffs.GetLength(0) * (double)childpmf[0]);

                // Child node 4
                for (int p = 0; p < treesOfcoeffs.GetLength(0); p++)
                {
                    sum5 += treesOfcoeffs[p, 4] * childpmf[0];
                }
                double mean5 = (double)sum5 / ((double)treesOfcoeffs.GetLength(0) * (double)childpmf[0]);

                mean[0] = mean1;
                mean[1] = mean2;
                mean[2] = mean3;
                mean[3] = mean4;
                mean[4] = mean5;
            }
            else
            {
                double sum  = 0;
                double sum2 = 0;
                double sum3 = 0;
                double sum4 = 0;
                double sum5 = 0;

                // Parent Node
                for (int j = 0; j < treesOfcoeffs.GetLength(0); j++)
                {
                    sum += treesOfcoeffs[j, 0] * rootpmf[1];
                }
                double mean1 = (double)sum / ((double)treesOfcoeffs.GetLength(0) * (double)rootpmf[1]);

                // Child node 1
                for (int k = 0; k < treesOfcoeffs.GetLength(0); k++)
                {
                    sum2 += treesOfcoeffs[k, 1] * childpmf[1];
                }
                double mean2 = (double)sum2 / ((double)treesOfcoeffs.GetLength(0) * (double)childpmf[1]);

                // Child node 2
                for (int l = 0; l < treesOfcoeffs.GetLength(0); l++)
                {
                    sum3 += treesOfcoeffs[l, 2] * childpmf[1];
                }
                double mean3 = (double)sum3 / ((double)treesOfcoeffs.GetLength(0) * (double)childpmf[1]);

                // Child node 3
                for (int o = 0; o < treesOfcoeffs.GetLength(0); o++)
                {
                    sum4 += treesOfcoeffs[o, 3] * childpmf[1];
                }
                double mean4 = (double)sum4 / ((double)treesOfcoeffs.GetLength(0) * (double)childpmf[1]);

                // Child node 4
                for (int p = 0; p < treesOfcoeffs.GetLength(0); p++)
                {
                    sum5 += treesOfcoeffs[p, 4] * childpmf[1];
                }
                double mean5 = (double)sum5 / ((double)treesOfcoeffs.GetLength(0) * (double)childpmf[1]);

                mean[0] = mean1;
                mean[1] = mean2;
                mean[2] = mean3;
                mean[3] = mean4;
                mean[4] = mean5;
            }

            return(mean);
        }
Ejemplo n.º 4
0
        public static double TransitionStateProbability(double[,] coeffs, int n, int m) //P(n|m)
        {
            int size = ((coeffs.GetLength(0) * coeffs.GetLength(1)) / 16) * 3;

            double[,] hiddenstates = GetHiddenStateValue(coeffs);
            double[] transitionProb = new double[size];
            double[,] trees = Haar.ListOfCoeffs(hiddenstates);

            int n1m1 = 0;
            int n2m1 = 0;
            int n1m2 = 0;
            int n2m2 = 0;

            if (n == 1 && m == 1)
            {
                for (int i = 0; i < trees.GetLength(0); i++)
                {
                    n1m1 = 0;
                    for (int j = 1; j < trees.GetLength(1); j++)
                    {
                        if (trees[i, j] == 1 && trees[i, 0] == 1)
                        {
                            n1m1++;
                        }
                    }
                    transitionProb[i] = (double)n1m1 / (double)4;
                }

                double   sum            = transitionProb.Sum();
                double[] rootpmf        = ParentStateProbability(coeffs);
                double   pmf1           = rootpmf[0];
                double   transitionn1m1 = (double)sum / ((double)trees.GetLength(0) * (double)pmf1);
                return(transitionn1m1);
            }
            else if (n == 2 && m == 1)
            {
                for (int i = 0; i < trees.GetLength(0); i++)
                {
                    n2m1 = 0;
                    for (int j = 1; j < trees.GetLength(1); j++)
                    {
                        if (trees[i, j] == 2 && trees[i, 0] == 1)
                        {
                            n2m1++;
                        }
                    }
                    transitionProb[i] = (double)n2m1 / (double)4;
                }

                double   sum            = transitionProb.Sum();
                double[] rootpmf        = ParentStateProbability(coeffs);
                double   pmf1           = rootpmf[0];
                double   transitionn2m1 = (double)sum / ((double)trees.GetLength(0) * (double)pmf1);
                return(transitionn2m1);
            }
            else if (n == 1 && m == 2)
            {
                for (int i = 0; i < trees.GetLength(0); i++)
                {
                    n1m2 = 0;
                    for (int j = 1; j < trees.GetLength(1); j++)
                    {
                        if (trees[i, j] == 1 && trees[i, 0] == 2)
                        {
                            n1m2++;
                        }
                    }
                    transitionProb[i] = (double)n1m2 / (double)4;
                }

                double   sum            = transitionProb.Sum();
                double[] rootpmf        = ParentStateProbability(coeffs);
                double   pmf2           = rootpmf[1];
                double   transitionn1m2 = (double)sum / ((double)trees.GetLength(0) * (double)pmf2);
                return(transitionn1m2);
            }
            else
            {
                for (int i = 0; i < trees.GetLength(0); i++)
                {
                    n2m2 = 0;
                    for (int j = 1; j < trees.GetLength(1); j++)
                    {
                        if (trees[i, j] == 2 && trees[i, 0] == 2)
                        {
                            n2m2++;
                        }
                    }
                    transitionProb[i] = (double)n2m2 / (double)4;
                }

                double   sum            = transitionProb.Sum();
                double[] rootpmf        = ParentStateProbability(coeffs);
                double   pmf2           = rootpmf[1];
                double   transitionn2m2 = (double)sum / ((double)trees.GetLength(0) * (double)pmf2);
                return(transitionn2m2);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Calculate likelihood probability of child node in state n given the parent node in state m.
        /// P(n|m) , n = child node and m = parent node.
        /// Create A Matrix that contain:
        ///     [P(n=1|m=1)  P(n=2|m=1)]   -> [ pos=1    pos=2 ]
        ///     [P(n=1|m=2)  P(n=2|m=2)]      [ pos=3    pos=4 ]
        /// The nature of Child Hidden States are that 1 & 3 always same, and 2 & 4 always same.(still in doubt)
        /// </summary>
        /// <param name="coeffs">Wavelet Coefficients</param>
        /// <param name="pos">Position of child node for each parent -> pos ={1,2,3,4}</param>
        /// <param name="m">Hidden state of Parent node</param>
        /// <param name="n">Hidden state of Child node</param>
        /// <returns></returns>
        public static double TransitionProbability(double[,] coeffs, /*int pos,*/ int n, int m)
        {
            double[,] hiddenstates = GetHiddenStateValue(coeffs);
            // Calculate number of parent node with state 1 or 2
            double rootpmf1    = StateProbability2(hiddenstates, 2, 1);
            double rootpmf2    = StateProbability2(hiddenstates, 2, 2);
            double NumOfRootm1 = (double)rootpmf1 * (double)(((coeffs.GetLength(0) * coeffs.GetLength(1)) / 16) * 3); //Number of parent node whose m = 1
            double NumOfRootm2 = (double)rootpmf2 * (double)(((coeffs.GetLength(0) * coeffs.GetLength(1)) / 16) * 3); //Number of parent node whose m = 2

            double[] Scale2 = Haar.Scale2To1DCoeff(hiddenstates);
            double[,] Scale1 = Haar.Scale1To1DCoeff(hiddenstates);
            double[,] Scale  = new double[Scale2.Length, 5];

            for (int i = 0; i < Scale2.Length; i++)
            {
                Scale[i, 0] = Scale2[i];
                Scale[i, 1] = Scale1[i, 0];
                Scale[i, 2] = Scale1[i, 1];
                Scale[i, 3] = Scale1[i, 2];
                Scale[i, 4] = Scale1[i, 3];
            }


            ///Calculating Transition probability
            int c1 = 0;
            int c2 = 0;
            int c3 = 3;
            int c4 = 4;

            if (n == 1 && m == 1)
            {
                for (int i = 0; i < Scale.GetLength(0); i++)
                {
                    for (int j = 1; j < Scale.GetLength(1); j++)
                    {
                        if (Scale[i, j] == 1 && Scale[i, 0] == 1)
                        {
                            c1++;
                        }
                    }
                }
                double transitionProb = (double)c1 / (double)NumOfRootm1;
                return(transitionProb);
            }
            else if (n == 2 && m == 1)
            {
                for (int i = 0; i < Scale.GetLength(0); i++)
                {
                    for (int j = 1; j < Scale.GetLength(1); j++)
                    {
                        if (Scale[i, j] == 2 && Scale[i, 0] == 1)
                        {
                            c2++;
                        }
                    }
                }
                double transitionProb = (double)c2 / (double)NumOfRootm1;
                return(transitionProb);
            }
            else if (n == 1 && m == 2)
            {
                for (int i = 0; i < Scale.GetLength(0); i++)
                {
                    for (int j = 1; j < Scale.GetLength(1); j++)
                    {
                        if (Scale[i, j] == 1 && Scale[i, 0] == 2)
                        {
                            c3++;
                        }
                    }
                }
                double transtitionProb = (double)c3 / (double)NumOfRootm2;
                return(transtitionProb);
            }
            else
            {
                for (int i = 0; i < Scale.GetLength(0); i++)
                {
                    for (int j = 1; j < Scale.GetLength(1); j++)
                    {
                        if (Scale[i, j] == 2 && Scale[i, 0] == 2)
                        {
                            c4++;
                        }
                    }
                }
                double transtitionProb = (double)c4 / (double)NumOfRootm2;
                return(transtitionProb);
            }
        }
Ejemplo n.º 6
0
        public static double[] Variances(double[,] coeffs, int m)
        {
            double[] variances = new double[5]; //5 node each tree
            double[,] treesOfcoeffs       = Haar.ListOfCoeffs(coeffs);
            double[,] hiddenstates        = GetHiddenStateValue(coeffs);
            double[,] treesOfHiddenStates = Haar.ListOfCoeffs(hiddenstates);

            //P(m)
            double[] rootpmf = ParentStateProbability(hiddenstates);

            //P(n|m)
            double transitionn1m1 = TransitionProbability(hiddenstates, 1, 1);
            double transitionn2m1 = TransitionProbability(hiddenstates, 2, 1);
            double transitionn1m2 = TransitionProbability(hiddenstates, 1, 2);
            double transitionn2m2 = TransitionProbability(hiddenstates, 2, 2);

            //P(n)
            double[] childpmf = ChildStateProbability(hiddenstates);

            double[] mean1 = Means(coeffs, 1);
            double[] mean2 = Means(coeffs, 2);

            if (m == 1)
            {
                double sum  = 0;
                double sum2 = 0;
                double sum3 = 0;
                double sum4 = 0;
                double sum5 = 0;

                // Parent Node
                for (int j = 0; j < treesOfcoeffs.GetLength(0); j++)
                {
                    sum += Math.Pow((treesOfcoeffs[j, 0] - mean1[0]), 2) * rootpmf[0];
                }
                double variance1 = (double)sum / ((double)treesOfcoeffs.GetLength(0) * (double)rootpmf[0]);

                // Child node 1
                for (int k = 0; k < treesOfcoeffs.GetLength(0); k++)
                {
                    sum2 += Math.Pow((treesOfcoeffs[k, 1] - mean1[1]), 2) * childpmf[0];
                }
                double variance2 = (double)sum2 / ((double)treesOfcoeffs.GetLength(0) * (double)childpmf[0]);

                // Child node 2
                for (int l = 0; l < treesOfcoeffs.GetLength(0); l++)
                {
                    sum3 += Math.Pow((treesOfcoeffs[l, 2] - mean1[2]), 2) * childpmf[0];
                }
                double variance3 = (double)sum3 / ((double)treesOfcoeffs.GetLength(0) * (double)childpmf[0]);

                // Child node 3
                for (int o = 0; o < treesOfcoeffs.GetLength(0); o++)
                {
                    sum4 += Math.Pow((treesOfcoeffs[o, 3] - mean1[3]), 2) * childpmf[0];
                }
                double variance4 = (double)sum4 / ((double)treesOfcoeffs.GetLength(0) * (double)childpmf[0]);

                // Child node 4
                for (int p = 0; p < treesOfcoeffs.GetLength(0); p++)
                {
                    sum5 += Math.Pow((treesOfcoeffs[p, 4] - mean1[4]), 2) * childpmf[0];
                }
                double variance5 = (double)sum5 / ((double)treesOfcoeffs.GetLength(0) * (double)childpmf[0]);

                variances[0] = variance1;
                variances[1] = variance2;
                variances[2] = variance3;
                variances[3] = variance4;
                variances[4] = variance5;
            }
            else
            {
                double sum  = 0;
                double sum2 = 0;
                double sum3 = 0;
                double sum4 = 0;
                double sum5 = 0;

                // Parent Node
                for (int j = 0; j < treesOfcoeffs.GetLength(0); j++)
                {
                    sum += Math.Pow((treesOfcoeffs[j, 0] - mean2[0]), 2) * rootpmf[1];
                }
                double variance1 = (double)sum / ((double)treesOfcoeffs.GetLength(0) * (double)rootpmf[1]);

                // Child node 1
                for (int k = 0; k < treesOfcoeffs.GetLength(0); k++)
                {
                    sum2 += Math.Pow((treesOfcoeffs[k, 1] - mean2[1]), 2) * childpmf[1];
                }
                double variance2 = (double)sum2 / ((double)treesOfcoeffs.GetLength(0) * (double)childpmf[1]);

                // Child node 2
                for (int l = 0; l < treesOfcoeffs.GetLength(0); l++)
                {
                    sum3 += Math.Pow((treesOfcoeffs[l, 2] - mean2[2]), 2) * childpmf[1];
                }
                double variance3 = (double)sum3 / ((double)treesOfcoeffs.GetLength(0) * (double)childpmf[1]);

                // Child node 3
                for (int o = 0; o < treesOfcoeffs.GetLength(0); o++)
                {
                    sum4 += Math.Pow((treesOfcoeffs[o, 3] - mean2[3]), 2) * childpmf[1];
                }
                double variance4 = (double)sum4 / ((double)treesOfcoeffs.GetLength(0) * (double)childpmf[1]);

                // Child node 4
                for (int p = 0; p < treesOfcoeffs.GetLength(0); p++)
                {
                    sum5 += Math.Pow((treesOfcoeffs[p, 4] - mean2[4]), 2) * childpmf[1];
                }
                double variance5 = (double)sum5 / ((double)treesOfcoeffs.GetLength(0) * (double)childpmf[1]);

                variances[0] = variance1;
                variances[1] = variance2;
                variances[2] = variance3;
                variances[3] = variance4;
                variances[4] = variance5;
            }

            return(variances);
        }