예제 #1
0
 public static Gaussian UsesAverageLogarithm(NonconjugateGaussian[] Uses, Gaussian Def, Gaussian result)
 {
     NonconjugateGaussian prod = Uses[0];
     for (int i = 1; i < Uses.Length; i++)
         prod.SetToProduct(prod, Uses[i]);
     result = prod.GetGaussian(true);
     result.SetToProduct(result, Def);
     return result;
 }
예제 #2
0
파일: Copy.cs 프로젝트: mesgarpour/ERMER
        /*-------------- Nonconjugate Gaussian --------------------*/

        /// <summary>VMP message to <c>value</c>.</summary>
        /// <param name="copy">Incoming message from <c>copy</c>.</param>
        /// <param name="value">Incoming message from <c>value</c>.</param>
        /// <param name="to_value">Previous outgoing message to <c>value</c>.</param>
        /// <returns>The outgoing VMP message to the <c>value</c> argument.</returns>
        /// <remarks>
        ///   <para>The outgoing message is the factor viewed as a function of <c>value</c> with <c>copy</c> integrated out. The formula is <c>sum_copy p(copy) factor(copy,value)</c>.</para>
        /// </remarks>
        /// <remarks><para>
        /// We reverse the direction of factor to get the behaviour we want here.
        /// </para></remarks>
        public static Gaussian ValueAverageLogarithm(NonconjugateGaussian copy, Gaussian value, Gaussian to_value)
        {
            var a = value / to_value;

            copy *= new NonconjugateGaussian(a);
            var result = copy.GetGaussian(true) / a;

            return(result);
        }
예제 #3
0
        public static Gamma ExpAverageLogarithm([Proper] NonconjugateGaussian d)
        {
            double mD, vD;

            d.GetMeanAndVariance(out mD, out vD);
            double lm = mD + vD / 2;

            //return Gamma.FromMeanAndVariance(Math.Exp(lm), Math.Exp(2*lm)*(Math.Exp(vD)-1));
            return(Gamma.FromLogMeanAndMeanLog(lm, mD));
        }
예제 #4
0
        public static Gaussian UsesAverageLogarithm(NonconjugateGaussian[] Uses, Gaussian Def, Gaussian result)
        {
            NonconjugateGaussian prod = Uses[0];

            for (int i = 1; i < Uses.Length; i++)
            {
                prod.SetToProduct(prod, Uses[i]);
            }
            result = prod.GetGaussian(true);
            result.SetToProduct(result, Def);
            return(result);
        }
예제 #5
0
파일: Copy.cs 프로젝트: mesgarpour/ERMER
 /// <summary>VMP message to <c>copy</c>.</summary>
 /// <param name="value">Incoming message from <c>value</c>.</param>
 /// <param name="copy">Incoming message from <c>copy</c>.</param>
 /// <param name="to_value">Previous outgoing message to <c>value</c>.</param>
 /// <returns>The outgoing VMP message to the <c>copy</c> argument.</returns>
 /// <remarks>
 ///   <para>The outgoing message is a distribution matching the moments of <c>copy</c> as the random arguments are varied. The formula is <c>proj[sum_(value) p(value) factor(copy,value)]</c>.</para>
 /// </remarks>
 /// <remarks><para>
 /// This message should include the previous contribution.
 /// </para></remarks>
 public static NonconjugateGaussian CopyAverageLogarithm(Gaussian value, NonconjugateGaussian copy, Gaussian to_value)
 {
     return(new NonconjugateGaussian(value / to_value));
 }
예제 #6
0
 /// <summary>VMP message to <c>b</c>.</summary>
 /// <param name="ProductExp">Incoming message from <c>productExp</c>. Must be a proper distribution. If uniform, the result will be uniform.</param>
 /// <param name="A">Constant value for <c>a</c>.</param>
 /// <param name="B">Incoming message from <c>b</c>. Must be a proper distribution. If uniform, the result will be uniform.</param>
 /// <param name="to_B">Previous outgoing message to <c>B</c>.</param>
 /// <param name="result">Modified to contain the outgoing message.</param>
 /// <returns>
 ///   <paramref name="result" />
 /// </returns>
 /// <remarks>
 ///   <para>The outgoing message is the factor viewed as a function of <c>b</c> with <c>productExp</c> integrated out. The formula is <c>sum_productExp p(productExp) factor(productExp,a,b)</c>.</para>
 /// </remarks>
 /// <exception cref="ImproperMessageException">
 ///   <paramref name="ProductExp" /> is not a proper distribution.</exception>
 /// <exception cref="ImproperMessageException">
 ///   <paramref name="B" /> is not a proper distribution.</exception>
 public static NonconjugateGaussian BAverageLogarithm(
     [SkipIfUniform] Gaussian ProductExp, double A, [Proper] Gaussian B, NonconjugateGaussian to_B, NonconjugateGaussian result)
 {
     return(BAverageLogarithm(ProductExp, Gaussian.PointMass(A), B, to_B, result));
 }
예제 #7
0
        /// <summary>VMP message to <c>b</c>.</summary>
        /// <param name="ProductExp">Incoming message from <c>productExp</c>. Must be a proper distribution. If uniform, the result will be uniform.</param>
        /// <param name="A">Incoming message from <c>a</c>. Must be a proper distribution. If uniform, the result will be uniform.</param>
        /// <param name="B">Incoming message from <c>b</c>. Must be a proper distribution. If uniform, the result will be uniform.</param>
        /// <param name="to_B">Previous outgoing message to <c>B</c>.</param>
        /// <param name="result">Modified to contain the outgoing message.</param>
        /// <returns>
        ///   <paramref name="result" />
        /// </returns>
        /// <remarks>
        ///   <para>The outgoing message is the exponential of the average log-factor value, where the average is over all arguments except <c>b</c>. Because the factor is deterministic, <c>productExp</c> is integrated out before taking the logarithm. The formula is <c>exp(sum_(a) p(a) log(sum_productExp p(productExp) factor(productExp,a,b)))</c>.</para>
        /// </remarks>
        /// <exception cref="ImproperMessageException">
        ///   <paramref name="ProductExp" /> is not a proper distribution.</exception>
        /// <exception cref="ImproperMessageException">
        ///   <paramref name="A" /> is not a proper distribution.</exception>
        /// <exception cref="ImproperMessageException">
        ///   <paramref name="B" /> is not a proper distribution.</exception>
        public static NonconjugateGaussian BAverageLogarithm(
            [SkipIfUniform] Gaussian ProductExp, [Proper, SkipIfUniform] Gaussian A, [Proper, SkipIfUniform] Gaussian B, NonconjugateGaussian to_B, NonconjugateGaussian result)
        {
            if (B.IsPointMass)
            {
                return(NonconjugateGaussian.Uniform());
            }
            if (ProductExp.IsPointMass)
            {
                return(BAverageLogarithm(ProductExp.Point, A));
            }
            if (!B.IsProper())
            {
                throw new ImproperMessageException(B);
            }
            // catch uniform case to avoid 0*Inf
            if (ProductExp.IsUniform())
            {
                return(NonconjugateGaussian.Uniform());
            }
            double mx, vx, m, v, mz, vz;

            ProductExp.GetMeanAndVariance(out mz, out vz);
            A.GetMeanAndVariance(out mx, out vx);
            B.GetMeanAndVariance(out m, out v);

            //if (mx * mz < 0)
            //{
            //    Console.WriteLine("Warning: mx*mz < 0, setting to uniform");
            //    result.SetToUniform();
            //    return result;
            //}

            double Ex2        = mx * mx + vx;
            double grad2_S_m2 = -2 * Ex2 * Math.Exp(2 * m + 2 * v) / vz + mx * mz * Math.Exp(m + .5 * v) / vz;
            double grad_m     = -Ex2 *Math.Exp(2 *m + 2 *v) / vz + mx * mz * Math.Exp(m + .5 * v) / vz;

            double threshold = 10;
            double mf, vf, afm1, bf;

            if (grad2_S_m2 >= -threshold && mx * mz > 0)
            {
                mf = Math.Log(mx * mz / Ex2) - 1.5 * v;
                vf = (mf - m) / grad_m;
            }
            else
            {
                vf = -1 / grad2_S_m2;
                mf = m - grad_m / grad2_S_m2;
            }

            Gaussian priorG;

            if (result.IsUniform())
            {
                priorG = B;
            }
            else
            {
                var prior = new NonconjugateGaussian();
                prior.SetToRatio((new NonconjugateGaussian(B)), to_B);
                priorG = prior.GetGaussian();
            }

            result.MeanTimesPrecision = mf / vf;
            result.Precision          = 1 / vf;

            var updatedM = new Gaussian(mf, vf) * priorG;

            m = updatedM.GetMean();

            double grad_S2_v2 = -2 * Ex2 * Math.Exp(2 * m + 2 * v) / vz + .25 * mx * mz * Math.Exp(m + .5 * v) / vz;
            double grad_S_v   = -Ex2 *Math.Exp(2 *m + 2 *v) / vz + .5 * mx * mz * Math.Exp(m + .5 * v) / vz;

            afm1 = -1;
            bf   = -1;
            if (grad2_S_m2 >= -threshold)
            {
                afm1 = -v * v * grad_S2_v2;
                bf   = -grad_S_v + afm1 / v;
            }

            if ((afm1 < 0 || bf < 0) && mx * mz > 0)
            {
                double v_opt = 2 / 3 * (Math.Log(mx * mz / Ex2 / 2) - m);
                if (v_opt != v)
                {
                    bf   = v * grad_S_v / (v_opt - v);
                    afm1 = v_opt * bf;
                }
            }

            if (afm1 < 0 || bf < 0)
            {
                afm1 = -v * v * grad_S2_v2;
                bf   = -grad_S_v + afm1 / v;
            }

            if (afm1 < 0 || bf < 0)
            {
                result.Shape = 1;
                result.Rate  = 0;
            }
            else
            {
                result.Shape = afm1 + 1;
                result.Rate  = bf;
            }

            if (!result.IsProper())
            {
                throw new ApplicationException("improper");
            }
            return(result);
            // REMEMBER TO ADD ENTROPY TERM IN REPLICATE OP
        }
예제 #8
0
            public void Initialize(bool skipStringDistributions = false)
            {
                // DO NOT make this a constructor, because it makes the test not notice complete lack of serialization as an empty object is set up exactly as the thing
                // you are trying to deserialize.
                this.pareto  = new Pareto(1.2, 3.5);
                this.poisson = new Poisson(2.3);
                this.wishart = new Wishart(20, new PositiveDefiniteMatrix(new double[, ] {
                    { 22, 21 }, { 21, 23 }
                }));
                this.vectorGaussian = new VectorGaussian(Vector.FromArray(13, 14), new PositiveDefiniteMatrix(new double[, ] {
                    { 16, 15 }, { 15, 17 }
                }));
                this.unnormalizedDiscrete = UnnormalizedDiscrete.FromLogProbs(DenseVector.FromArray(5.1, 5.2, 5.3));
                this.pointMass            = PointMass <double> .Create(1.1);

                this.gaussian             = new Gaussian(11.0, 12.0);
                this.nonconjugateGaussian = new NonconjugateGaussian(1.2, 2.3, 3.4, 4.5);
                this.gamma              = new Gamma(9.0, 10.0);
                this.gammaPower         = new GammaPower(5.6, 2.8, 3.4);
                this.discrete           = new Discrete(6.0, 7.0, 8.0);
                this.conjugateDirichlet = new ConjugateDirichlet(1.2, 2.3, 3.4, 4.5);
                this.dirichlet          = new Dirichlet(3.0, 4.0, 5.0);
                this.beta      = new Beta(2.0, 1.0);
                this.binomial  = new Binomial(5, 0.8);
                this.bernoulli = new Bernoulli(0.6);

                this.sparseBernoulliList    = SparseBernoulliList.Constant(4, new Bernoulli(0.1));
                this.sparseBernoulliList[1] = new Bernoulli(0.9);
                this.sparseBernoulliList[3] = new Bernoulli(0.7);

                this.sparseBetaList    = SparseBetaList.Constant(5, new Beta(2.0, 2.0));
                this.sparseBetaList[0] = new Beta(3.0, 4.0);
                this.sparseBetaList[1] = new Beta(5.0, 6.0);

                this.sparseGaussianList    = SparseGaussianList.Constant(6, Gaussian.FromMeanAndPrecision(0.1, 0.2));
                this.sparseGaussianList[4] = Gaussian.FromMeanAndPrecision(0.3, 0.4);
                this.sparseGaussianList[5] = Gaussian.FromMeanAndPrecision(0.5, 0.6);

                this.sparseGammaList = SparseGammaList.Constant(1, Gamma.FromShapeAndRate(1.0, 2.0));

                this.truncatedGamma    = new TruncatedGamma(1.2, 2.3, 3.4, 4.5);
                this.truncatedGaussian = new TruncatedGaussian(1.2, 3.4, 5.6, 7.8);
                this.wrappedGaussian   = new WrappedGaussian(1.2, 2.3, 3.4);

                ga = Distribution <double> .Array(new[] { this.gaussian, this.gaussian });

                vga = Distribution <Vector> .Array(new[] { this.vectorGaussian, this.vectorGaussian });

                ga2D = Distribution <double> .Array(new[, ] {
                    { this.gaussian, this.gaussian }, { this.gaussian, this.gaussian }
                });

                vga2D = Distribution <Vector> .Array(new[, ] {
                    { this.vectorGaussian, this.vectorGaussian }, { this.vectorGaussian, this.vectorGaussian }
                });

                gaJ = Distribution <double> .Array(new[] { new[] { this.gaussian, this.gaussian }, new[] { this.gaussian, this.gaussian } });

                vgaJ = Distribution <Vector> .Array(new[] { new[] { this.vectorGaussian, this.vectorGaussian }, new[] { this.vectorGaussian, this.vectorGaussian } });

                var gp    = new GaussianProcess(new ConstantFunction(0), new SquaredExponential(0));
                var basis = Util.ArrayInit(2, i => Vector.FromArray(1.0 * i));

                this.sparseGp = new SparseGP(new SparseGPFixed(gp, basis));

                this.quantileEstimator = new QuantileEstimator(0.01);
                this.quantileEstimator.Add(5);
                this.outerQuantiles = OuterQuantiles.FromDistribution(3, this.quantileEstimator);
                this.innerQuantiles = InnerQuantiles.FromDistribution(3, this.outerQuantiles);

                if (!skipStringDistributions)
                {
                    // String distributions can not be serialized by some formatters (namely BinaryFormatter)
                    // That is fine because this combination is never used in practice
                    this.stringDistribution1 = StringDistribution.String("aa")
                                               .Append(StringDistribution.OneOf("b", "ccc")).Append("dddd");
                    this.stringDistribution2 = new StringDistribution();
                    this.stringDistribution2.SetToProduct(StringDistribution.OneOf("a", "b"),
                                                          StringDistribution.OneOf("b", "c"));
                }
            }
예제 #9
0
        public static NonconjugateGaussian DNonconjugateAverageLogarithm([Proper] Gamma exp, [Proper, SkipIfUniform] Gaussian d, NonconjugateGaussian result)
        {
            var    vf = -1.0;
            var    a = exp.Shape;
            var    v_opt = 1.0 / (a - 1.0);
            var    b = exp.Rate;
            double m = 0.0, v = 0.0;

            d.GetMeanAndVariance(out m, out v);
            if (a > 1)
            {
                var mf = Math.Log((a - 1) / b) - .5 * v_opt;
                if (mf != m)
                {
                    var grad_S_m = -b *Math.Exp(m + v / 2) + a - 1;

                    vf = (mf - m) / grad_S_m;
                    result.MeanTimesPrecision = mf / vf;
                    result.Precision          = 1 / vf;
                }
            }
            //m = (mp + prior_mp)/(p + prior_p);
            if (vf < 0)
            {
                result.Precision          = b * Math.Exp(m + v / 2);
                result.MeanTimesPrecision = (m - 1) * result.Precision + a - 1;
            }

            double bf = -1, afm1 = -1;

            if (a <= 1)
            {
                v_opt = FindMinimumInV(b * Math.Exp(m));
            }
            if (v_opt != v)
            {
                var grad_S_v = -.5 * b * Math.Exp(m + v / 2);
                bf   = v * grad_S_v / (v_opt - v);
                afm1 = v_opt * bf;
            }
            if (afm1 < 0 || bf < 0)
            {
                afm1 = b * v * v * Math.Exp(m + v / 2) / 4;
                bf   = b * (1 + v / 2) * Math.Exp(m + v / 2) / 2;
            }
            result.Shape = afm1 + 1;
            result.Rate  = bf;
            if (!result.IsProper())
            {
                throw new ApplicationException("improper message calculated by ExpOp.DNonconjugateAverageLogarithm");
            }
            return(result);
        }
예제 #10
0
 public static NonconjugateGaussian DAverageLogarithm([Proper] Gamma exp, [Proper, SkipIfUniform] NonconjugateGaussian d, NonconjugateGaussian result)
 {
     return(DNonconjugateAverageLogarithm(exp, d.GetGaussian(true), result));
 }
예제 #11
0
 public static Gaussian MarginalAverageLogarithm(NonconjugateGaussian[] Uses, Gaussian Def, Gaussian result)
 {
     return UsesAverageLogarithm(Uses, Def, result);
 }
예제 #12
0
		/// <summary>
		/// VMP message to 'b'
		/// </summary>
		/// <param name="ProductExp">Incoming message from 'productExp'. Must be a proper distribution.  If uniform, the result will be uniform.</param>
		/// <param name="A">Constant value for 'a'.</param>
		/// <param name="B">Incoming message from 'b'. Must be a proper distribution.  If uniform, the result will be uniform.</param>
		/// <param name="to_B">Previous outgoing message to 'B'.</param>
		/// <param name="result">Modified to contain the outgoing message</param>
		/// <returns><paramref name="result"/></returns>
		/// <remarks><para>
		/// The outgoing message is the factor viewed as a function of 'b' with 'productExp' integrated out.
		/// The formula is <c>sum_productExp p(productExp) factor(productExp,a,b)</c>.
		/// </para></remarks>
		/// <exception cref="ImproperMessageException"><paramref name="ProductExp"/> is not a proper distribution</exception>
		/// <exception cref="ImproperMessageException"><paramref name="B"/> is not a proper distribution</exception>
		public static NonconjugateGaussian BAverageLogarithm([SkipIfUniform] Gaussian ProductExp, double A, [Proper] Gaussian B, NonconjugateGaussian to_B, NonconjugateGaussian result)
        {
            return BAverageLogarithm(ProductExp, Gaussian.PointMass(A), B, to_B, result);
        }
예제 #13
0
		/// <summary>
		/// VMP message to 'b'
		/// </summary>
		/// <param name="ProductExp">Incoming message from 'productExp'. Must be a proper distribution.  If uniform, the result will be uniform.</param>
		/// <param name="A">Incoming message from 'a'. Must be a proper distribution.  If uniform, the result will be uniform.</param>
		/// <param name="B">Incoming message from 'b'. Must be a proper distribution.  If uniform, the result will be uniform.</param>
		/// <param name="to_B">Previous outgoing message to 'B'.</param>
		/// <param name="result">Modified to contain the outgoing message</param>
		/// <returns><paramref name="result"/></returns>
		/// <remarks><para>
		/// The outgoing message is the exponential of the average log-factor value, where the average is over all arguments except 'b'.
		/// Because the factor is deterministic, 'productExp' is integrated out before taking the logarithm.
		/// The formula is <c>exp(sum_(a) p(a) log(sum_productExp p(productExp) factor(productExp,a,b)))</c>.
		/// </para></remarks>
		/// <exception cref="ImproperMessageException"><paramref name="ProductExp"/> is not a proper distribution</exception>
		/// <exception cref="ImproperMessageException"><paramref name="A"/> is not a proper distribution</exception>
		/// <exception cref="ImproperMessageException"><paramref name="B"/> is not a proper distribution</exception>
		public static NonconjugateGaussian BAverageLogarithm([SkipIfUniform] Gaussian ProductExp, [Proper, SkipIfUniform] Gaussian A, [Proper, SkipIfUniform] Gaussian B, NonconjugateGaussian to_B, NonconjugateGaussian result)
        {
            if (B.IsPointMass) return NonconjugateGaussian.Uniform();
            if (ProductExp.IsPointMass) return BAverageLogarithm(ProductExp.Point, A);
            if (!B.IsProper()) throw new ImproperMessageException(B);
            // catch uniform case to avoid 0*Inf
            if (ProductExp.IsUniform()) return NonconjugateGaussian.Uniform();
            double mx, vx, m, v, mz, vz;
            ProductExp.GetMeanAndVariance(out mz, out vz);
            A.GetMeanAndVariance(out mx, out vx);
            B.GetMeanAndVariance(out m, out v);

            //if (mx * mz < 0)
            //{
            //    Console.WriteLine("Warning: mx*mz < 0, setting to uniform");
            //    result.SetToUniform();
            //    return result;
            //}

            double Ex2 = mx * mx + vx;
            double grad2_S_m2 = -2 * Ex2 * Math.Exp(2 * m + 2 * v) / vz + mx * mz * Math.Exp(m + .5 * v) / vz;
            double grad_m = -Ex2 * Math.Exp(2 * m + 2 * v) / vz + mx * mz * Math.Exp(m + .5 * v) / vz;
            double threshold = 10;
            double mf, vf, afm1, bf;
            if (grad2_S_m2 >= -threshold && mx * mz > 0)
            {
                mf = Math.Log(mx * mz / Ex2) - 1.5 * v;
                vf = (mf - m) / grad_m;
            }
            else
            {
                vf = -1 / grad2_S_m2;
                mf = m - grad_m / grad2_S_m2;
            }
            
            Gaussian priorG;
            if (result.IsUniform())
                priorG = B;
            else
            {
                var prior = new NonconjugateGaussian();
                prior.SetToRatio((new NonconjugateGaussian(B)), to_B);
                priorG = prior.GetGaussian();
            }

            result.MeanTimesPrecision = mf / vf ;
            result.Precision = 1 / vf;

            var updatedM = new Gaussian(mf,vf) * priorG;
            m = updatedM.GetMean(); 
            
            double grad_S2_v2 = -2 * Ex2 * Math.Exp(2 * m + 2 * v) / vz + .25 * mx * mz * Math.Exp(m + .5 * v) / vz;
            double grad_S_v = -Ex2 * Math.Exp(2 * m + 2 * v) / vz + .5 * mx * mz * Math.Exp(m + .5 * v) / vz;

            afm1 = -1;
            bf = -1;
            if (grad2_S_m2 >= -threshold)
            {
                afm1 = -v * v * grad_S2_v2;
                bf = -grad_S_v + afm1 / v;
            }

            if ((afm1 < 0 || bf < 0) && mx * mz > 0)
            {
                double v_opt = 2 / 3 * (Math.Log(mx * mz / Ex2 / 2) - m);
                if (v_opt != v)
                {
                    bf = v * grad_S_v / (v_opt - v);
                    afm1 = v_opt * bf;
                }
            }

            if (afm1 < 0 || bf < 0)
            {
                afm1 = -v * v * grad_S2_v2;
                bf = -grad_S_v + afm1 / v;
            }

            if (afm1 < 0 || bf < 0)
            {
                result.Shape = 1;
                result.Rate = 0; 
            }
            else
            {
                result.Shape = afm1 + 1;
                result.Rate = bf;
            }

            if (!result.IsProper())
                throw new ApplicationException("improper");
            return result;
            // REMEMBER TO ADD ENTROPY TERM IN REPLICATE OP
        }
예제 #14
0
파일: Exp.cs 프로젝트: prgoodwin/HabilisX
		public static NonconjugateGaussian DNonconjugateAverageLogarithm([Proper] Gamma exp, [Proper, SkipIfUniform] Gaussian d, NonconjugateGaussian result)
		{
			var vf = -1.0;
			var a = exp.Shape;
			var v_opt = 1.0 / (a - 1.0);
			var b = exp.Rate;
			double m = 0.0, v = 0.0;
			d.GetMeanAndVariance(out m, out v);
			if (a > 1) {
				var mf = Math.Log((a - 1) / b) - .5 * v_opt;
				if (mf != m) {
					var grad_S_m = -b * Math.Exp(m + v / 2) + a - 1;
					vf = (mf - m) / grad_S_m;
					result.MeanTimesPrecision = mf / vf;
					result.Precision = 1 / vf;
				}
			}
			//m = (mp + prior_mp)/(p + prior_p);
			if (vf < 0) {
				result.Precision = b * Math.Exp(m + v / 2);
				result.MeanTimesPrecision = (m - 1) * result.Precision + a - 1;
			}

			double bf = -1, afm1 = -1;
			if (a <= 1)
				v_opt = FindMinimumInV(b * Math.Exp(m));
			if (v_opt != v) {
				var grad_S_v = -.5 * b * Math.Exp(m + v / 2);
				bf = v * grad_S_v / (v_opt - v);
				afm1 = v_opt * bf;
			}
			if (afm1 < 0 || bf < 0) {
				afm1 = b * v * v * Math.Exp(m + v / 2) / 4;
				bf = b * (1 + v / 2) * Math.Exp(m + v / 2) / 2;
			}
			result.Shape = afm1 + 1;
			result.Rate = bf;
			if (!result.IsProper())
				throw new ApplicationException("improper message calculated by ExpOp.DNonconjugateAverageLogarithm");
			return result;

		}
예제 #15
0
파일: Exp.cs 프로젝트: prgoodwin/HabilisX
		public static NonconjugateGaussian DAverageLogarithm([Proper] Gamma exp, [Proper, SkipIfUniform] NonconjugateGaussian d, NonconjugateGaussian result)
		{
			return DNonconjugateAverageLogarithm(exp, d.GetGaussian(true), result);
		}
예제 #16
0
            public void Initialize()
            {
                // DO NOT make this a constructor, because it makes the test not notice complete lack of serialization as an empty object is set up exactly as the thing
                // you are trying to deserialize.
                this.pareto  = new Pareto(1.2, 3.5);
                this.poisson = new Poisson(2.3);
                this.wishart = new Wishart(20, new PositiveDefiniteMatrix(new double[, ] {
                    { 22, 21 }, { 21, 23 }
                }));
                this.vectorGaussian = new VectorGaussian(Vector.FromArray(13, 14), new PositiveDefiniteMatrix(new double[, ] {
                    { 16, 15 }, { 15, 17 }
                }));
                this.unnormalizedDiscrete = UnnormalizedDiscrete.FromLogProbs(DenseVector.FromArray(5.1, 5.2, 5.3));
                this.pointMass            = PointMass <double> .Create(1.1);

                this.gaussian             = new Gaussian(11.0, 12.0);
                this.nonconjugateGaussian = new NonconjugateGaussian(1.2, 2.3, 3.4, 4.5);
                this.gamma              = new Gamma(9.0, 10.0);
                this.gammaPower         = new GammaPower(5.6, 2.8, 3.4);
                this.discrete           = new Discrete(6.0, 7.0, 8.0);
                this.conjugateDirichlet = new ConjugateDirichlet(1.2, 2.3, 3.4, 4.5);
                this.dirichlet          = new Dirichlet(3.0, 4.0, 5.0);
                this.beta      = new Beta(2.0, 1.0);
                this.binomial  = new Binomial(5, 0.8);
                this.bernoulli = new Bernoulli(0.6);

                this.sparseBernoulliList    = SparseBernoulliList.Constant(4, new Bernoulli(0.1));
                this.sparseBernoulliList[1] = new Bernoulli(0.9);
                this.sparseBernoulliList[3] = new Bernoulli(0.7);

                this.sparseBetaList    = SparseBetaList.Constant(5, new Beta(2.0, 2.0));
                this.sparseBetaList[0] = new Beta(3.0, 4.0);
                this.sparseBetaList[1] = new Beta(5.0, 6.0);

                this.sparseGaussianList    = SparseGaussianList.Constant(6, Gaussian.FromMeanAndPrecision(0.1, 0.2));
                this.sparseGaussianList[4] = Gaussian.FromMeanAndPrecision(0.3, 0.4);
                this.sparseGaussianList[5] = Gaussian.FromMeanAndPrecision(0.5, 0.6);

                this.sparseGammaList = SparseGammaList.Constant(1, Gamma.FromShapeAndRate(1.0, 2.0));

                this.truncatedGamma    = new TruncatedGamma(1.2, 2.3, 3.4, 4.5);
                this.truncatedGaussian = new TruncatedGaussian(1.2, 3.4, 5.6, 7.8);
                this.wrappedGaussian   = new WrappedGaussian(1.2, 2.3, 3.4);

                ga = Distribution <double> .Array(new[] { this.gaussian, this.gaussian });

                vga = Distribution <Vector> .Array(new[] { this.vectorGaussian, this.vectorGaussian });

                ga2D = Distribution <double> .Array(new[, ] {
                    { this.gaussian, this.gaussian }, { this.gaussian, this.gaussian }
                });

                vga2D = Distribution <Vector> .Array(new[, ] {
                    { this.vectorGaussian, this.vectorGaussian }, { this.vectorGaussian, this.vectorGaussian }
                });

                gaJ = Distribution <double> .Array(new[] { new[] { this.gaussian, this.gaussian }, new[] { this.gaussian, this.gaussian } });

                vgaJ = Distribution <Vector> .Array(new[] { new[] { this.vectorGaussian, this.vectorGaussian }, new[] { this.vectorGaussian, this.vectorGaussian } });

                var gp    = new GaussianProcess(new ConstantFunction(0), new SquaredExponential(0));
                var basis = Util.ArrayInit(2, i => Vector.FromArray(1.0 * i));

                this.sparseGp = new SparseGP(new SparseGPFixed(gp, basis));

                this.quantileEstimator = new QuantileEstimator(0.01);
                this.quantileEstimator.Add(5);
            }