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 SparseBernoulliFromBetaFactor() { var calcSuffix = ": calculation differs between sparse and dense"; var sparsitySuffix = ": result is not sparse as expected"; var calcErrMsg = ""; var sparsityErrMsg = ""; var tolerance = 1e-10; Rand.Restart(12347); int listSize = 50; var sparseProbTrueDist = SparseBetaList.Constant(listSize, new Beta(1, 2)); sparseProbTrueDist[3] = new Beta(4, 5); sparseProbTrueDist[6] = new Beta(7, 8); var probTrueDist = sparseProbTrueDist.ToArray(); var sparseProbTruePoint = SparseList <double> .Constant(listSize, 0.1); sparseProbTruePoint[3] = 0.7; sparseProbTruePoint[6] = 0.8; var probTruePoint = sparseProbTruePoint.ToArray(); var sparseSampleDist = SparseBernoulliList.Constant(listSize, new Bernoulli(0.1)); sparseSampleDist[3] = new Bernoulli(0.8); sparseSampleDist[9] = new Bernoulli(0.9); var sampleDist = sparseSampleDist.ToArray(); var sparseSamplePoint = SparseList <bool> .Constant(listSize, false); sparseSamplePoint[3] = true; sparseSamplePoint[9] = true; var samplePoint = sparseSamplePoint.ToArray(); var toSparseSampleDist = SparseBernoulliList.Constant(listSize, new Bernoulli(0.1)); toSparseSampleDist[3] = new Bernoulli(0.4); toSparseSampleDist[4] = new Bernoulli(0.8); var toSampleDist = toSparseSampleDist.ToArray(); // --------------------------- // Check average log factor // --------------------------- calcErrMsg = "Average log factor" + calcSuffix; // Dist, dist var sparseAvgLog = SparseBernoulliFromBetaOp.AverageLogFactor(sparseSampleDist, sparseProbTrueDist); var avgLog = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.AverageLogFactor(sampleDist[i], probTrueDist[i])).Sum(); TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg); // Dist, point sparseAvgLog = SparseBernoulliFromBetaOp.AverageLogFactor(sparseSampleDist, sparseProbTruePoint); avgLog = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.AverageLogFactor(sampleDist[i], probTruePoint[i])).Sum(); TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg); // Point, dist sparseAvgLog = SparseBernoulliFromBetaOp.AverageLogFactor(sparseSamplePoint, sparseProbTrueDist); avgLog = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.AverageLogFactor(samplePoint[i], probTrueDist[i])).Sum(); TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg); // Point, point sparseAvgLog = SparseBernoulliFromBetaOp.AverageLogFactor(sparseSamplePoint, sparseProbTruePoint); avgLog = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.AverageLogFactor(samplePoint[i], probTruePoint[i])).Sum(); TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg); // --------------------------- // Check log average factor // --------------------------- calcErrMsg = "Log average factor" + calcSuffix; var sparseLogAvg = SparseBernoulliFromBetaOp.LogAverageFactor(sparseSampleDist, toSparseSampleDist); var logAvg = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.LogAverageFactor(sampleDist[i], toSampleDist[i])).Sum(); TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg); sparseLogAvg = SparseBernoulliFromBetaOp.LogAverageFactor(sparseSamplePoint, sparseProbTrueDist); logAvg = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.LogAverageFactor(samplePoint[i], probTrueDist[i])).Sum(); TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg); sparseLogAvg = SparseBernoulliFromBetaOp.LogAverageFactor(sparseSamplePoint, sparseProbTruePoint); logAvg = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.LogAverageFactor(samplePoint[i], probTruePoint[i])).Sum(); TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg); // --------------------------- // Check log evidence ratio // --------------------------- calcErrMsg = "Log evidence ratio" + calcSuffix; // Dist, dist var sparseEvidRat = SparseBernoulliFromBetaOp.LogEvidenceRatio(sparseSampleDist, sparseProbTrueDist); var evidRat = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.LogEvidenceRatio(sampleDist[i], probTrueDist[i])).Sum(); TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg); // Dist, point sparseEvidRat = SparseBernoulliFromBetaOp.LogEvidenceRatio(sparseSampleDist, sparseProbTruePoint); evidRat = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.LogEvidenceRatio(sampleDist[i], probTruePoint[i])).Sum(); TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg); // Point, dist sparseEvidRat = SparseBernoulliFromBetaOp.LogEvidenceRatio(sparseSamplePoint, sparseProbTrueDist); evidRat = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.LogEvidenceRatio(samplePoint[i], probTrueDist[i])).Sum(); TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg); // Point, point sparseEvidRat = SparseBernoulliFromBetaOp.LogEvidenceRatio(sparseSamplePoint, sparseProbTruePoint); evidRat = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.LogEvidenceRatio(samplePoint[i], probTruePoint[i])).Sum(); TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg); // --------------------------- // Check SampleConditional // --------------------------- calcErrMsg = "SampleConditional" + calcSuffix; sparsityErrMsg = "SampleConditional" + sparsitySuffix; // Use different common value to ensure this gets properly set var sparseSampleConditional = SparseBernoulliList.Constant(listSize, new Bernoulli(0.5)); sparseSampleConditional = SparseBernoulliFromBetaOp.SampleConditional(sparseProbTruePoint, sparseSampleConditional); var sampleConditional = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.SampleConditional(probTruePoint[i])); TAssert.True(2 == sparseSampleConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseSampleConditional.MaxDiff(sampleConditional) < tolerance, calcErrMsg); // --------------------------- // Check SampleAverageConditional // --------------------------- calcErrMsg = "SampleAverageConditional" + calcSuffix; sparsityErrMsg = "SampleAverageConditional" + sparsitySuffix; // Use different common value to ensure this gets properly set var sparseSampleAvgConditional = SparseBernoulliList.Constant(listSize, new Bernoulli(0.5)); sparseSampleAvgConditional = SparseBernoulliFromBetaOp.SampleAverageConditional(sparseProbTrueDist, sparseSampleAvgConditional); var sampleAvgConditional = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.SampleAverageConditional(probTrueDist[i])); TAssert.True(2 == sparseSampleAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseSampleAvgConditional.MaxDiff(sampleAvgConditional) < tolerance, calcErrMsg); sparseSampleAvgConditional = SparseBernoulliList.Constant(listSize, new Bernoulli(0.5)); sparseSampleAvgConditional = SparseBernoulliFromBetaOp.SampleAverageConditional(sparseProbTruePoint, sparseSampleAvgConditional); sampleAvgConditional = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.SampleAverageConditional(probTruePoint[i])); TAssert.True(2 == sparseSampleAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseSampleAvgConditional.MaxDiff(sampleAvgConditional) < tolerance, calcErrMsg); // --------------------------- // Check ProbTrueConditional // --------------------------- calcErrMsg = "ProbTrueConditional" + calcSuffix; sparsityErrMsg = "ProbTrueConditional" + sparsitySuffix; // Use different common value to ensure this gets properly set var sparseProbTrueConditional = SparseBetaList.Constant(listSize, new Beta(1.1, 2.2)); sparseProbTrueConditional = SparseBernoulliFromBetaOp.ProbTrueConditional(sparseSamplePoint, sparseProbTrueConditional); var probTrueConditional = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.ProbTrueConditional(samplePoint[i])); TAssert.True(2 == sparseProbTrueConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseProbTrueConditional.MaxDiff(probTrueConditional) < tolerance, calcErrMsg); // --------------------------- // Check ProbTrueAverageConditional // --------------------------- calcErrMsg = "ProbTrueAverageConditional" + calcSuffix; sparsityErrMsg = "ProbTrueAverageConditional" + sparsitySuffix; // Use different common value to ensure this gets properly set var sparseProbTrueAvgConditional = SparseBetaList.Constant(listSize, new Beta(1.1, 2.2)); sparseProbTrueAvgConditional = SparseBernoulliFromBetaOp.ProbTrueAverageConditional(sparseSampleDist, sparseProbTrueDist, sparseProbTrueAvgConditional); var probTrueAvgConditional = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.ProbTrueAverageConditional(sampleDist[i], probTrueDist[i])); TAssert.True(2 == sparseProbTrueAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseProbTrueAvgConditional.MaxDiff(probTrueAvgConditional) < tolerance, calcErrMsg); sparseProbTrueAvgConditional = SparseBetaList.Constant(listSize, new Beta(1.1, 2.2)); sparseProbTrueAvgConditional = SparseBernoulliFromBetaOp.ProbTrueAverageConditional(sparseSamplePoint, sparseProbTrueAvgConditional); probTrueAvgConditional = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.ProbTrueAverageConditional(samplePoint[i])); TAssert.True(2 == sparseProbTrueAvgConditional.SparseCount, sparsityErrMsg); TAssert.True(sparseProbTrueAvgConditional.MaxDiff(probTrueAvgConditional) < tolerance, calcErrMsg); // --------------------------- // Check SampleAverageLogarithm // --------------------------- calcErrMsg = "SampleAverageLogarithm" + calcSuffix; sparsityErrMsg = "SampleAverageLogarithm" + sparsitySuffix; // Use different common value to ensure this gets properly set var sparseSampleAvgLogarithm = SparseBernoulliList.Constant(listSize, new Bernoulli(0.5)); sparseSampleAvgLogarithm = SparseBernoulliFromBetaOp.SampleAverageLogarithm(sparseProbTrueDist, sparseSampleAvgLogarithm); var sampleAvgLogarithm = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.SampleAverageLogarithm(probTrueDist[i])); TAssert.True(2 == sparseSampleAvgLogarithm.SparseCount, sparsityErrMsg); TAssert.True(sparseSampleAvgLogarithm.MaxDiff(sampleAvgLogarithm) < tolerance, calcErrMsg); sparseSampleAvgLogarithm = SparseBernoulliList.Constant(listSize, new Bernoulli(0.5)); sparseSampleAvgLogarithm = SparseBernoulliFromBetaOp.SampleAverageLogarithm(sparseProbTruePoint, sparseSampleAvgLogarithm); sampleAvgLogarithm = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.SampleAverageLogarithm(probTruePoint[i])); TAssert.True(2 == sparseSampleAvgLogarithm.SparseCount, sparsityErrMsg); TAssert.True(sparseSampleAvgLogarithm.MaxDiff(sampleAvgLogarithm) < tolerance, calcErrMsg); // --------------------------- // Check ProbTrueAverageLogarithm // --------------------------- calcErrMsg = "ProbTrueAverageLogarithm" + calcSuffix; sparsityErrMsg = "ProbTrueAverageLogarithm" + sparsitySuffix; // Use different common value to ensure this gets properly set var sparseProbTrueAvgLogarithm = SparseBetaList.Constant(listSize, new Beta(1.1, 2.2)); sparseProbTrueAvgLogarithm = SparseBernoulliFromBetaOp.ProbTrueAverageLogarithm(sparseSampleDist, sparseProbTrueAvgLogarithm); var probTrueAvgLogarithm = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.ProbTrueAverageLogarithm(sampleDist[i])); TAssert.True(2 == sparseProbTrueAvgLogarithm.SparseCount, sparsityErrMsg); TAssert.True(sparseProbTrueAvgLogarithm.MaxDiff(probTrueAvgLogarithm) < tolerance, calcErrMsg); sparseProbTrueAvgLogarithm = SparseBetaList.Constant(listSize, new Beta(1.1, 2.2)); sparseProbTrueAvgLogarithm = SparseBernoulliFromBetaOp.ProbTrueAverageLogarithm(sparseSamplePoint, sparseProbTrueAvgLogarithm); probTrueAvgLogarithm = Util.ArrayInit(listSize, i => BernoulliFromBetaOp.ProbTrueAverageLogarithm(samplePoint[i])); TAssert.True(2 == sparseProbTrueAvgLogarithm.SparseCount, sparsityErrMsg); TAssert.True(sparseProbTrueAvgLogarithm.MaxDiff(probTrueAvgLogarithm) < tolerance, calcErrMsg); }
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); }