private static void GetGaussianFromQuantiles(double x0, double p0, double x1, double p1, out double mean, out double deviation) { // solve for the Gaussian mean and stddev that yield: // x0 = mean + stddev * NormalCdfInv(p0) double z0 = MMath.NormalCdfInv(p0); double z1 = MMath.NormalCdfInv(p1); Matrix Z = new Matrix(new double[, ] { { 1, z0 }, { 1, z1 } }); DenseVector X = DenseVector.FromArray(x0, x1); DenseVector A = DenseVector.Zero(2); A.SetToLeastSquares(X, Z); mean = A[0]; deviation = A[1]; }
/// <summary>Computations that depend on the observed value of vVector__343</summary> private void Changed_vVector__343() { if (this.Changed_vVector__343_iterationsDone == 1) { return; } this.vVector__343_marginal = new PointMass <Vector[]>(this.VVector__343); // The constant 'vVectorGaussian343' VectorGaussian vVectorGaussian343 = VectorGaussian.FromNatural(DenseVector.FromArray(new double[3] { 1547829870.0, 525077980.0, 200270.0 }), new PositiveDefiniteMatrix(new double[3, 3] { { 4254590363351.0, 1127383488860.0, 433199230.0 }, { 1127383488860.0, 482723521821.0, 146764360.0 }, { 433199230.0, 146764360.0, 56221.0 } })); this.vVector1029_marginal_F = ArrayHelper.MakeUniform <VectorGaussian>(vVectorGaussian343); // Buffer for ReplicateOp_Divide.Marginal<VectorGaussian> VectorGaussian vVector1029_rep_B_toDef = default(VectorGaussian); // Message to 'vVector1029_rep' from Replicate factor vVector1029_rep_B_toDef = ReplicateOp_Divide.ToDefInit <VectorGaussian>(vVectorGaussian343); // Message to 'vVector1029_marginal' from Variable factor this.vVector1029_marginal_F = VariableOp.MarginalAverageConditional <VectorGaussian>(vVector1029_rep_B_toDef, vVectorGaussian343, this.vVector1029_marginal_F); DistributionStructArray <Gaussian, double> vdouble__1029_F = default(DistributionStructArray <Gaussian, double>); // Create array for 'vdouble__1029' Forwards messages. vdouble__1029_F = new DistributionStructArray <Gaussian, double>(1); for (int index343 = 0; index343 < 1; index343++) { vdouble__1029_F[index343] = Gaussian.Uniform(); } DistributionStructArray <Gaussian, double> vdouble__1030_F = default(DistributionStructArray <Gaussian, double>); // Create array for 'vdouble__1030' Forwards messages. vdouble__1030_F = new DistributionStructArray <Gaussian, double>(1); for (int index343 = 0; index343 < 1; index343++) { vdouble__1030_F[index343] = Gaussian.Uniform(); } DistributionRefArray <VectorGaussian, Vector> vVector1029_rep_F = default(DistributionRefArray <VectorGaussian, Vector>); DistributionRefArray <VectorGaussian, Vector> vVector1029_rep_B = default(DistributionRefArray <VectorGaussian, Vector>); // Create array for 'vVector1029_rep' Forwards messages. vVector1029_rep_F = new DistributionRefArray <VectorGaussian, Vector>(1); // Create array for 'vVector1029_rep' Backwards messages. vVector1029_rep_B = new DistributionRefArray <VectorGaussian, Vector>(1); for (int index343 = 0; index343 < 1; index343++) { vVector1029_rep_B[index343] = ArrayHelper.MakeUniform <VectorGaussian>(vVectorGaussian343); vVector1029_rep_F[index343] = ArrayHelper.MakeUniform <VectorGaussian>(vVectorGaussian343); } // Buffer for ReplicateOp_Divide.UsesAverageConditional<VectorGaussian> VectorGaussian vVector1029_rep_F_marginal = default(VectorGaussian); // Message to 'vVector1029_rep' from Replicate factor vVector1029_rep_F_marginal = ReplicateOp_Divide.MarginalInit <VectorGaussian>(vVectorGaussian343); // Message to 'vVector1029_rep' from Replicate factor vVector1029_rep_F_marginal = ReplicateOp_Divide.Marginal <VectorGaussian>(vVector1029_rep_B_toDef, vVectorGaussian343, vVector1029_rep_F_marginal); // Buffer for InnerProductOp.InnerProductAverageConditional // Create array for replicates of 'vVector1029_rep_F_index343__AMean' Vector[] vVector1029_rep_F_index343__AMean = new Vector[1]; for (int index343 = 0; index343 < 1; index343++) { // Message to 'vdouble__1030' from InnerProduct factor vVector1029_rep_F_index343__AMean[index343] = InnerProductOp.AMeanInit(vVector1029_rep_F[index343]); } // Buffer for InnerProductOp.AMean // Create array for replicates of 'vVector1029_rep_F_index343__AVariance' PositiveDefiniteMatrix[] vVector1029_rep_F_index343__AVariance = new PositiveDefiniteMatrix[1]; for (int index343 = 0; index343 < 1; index343++) { // Message to 'vdouble__1030' from InnerProduct factor vVector1029_rep_F_index343__AVariance[index343] = InnerProductOp.AVarianceInit(vVector1029_rep_F[index343]); // Message to 'vVector1029_rep' from Replicate factor vVector1029_rep_F[index343] = ReplicateOp_Divide.UsesAverageConditional <VectorGaussian>(vVector1029_rep_B[index343], vVector1029_rep_F_marginal, index343, vVector1029_rep_F[index343]); } // Create array for 'vdouble__1030_marginal' Forwards messages. this.vdouble__1030_marginal_F = new DistributionStructArray <Gaussian, double>(1); for (int index343 = 0; index343 < 1; index343++) { this.vdouble__1030_marginal_F[index343] = Gaussian.Uniform(); } // Message from use of 'vdouble__1030' DistributionStructArray <Gaussian, double> vdouble__1030_use_B = default(DistributionStructArray <Gaussian, double>); // Create array for 'vdouble__1030_use' Backwards messages. vdouble__1030_use_B = new DistributionStructArray <Gaussian, double>(1); for (int index343 = 0; index343 < 1; index343++) { vdouble__1030_use_B[index343] = Gaussian.Uniform(); // Message to 'vdouble__1030' from InnerProduct factor vVector1029_rep_F_index343__AVariance[index343] = InnerProductOp.AVariance(vVector1029_rep_F[index343], vVector1029_rep_F_index343__AVariance[index343]); // Message to 'vdouble__1030' from InnerProduct factor vVector1029_rep_F_index343__AMean[index343] = InnerProductOp.AMean(vVector1029_rep_F[index343], vVector1029_rep_F_index343__AVariance[index343], vVector1029_rep_F_index343__AMean[index343]); // Message to 'vdouble__1030' from InnerProduct factor vdouble__1030_F[index343] = InnerProductOp.InnerProductAverageConditional(vVector1029_rep_F_index343__AMean[index343], vVector1029_rep_F_index343__AVariance[index343], this.VVector__343[index343]); // Message to 'vdouble__1030_marginal' from DerivedVariable factor this.vdouble__1030_marginal_F[index343] = DerivedVariableOp.MarginalAverageConditional <Gaussian>(vdouble__1030_use_B[index343], vdouble__1030_F[index343], this.vdouble__1030_marginal_F[index343]); } // Create array for 'vdouble__1029_marginal' Forwards messages. this.vdouble__1029_marginal_F = new DistributionStructArray <Gaussian, double>(1); for (int index343 = 0; index343 < 1; index343++) { this.vdouble__1029_marginal_F[index343] = Gaussian.Uniform(); } // Message from use of 'vdouble__1029' DistributionStructArray <Gaussian, double> vdouble__1029_use_B = default(DistributionStructArray <Gaussian, double>); // Create array for 'vdouble__1029_use' Backwards messages. vdouble__1029_use_B = new DistributionStructArray <Gaussian, double>(1); for (int index343 = 0; index343 < 1; index343++) { vdouble__1029_use_B[index343] = Gaussian.Uniform(); // Message to 'vdouble__1029' from GaussianFromMeanAndVariance factor vdouble__1029_F[index343] = GaussianFromMeanAndVarianceOp.SampleAverageConditional(vdouble__1030_F[index343], 0.1); // Message to 'vdouble__1029_marginal' from Variable factor this.vdouble__1029_marginal_F[index343] = VariableOp.MarginalAverageConditional <Gaussian>(vdouble__1029_use_B[index343], vdouble__1029_F[index343], this.vdouble__1029_marginal_F[index343]); } this.Changed_vVector__343_iterationsDone = 1; }
/// <summary>Computations that depend on the observed value of vVector__134 and vdouble__402</summary> private void Changed_vVector__134_vdouble__402() { if (this.Changed_vVector__134_vdouble__402_iterationsDone == 1) { return; } this.vVector__134_marginal = new PointMass <Vector[]>(this.VVector__134); this.vdouble__402_marginal = new DistributionStructArray <Gaussian, double>(5622, delegate(int index134) { return(Gaussian.Uniform()); }); this.vdouble__402_marginal = Distribution.SetPoint <DistributionStructArray <Gaussian, double>, double[]>(this.vdouble__402_marginal, this.Vdouble__402); // The constant 'vVectorGaussian134' VectorGaussian vVectorGaussian134 = VectorGaussian.FromNatural(DenseVector.FromArray(new double[3] { 0.0, 0.0, 0.0 }), new PositiveDefiniteMatrix(new double[3, 3] { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 } })); this.vVector403_marginal_F = ArrayHelper.MakeUniform <VectorGaussian>(vVectorGaussian134); // Message from use of 'vdouble__403' DistributionStructArray <Gaussian, double> vdouble__403_use_B = default(DistributionStructArray <Gaussian, double>); // Create array for 'vdouble__403_use' Backwards messages. vdouble__403_use_B = new DistributionStructArray <Gaussian, double>(5622); for (int index134 = 0; index134 < 5622; index134++) { vdouble__403_use_B[index134] = Gaussian.Uniform(); // Message to 'vdouble__403_use' from GaussianFromMeanAndVariance factor vdouble__403_use_B[index134] = GaussianFromMeanAndVarianceOp.MeanAverageConditional(this.Vdouble__402[index134], 0.1); } DistributionRefArray <VectorGaussian, Vector> vVector403_rep_B = default(DistributionRefArray <VectorGaussian, Vector>); // Create array for 'vVector403_rep' Backwards messages. vVector403_rep_B = new DistributionRefArray <VectorGaussian, Vector>(5622); for (int index134 = 0; index134 < 5622; index134++) { vVector403_rep_B[index134] = ArrayHelper.MakeUniform <VectorGaussian>(vVectorGaussian134); // Message to 'vVector403_rep' from InnerProduct factor vVector403_rep_B[index134] = InnerProductOp.AAverageConditional(vdouble__403_use_B[index134], this.VVector__134[index134], vVector403_rep_B[index134]); } // Buffer for ReplicateOp_Divide.Marginal<VectorGaussian> VectorGaussian vVector403_rep_B_toDef = default(VectorGaussian); // Message to 'vVector403_rep' from Replicate factor vVector403_rep_B_toDef = ReplicateOp_Divide.ToDefInit <VectorGaussian>(vVectorGaussian134); // Message to 'vVector403_rep' from Replicate factor vVector403_rep_B_toDef = ReplicateOp_Divide.ToDef <VectorGaussian>(vVector403_rep_B, vVector403_rep_B_toDef); // Message to 'vVector403_marginal' from Variable factor this.vVector403_marginal_F = VariableOp.MarginalAverageConditional <VectorGaussian>(vVector403_rep_B_toDef, vVectorGaussian134, this.vVector403_marginal_F); DistributionStructArray <Gaussian, double> vdouble__403_F = default(DistributionStructArray <Gaussian, double>); // Create array for 'vdouble__403' Forwards messages. vdouble__403_F = new DistributionStructArray <Gaussian, double>(5622); for (int index134 = 0; index134 < 5622; index134++) { vdouble__403_F[index134] = Gaussian.Uniform(); } DistributionRefArray <VectorGaussian, Vector> vVector403_rep_F = default(DistributionRefArray <VectorGaussian, Vector>); // Create array for 'vVector403_rep' Forwards messages. vVector403_rep_F = new DistributionRefArray <VectorGaussian, Vector>(5622); for (int index134 = 0; index134 < 5622; index134++) { vVector403_rep_F[index134] = ArrayHelper.MakeUniform <VectorGaussian>(vVectorGaussian134); } // Buffer for ReplicateOp_Divide.UsesAverageConditional<VectorGaussian> VectorGaussian vVector403_rep_F_marginal = default(VectorGaussian); // Message to 'vVector403_rep' from Replicate factor vVector403_rep_F_marginal = ReplicateOp_Divide.MarginalInit <VectorGaussian>(vVectorGaussian134); // Message to 'vVector403_rep' from Replicate factor vVector403_rep_F_marginal = ReplicateOp_Divide.Marginal <VectorGaussian>(vVector403_rep_B_toDef, vVectorGaussian134, vVector403_rep_F_marginal); // Buffer for InnerProductOp.InnerProductAverageConditional // Create array for replicates of 'vVector403_rep_F_index134__AMean' Vector[] vVector403_rep_F_index134__AMean = new Vector[5622]; for (int index134 = 0; index134 < 5622; index134++) { // Message to 'vdouble__403' from InnerProduct factor vVector403_rep_F_index134__AMean[index134] = InnerProductOp.AMeanInit(vVector403_rep_F[index134]); } // Buffer for InnerProductOp.AMean // Create array for replicates of 'vVector403_rep_F_index134__AVariance' PositiveDefiniteMatrix[] vVector403_rep_F_index134__AVariance = new PositiveDefiniteMatrix[5622]; for (int index134 = 0; index134 < 5622; index134++) { // Message to 'vdouble__403' from InnerProduct factor vVector403_rep_F_index134__AVariance[index134] = InnerProductOp.AVarianceInit(vVector403_rep_F[index134]); // Message to 'vVector403_rep' from Replicate factor vVector403_rep_F[index134] = ReplicateOp_Divide.UsesAverageConditional <VectorGaussian>(vVector403_rep_B[index134], vVector403_rep_F_marginal, index134, vVector403_rep_F[index134]); } // Create array for 'vdouble__403_marginal' Forwards messages. this.vdouble__403_marginal_F = new DistributionStructArray <Gaussian, double>(5622); for (int index134 = 0; index134 < 5622; index134++) { this.vdouble__403_marginal_F[index134] = Gaussian.Uniform(); // Message to 'vdouble__403' from InnerProduct factor vVector403_rep_F_index134__AVariance[index134] = InnerProductOp.AVariance(vVector403_rep_F[index134], vVector403_rep_F_index134__AVariance[index134]); // Message to 'vdouble__403' from InnerProduct factor vVector403_rep_F_index134__AMean[index134] = InnerProductOp.AMean(vVector403_rep_F[index134], vVector403_rep_F_index134__AVariance[index134], vVector403_rep_F_index134__AMean[index134]); // Message to 'vdouble__403' from InnerProduct factor vdouble__403_F[index134] = InnerProductOp.InnerProductAverageConditional(vVector403_rep_F_index134__AMean[index134], vVector403_rep_F_index134__AVariance[index134], this.VVector__134[index134]); // Message to 'vdouble__403_marginal' from DerivedVariable factor this.vdouble__403_marginal_F[index134] = DerivedVariableOp.MarginalAverageConditional <Gaussian>(vdouble__403_use_B[index134], vdouble__403_F[index134], this.vdouble__403_marginal_F[index134]); } this.Changed_vVector__134_vdouble__402_iterationsDone = 1; }
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 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); }