Ejemplo n.º 1
0
        public static void InitialParameter(int _m, int _n, int _k)
        {
            m = _m + 1;
            n = _n;
            k = _k;

            alpha = Enumerable.Range(0, m - 1).Select(dp => 1.0).ToArray();
            beta = Enumerable.Range(0, k).Select(dp => 1.0).ToArray();
            MathNet.Numerics.Distributions.Dirichlet dPhi = new MathNet.Numerics.Distributions.Dirichlet(alpha);
            MathNet.Numerics.Distributions.Dirichlet dTheta = new MathNet.Numerics.Distributions.Dirichlet(beta);
            phi = new MathNet.Numerics.Distributions.Categorical[m];
            zArr = new int[n];


            var p = new double[k];
            for (int i = 0; i < k; i++)
                p[i] = 1.0 / k;

            theta = new MathNet.Numerics.Distributions.Categorical(

                p
                );

            for (int i = 0; i < n; i++)
            {
                zArr[i] = theta.Sample();
            }
            for (int i = 0; i < m; i++)
            {
                phi[i] = new MathNet.Numerics.Distributions.Categorical(
                    dPhi.Sample()
                    );
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// The probability density function for a primitive distribution and point.
        /// Throws NotImplementedException if no PDF is defined for given distribution.
        /// </summary>
        public static Prob Pdf(PrimitiveDist <double[]> dist, double[] x)
        {
            if (dist is DirichletPrimitive)
            {
                var dir = dist as DirichletPrimitive;
                var d   = new MathNet.Numerics.Distributions.Dirichlet(dir.alpha);

                return(Prob(d.Density(x)));
            }

            if (dist is MultiVariateNormalPrimitive)
            {
                var d = dist as MultiVariateNormalPrimitive;

                return(Prob(d.mvn.Density(DenseMatrix.OfRowArrays(x))));
            }
            throw new NotImplementedException("No PDF for this distribution implemented");
        }
Ejemplo n.º 3
0
        public static void GibbsSamplingEach(List<SequenceEle> seq, int[][] matrix)
        {
            for (int i = 0; i < n; i++)
            {
                //draw z
                double[] p = new double[k];
                for (int z = 0; z < k; z++)
                {
                    var theta_tmp = theta.P[z];
                    var phi_tmp = 0.0;
                    if (i == 0)
                        try
                        {
                            phi_tmp = phi[m - 1].P[seq[i].tIndex];
                        }
                        catch (Exception e)
                        {

                        }
                    else
                        try
                        {
                            phi_tmp = phi[seq[i - 1].tIndex].P[seq[i].tIndex];
                        }
                        catch (Exception e)
                        {

                        }
                    if (phi_tmp <= 0)
                        phi_tmp = 1.0 / Math.Pow(10, 100);
                    try
                    {
                        var f = Math.Exp(
                            (-1) * Math.Pow((z + 1 - matrix[i][seq[i].tIndex]
                            ), 2)
                            );
                        p[z] = Math.Pow(10, 10) * theta_tmp * phi_tmp * f;
                    }
                    catch (Exception e)
                    {

                    }
                }
                try
                {
                    MathNet.Numerics.Distributions.Categorical dis = new MathNet.Numerics.Distributions.Categorical(p);
                    zArr[i] = dis.Sample();
                }
                catch (Exception e)
                {

                }

                var beta_tmp = new double[k];
                beta.CopyTo(beta_tmp, 0);
                foreach (var g in zArr.GroupBy(dp => dp))
                {
                    beta_tmp[g.Key] += g.Count();
                }
                var d_tmp = new MathNet.Numerics.Distributions.Dirichlet(beta_tmp);
                theta = new MathNet.Numerics.Distributions.Categorical(d_tmp.Sample());

                for (int j = 0; j < m; j++)
                {
                    p = Enumerable.Range(0, m).Select(dp => 1.0).ToArray();
                    for (int ii = 0; ii < n; ii++)
                    {

                        if (j == m - 1 && ii == 0)
                        {
                            try
                            {


                                var z = zArr[ii];

                                var f = Math.Exp(
                        (-1) * Math.Pow((z - matrix[ii][seq[ii].tIndex] / ((double)matrix[ii].Max()) * k), 2)
                        );

                                p[seq[ii].tIndex] *= phi[j].P[seq[ii].tIndex] * f;

                            }
                            catch (Exception e)
                            {

                            }
                        }


                        else
                        {

                            if (ii == 0)
                                continue;
                            try
                            {
                                if (seq[ii - 1].tIndex == j)
                                {
                                    var z = zArr[ii];

                                    var f = Math.Exp(
                            (-1) * Math.Pow((z + 1 - matrix[ii][seq[ii].tIndex]
                            ), 2)
                            );

                                    p[seq[ii].tIndex] *= phi[j].P[seq[ii].tIndex] * f;
                                }
                            }
                            catch (Exception e)
                            {

                            }
                        }
                    }

                    for (int s = 0; s < p.Length; s++)
                        if (p[s] == 1.0 || p[s] <= 0)
                            p[s] = 1.0 / Math.Pow(10, 100);
                    phi[j] = new MathNet.Numerics.Distributions.Categorical(p);
                }
            }
        }
Ejemplo n.º 4
0
        public DirichletPrimitive(double[] alpha, Random gen)
        {
            this.alpha = alpha;

            dist = new MathNet.Numerics.Distributions.Dirichlet(alpha, gen);
        }