public static Gamma RateAverageConditional(TruncatedGamma sample, double shape, double lowerBound, double upperBound)
 {
     if (sample.IsPointMass)
     {
         return(RateAverageConditional(sample.Point, shape, lowerBound, upperBound));
     }
     if (shape != 1)
     {
         throw new NotImplementedException();
     }
     if (!double.IsPositiveInfinity(upperBound))
     {
         throw new NotImplementedException();
     }
     // when shape=1 and upperBound=infinity, the factor reduces to:  f(x,b) = b exp(-b(x-L))
     if (!sample.Gamma.IsUniform())
     {
         throw new NotImplementedException();
     }
     if (!double.IsPositiveInfinity(sample.UpperBound))
     {
         throw new NotImplementedException();
     }
     if (sample.LowerBound <= lowerBound)
     {
         return(Gamma.Uniform());
     }
     // when the message from x is uniform with a lower bound of L2 > L, then f(b) = int_L2^inf b exp(-b(x-L)) dx = exp(-b(L2-L))
     return(Gamma.FromShapeAndRate(shape, sample.LowerBound - lowerBound));
 }
示例#2
0
        // GammaPower = TruncatedGamma ^ y  /////////////////////////////////////////////////////////

        public static GammaPower PowAverageConditional([SkipIfUniform] TruncatedGamma x, double y, GammaPower result)
        {
            if (result.Power == -1)
            {
                y = -y;
            }
            else if (result.Power != 1)
            {
                throw new ArgumentException($"result.Power ({result.Power}) is not 1 or -1", nameof(result));
            }
            double mean = x.GetMeanPower(y);

            if (x.LowerBound > 0)
            {
                double meanInverse = x.GetMeanPower(-y);
                Gamma  result1     = GammaFromMeanAndMeanInverse(mean, meanInverse);
                return(GammaPower.FromShapeAndRate(result1.Shape, result1.Rate, result.Power));
            }
            else
            {
                double variance = x.GetMeanPower(2 * y) - mean * mean;
                Gamma  result1  = Gamma.FromMeanAndVariance(mean, variance);
                return(GammaPower.FromShapeAndRate(result1.Shape, result1.Rate, result.Power));
            }
        }
示例#3
0
 public static Gamma CopyAverageConditional(TruncatedGamma value)
 {
     if (!value.IsPointMass)
     {
         throw new ArgumentException("value is not a point mass");
     }
     return(value.ToGamma());
 }
        public static RpropBufferData BufferTGa([NoInit] TruncatedGamma use, TruncatedGamma def, TruncatedGamma to_marginal, RpropBufferData bufferTGa)
        {
            var currDist = use * def;

            if (currDist.IsPointMass)
            {
                if (double.IsInfinity(currDist.Point) || double.IsNaN(currDist.Point))
                {
                    throw new ArgumentOutOfRangeException();
                }
                if (VariablePointOp_RpropGamma.UseMean)
                {
                    bufferTGa.nextPoint = Math.Log(currDist.Point);
                }
                else
                {
                    bufferTGa.nextPoint = currDist.Point;
                }
                return(bufferTGa);
            }
            // cannot use buffer.nextPoint as currPoint since Marginal could be initialized by user
            double currPoint;

            if (to_marginal.IsPointMass)
            {
                currPoint = to_marginal.Point;
            }
            else
            {
                currPoint = currDist.GetMean();
            }
            double currDeriv, currDeriv2;

            if (VariablePointOp_RpropGamma.UseMean)
            {
                bufferTGa.lowerBound = Math.Log(currDist.LowerBound);
                bufferTGa.upperBound = Math.Log(currDist.UpperBound);
                currDeriv            = currDist.Gamma.Shape - currDist.Gamma.Rate * currPoint;
                //Trace.WriteLine($"use deriv = {(use.Gamma.Shape-1) - use.Gamma.Rate*currPoint} def deriv = {def.Gamma.Shape - def.Gamma.Rate*currPoint} total deriv = {currDeriv}");
                if (currPoint <= 0)
                {
                    throw new ArgumentException($"currPoint ({currPoint}) <= 0");
                }
                bufferTGa.SetNextPoint(Math.Log(currPoint), currDeriv);
            }
            else
            {
                bufferTGa.lowerBound = currDist.LowerBound;
                bufferTGa.upperBound = currDist.UpperBound;
                currDist.Gamma.GetDerivatives(currPoint, out currDeriv, out currDeriv2);
                bufferTGa.SetNextPoint(currPoint, currDeriv);
            }
            return(bufferTGa);
        }
示例#5
0
 public static TruncatedGamma AAverageConditional([SkipIfUniform] TruncatedGamma min, double b)
 {
     if (min.IsUniform())
     {
         return(TruncatedGamma.Uniform());
     }
     if (!min.IsPointMass)
     {
         throw new ArgumentException("min is not a point mass");
     }
     return(TruncatedGamma.PointMass(min.Point));
 }
 /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="VariablePointOp_RpropTruncatedGamma"]/message_doc[@name="MarginalAverageConditional(TruncatedGamma, TruncatedGamma, RpropBufferData, TruncatedGamma)"]/*'/>
 public static TruncatedGamma MarginalAverageConditional([IgnoreDependency] TruncatedGamma use, [IgnoreDependency] TruncatedGamma def, [RequiredArgument] RpropBufferData bufferTGa, TruncatedGamma result)
 {
     if (VariablePointOp_RpropGamma.UseMean)
     {
         result.Point = Math.Exp(bufferTGa.nextPoint);
     }
     else
     {
         result.Point = bufferTGa.nextPoint;
     }
     return(result);
 }
示例#7
0
        // Gamma = TruncatedGamma ^ y  /////////////////////////////////////////////////////////

        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="PowerOp"]/message_doc[@name="PowAverageConditional(TruncatedGamma, double)"]/*'/>
        public static Gamma PowAverageConditional([SkipIfUniform] TruncatedGamma x, double y)
        {
            double mean = x.GetMeanPower(y);

            if (x.LowerBound > 0)
            {
                double meanInverse = x.GetMeanPower(-y);
                return(GammaFromMeanAndMeanInverse(mean, meanInverse));
            }
            else
            {
                double variance = x.GetMeanPower(2 * y) - mean * mean;
                return(Gamma.FromMeanAndVariance(mean, variance));
            }
        }
示例#8
0
        public static TruncatedGamma XAverageConditional([SkipIfUniform] Gamma pow, TruncatedGamma x, double y)
        {
            // message computed below should be uniform when pow is uniform, but may not due to roundoff error.
            if (pow.IsUniform())
            {
                return(TruncatedGamma.Uniform());
            }
            // Factor is (x^y)^(pow.Shape/pow.Power - 1) * exp(-pow.Rate*(x^y)^1/pow.Power)
            // =propto x^(pow.Shape/(pow.Power/y) - y) * exp(-pow.Rate*x^y/pow.Power)
            // newShape/(pow.Power/y) - 1 = pow.Shape/(pow.Power/y) - y
            // newShape = pow.Shape + (1-y)*(pow.Power/y)
            double power       = 1 / y;
            var    toPow       = PowAverageConditional(x, y);
            var    powMarginal = pow * toPow;
            // xMarginal2 is the exact distribution of pow^(1/y) where pow has distribution powMarginal
            GammaPower xMarginal2 = GammaPower.FromShapeAndRate(powMarginal.Shape, powMarginal.Rate, power);
            var        xMarginal  = new TruncatedGamma(GammaFromGammaPower(xMarginal2), x.LowerBound, x.UpperBound);
            var        result     = xMarginal;

            result.SetToRatio(xMarginal, x, GammaProductOp_Laplace.ForceProper);
            return(result);
        }
示例#9
0
 public static TruncatedGamma BAverageConditional([SkipIfUniform] TruncatedGamma min, double a)
 {
     return(AAverageConditional(min, a));
 }
示例#10
0
 public static TruncatedGamma MinAverageConditional(double a, TruncatedGamma b)
 {
     return(MinAverageConditional(b, a));
 }
示例#11
0
 public static TruncatedGamma MinAverageConditional(TruncatedGamma a, double b)
 {
     return(new TruncatedGamma(a.Gamma, a.LowerBound, Math.Min(b, a.UpperBound)));
 }
示例#12
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"));
                }
            }
 public static Gamma RateAverageLogarithm(TruncatedGamma sample, double shape, double lowerBound, double upperBound)
 {
     return(RateAverageConditional(sample, shape, lowerBound, upperBound));
 }
示例#14
0
文件: Factor.cs 项目: kant2002/infer
 public static double TruncatedGammaFromShapeAndRate(double shape, double rate, double lowerBound, double upperBound)
 {
     return(TruncatedGamma.Sample(Gamma.FromShapeAndRate(shape, rate), lowerBound, upperBound));
 }
示例#15
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);
            }