public static IList <double> Sample(IList <double> means, IList <double> precs)
        {
            SparseList <double> sample = SparseList <double> .FromSize(means.Count);

            sample.SetToFunction(means, precs, (m, p) => Gaussian.Sample(m, p));
            return(sample);
        }
        public static IList <TResult> ListZip <TFirst, TSecond, TThird, TFourth, TResult>(
            this IList <TFirst> first,
            IList <TSecond> second,
            IList <TThird> third,
            IList <TFourth> fourth,
            Func <TFirst, TSecond, TThird, TFourth, TResult> fun)
        //where TResult : Diffable
        {
            if (first is ISparseList <TFirst> &&
                second is ISparseList <TSecond> &&
                third is ISparseList <TThird> &&
                fourth is ISparseList <TFourth> )
            {
                var result = SparseList <TResult> .FromSize(first.Count);

                //var result = ApproximateSparseList<TResult>.FromSize(first.Count, 1e-6);
                result.SetToFunction((ISparseList <TFirst>)first, (ISparseList <TSecond>)second, (ISparseList <TThird>)third, (ISparseList <TFourth>)fourth, fun);
                return(result);
            }
            else
            {
                var result = new List <TResult>();
                using (IEnumerator <TFirst> e1 = first.GetEnumerator())
                    using (IEnumerator <TSecond> e2 = second.GetEnumerator())
                        using (IEnumerator <TThird> e3 = third.GetEnumerator())
                            using (IEnumerator <TFourth> e4 = fourth.GetEnumerator())
                                while (e1.MoveNext() && e2.MoveNext() && e3.MoveNext() && e4.MoveNext())
                                {
                                    result.Add(fun(e1.Current, e2.Current, e3.Current, e4.Current));
                                }
                return(result);
            }
        }
        public static IList <TResult> ListZip <TFirst, TSecond, TThird, TResult>(
            this IList <TFirst> first,
            IEnumerable <TSecond> second,
            IEnumerable <TThird> third,
            Func <TFirst, TSecond, TThird, TResult> fun)
        {
            if (first is ISparseEnumerable <TFirst> &&
                second is ISparseEnumerable <TSecond> &&
                third is ISparseEnumerable <TThird> )
            {
                var result = SparseList <TResult> .FromSize(first.Count);

                result.SetToFunction((SparseList <TFirst>)first, (SparseList <TSecond>)second, (SparseList <TThird>)third, fun);
                return(result);
            }
            else
            {
                var result = new List <TResult>();
                using (IEnumerator <TFirst> e1 = first.GetEnumerator())
                    using (IEnumerator <TSecond> e2 = second.GetEnumerator())
                        using (IEnumerator <TThird> e3 = third.GetEnumerator())
                            while (e1.MoveNext() && e2.MoveNext() && e3.MoveNext())
                            {
                                result.Add(fun(e1.Current, e2.Current, e3.Current));
                            }
                return(result);
            }
        }
        public static ISparseList <double> Sample(ISparseList <double> trueCounts, ISparseList <double> falseCounts)
        {
            SparseList <double> sample = SparseList <double> .FromSize(trueCounts.Count);

            sample.SetToFunction(trueCounts, falseCounts, (tc, fc) => Beta.Sample(tc, fc));
            return(sample);
        }
Example #5
0
        public static ISparseList <double> Sample(ISparseList <double> shapes, ISparseList <double> rates)
        {
            SparseList <double> sample = SparseList <double> .FromSize(shapes.Count);

            sample.SetToFunction(shapes, rates, (s, r) => Gamma.Sample(s, r));
            return(sample);
        }
        public static ISparseList <bool> Sample(ISparseList <double> probTrue)
        {
            SparseList <bool> sample = SparseList <bool> .FromSize(probTrue.Count);

            sample.SetToFunction(probTrue, p => Bernoulli.Sample(p));
            return(sample);
        }
        /// <summary>
        /// Similar to LINQ Select, except it takes and returns a list.
        /// If the supplied list is sparse, the returned list will also be sparse.
        /// </summary>
        /// <typeparam name="T">The type of this list</typeparam>
        /// <typeparam name="T2">The target element type</typeparam>
        /// <param name="source">This list</param>
        /// <param name="converter">The converter</param>
        /// <returns></returns>
        public static IList <T2> ListSelect <T, T2>(this IList <T> source, Func <T, T2> converter)
        {
            if (source is SparseList <T> )
            {
                var target = SparseList <T2> .FromSize(source.Count);

                target.SetToFunction <T>(source, converter);
                return(target);
            }
            return(source.Select(converter).ToList());
        }
Example #8
0
        /// <summary>
        /// Converts index subset to a sparse list of boolean values.
        /// </summary>
        /// <param name="indices">The indices.</param>
        /// <param name="length">The length of the boolean list.</param>
        /// <returns>The equivalent sparse list of boolean values.</returns>
        public static ISparseList <bool> SubsetToList(IList <int> indices, int length)
        {
            var result = SparseList <bool> .Constant(length, false);

            foreach (int index in indices)
            {
                result[index] = true;
            }

            return(result);
        }
Example #9
0
        /// <summary>
        /// Gets the log probability of the given value under this distribution.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The log probability of the given value under this distribution.</returns>
        public double GetLogProb(IList <int> value)
        {
            var boolValue = SparseList <bool> .Constant(this.SparseBernoulliList.Count, false);

            foreach (int index in value)
            {
                boolValue[index] = true;
            }

            return(this.SparseBernoulliList.GetLogProb(boolValue));
        }
Example #10
0
        /// <summary>
        /// Gets the variance as a sparse list.
        /// </summary>
        /// <returns>The variance of this distribution.</returns>
        public ISparseList <double> GetVariance()
        {
            ISparseList <double> result = SparseList <double> .Constant(Count, CommonValue.GetVariance());

            var sen = GetSparseEnumerator();

            while (sen.MoveNext())
            {
                result[sen.CurrentIndex] = sen.Current.GetVariance();
            }

            return(result);
        }
Example #11
0
        // Returns array of triangle lists (stored as arrays)
        // This requires 2 passes over mesh, but each pass is linear
        public static int[][] FindTriangleSetsByGroup(NGonsCore.geometry3Sharp.mesh.DMesh3 mesh, int ignoreGID = int.MinValue)
        {
            if (!mesh.HasTriangleGroups)
            {
                return(new int[0][]);
            }

            // find # of groups and triangle count for each
            SparseList <int> counts   = CountAllGroups(mesh);
            List <int>       GroupIDs = new List <int>();

            foreach (var idxval in counts.Values())
            {
                if (idxval.Key != ignoreGID && idxval.Value > 0)
                {
                    GroupIDs.Add(idxval.Key);
                }
            }
            GroupIDs.Sort();        // might as well sort ascending...
            SparseList <int> groupMap = new SparseList <int>(mesh.MaxGroupID, GroupIDs.Count, -1);

            // allocate sets
            int[][] sets     = new int[GroupIDs.Count][];
            int[]   counters = new int[GroupIDs.Count];
            for (int i = 0; i < GroupIDs.Count; ++i)
            {
                int gid = GroupIDs[i];
                sets[i]       = new int[counts[gid]];
                counters[i]   = 0;
                groupMap[gid] = i;
            }

            // accumulate triangles
            int NT = mesh.MaxTriangleID;

            for (int tid = 0; tid < NT; ++tid)
            {
                if (mesh.IsTriangle(tid))
                {
                    int gid = mesh.GetTriangleGroup(tid);
                    int i   = groupMap[gid];
                    if (i >= 0)
                    {
                        int k = counters[i]++;
                        sets[i][k] = tid;
                    }
                }
            }

            return(sets);
        }
 /// <summary>
 /// Sums the elements of this list.
 /// </summary>
 /// <param name="source"></param>
 /// <returns></returns>
 public static int ListSum(this IList <int> source)
 {
     if (source is SparseList <int> )
     {
         SparseList <int> sl = (SparseList <int>)source;
         int sum             = sl.CommonValue * (sl.Count - sl.SparseValues.Count);
         foreach (var sel in sl.SparseValues)
         {
             sum += sel.Value;
         }
         return(sum);
     }
     return(source.Sum());
 }
Example #13
0
        // returned pairs are [group_id, tri_count]
        public static SparseList <int> CountAllGroups(NGonsCore.geometry3Sharp.mesh.DMesh3 mesh)
        {
            SparseList <int> GroupCounts = new SparseList <int>(mesh.MaxGroupID, 0, 0);

            if (mesh.HasTriangleGroups)
            {
                int NT = mesh.MaxTriangleID;
                for (int tid = 0; tid < NT; ++tid)
                {
                    if (mesh.IsTriangle(tid))
                    {
                        int gid = mesh.GetTriangleGroup(tid);
                        GroupCounts[gid] = GroupCounts[gid] + 1;
                    }
                }
            }
            return(GroupCounts);
        }
Example #14
0
 public SparseListCleaningEnumerator(SparseList <T> list)
 {
     this.list    = list;
     this.version = list.version;
     //while (Current == null && MoveNext()) ;
 }
        /// <summary>
        /// Samples from this sparse Gaussian list
        /// </summary>
        /// <returns></returns>
        /// <remarks>This method is inefficient in that the result will be dense even though the return type is sparse.</remarks>
        public IList <double> Sample()
        {
            SparseList <double> sv = SparseList <double> .FromSize(Count);

            return(Sample(sv));
        }
Example #16
0
 public void Dispose()
 {
     list = null;
 }
Example #17
0
        public void SoftmaxTest()
        {
            // Check sparse versus dense.
            // Check situation where some entries are positive or negative infinity
            // Check effects of different common values
            double trueCV = -1;

            double[][] array = new double[][]
            {
                new double[] { trueCV, trueCV, 5, 4, trueCV, -3 },
                new double[] { trueCV, trueCV, double.NegativeInfinity, 5, trueCV, 7 },
                new double[] { trueCV, trueCV, double.PositiveInfinity, 5, trueCV, double.PositiveInfinity },
                new double[] { trueCV, trueCV, double.NegativeInfinity, 5, trueCV, double.PositiveInfinity },
                new double[]
                { double.NegativeInfinity, double.NegativeInfinity, double.NegativeInfinity, double.NegativeInfinity, double.NegativeInfinity, double.NegativeInfinity }
            };

            double[] forcedCVArr = new double[]
            {
                trueCV, 0, double.PositiveInfinity, double.NegativeInfinity
            };

            double[][] expected = new double[][]
            {
                new double[] { 0.001802, 0.001802, 0.7269, 0.2674, 0.001802, 0.0002439 },
                new double[] { 0.0002952, 0.0002952, 0, 0.1191, 0.0002952, 0.88 },
                new double[] { 0, 0, 0.5, 0, 0, 0.5 },
                new double[] { 0, 0, 0, 0, 0, 1 },
                new double[] { 0.1667, 0.1667, 0.1667, 0.1667, 0.1667, 0.1667 }
            };

            for (int pass = 0; pass < 2; pass++)
            {
                for (int i = 0; i < array.Length; i++)
                {
                    if (pass == 0)
                    {
                        Vector sma = MMath.Softmax(new List <double>(array[i]));
                        for (int e = 0; e < sma.Count; e++)
                        {
                            Assert.Equal(expected[i][e], sma[e], 1e-4);
                        }
                        Console.WriteLine(sma);
                    }
                    else
                    {
                        for (int k = 0; k < forcedCVArr.Length; k++)
                        {
                            var lst = SparseList <double> .Constant(array[i].Length, forcedCVArr[k]);

                            for (int j = 0; j < array[i].Length; j++)
                            {
                                if (array[i][j] != forcedCVArr[k])
                                {
                                    lst.SparseValues.Add(new ValueAtIndex <double>(j, array[i][j]));
                                }
                            }
                            Vector sma = MMath.Softmax(lst);
                            for (int e = 0; e < sma.Count; e++)
                            {
                                Assert.Equal(expected[i][e], sma[e], 1e-4);
                            }
                            Console.WriteLine(sma);
                        }
                    }
                }
            }
        }
Example #18
0
        /// <summary>
        /// Samples from this sparse distribution list.
        /// </summary>
        /// <returns>A sample from this distribution.</returns>
        /// <remarks>This method is inefficient in that the result will be dense even though the return type is sparse.</remarks>
        public ISparseList <TDomain> Sample()
        {
            ISparseList <TDomain> sv = SparseList <TDomain> .FromSize(Count);

            return(this.Sample(sv));
        }
Example #19
0
        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);
        }
Example #20
0
        public void SparseGaussianListFactor()
        {
            SparseGaussianList.DefaultTolerance = 1e-10;
            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;

            // True distribution for the means
            var sparseMeanDist = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(1, 2), tolerance);

            sparseMeanDist[3] = Gaussian.FromMeanAndPrecision(4, 5);
            sparseMeanDist[6] = Gaussian.FromMeanAndPrecision(7, 8);
            var meanDist        = sparseMeanDist.ToArray();
            var sparseMeanPoint = SparseList <double> .Constant(listSize, 0.1);

            sparseMeanPoint[3] = 0.7;
            sparseMeanPoint[6] = 0.8;
            var meanPoint = sparseMeanPoint.ToArray();

            // True distribution for the precisions
            var sparsePrecDist = SparseGammaList.Constant(listSize, Gamma.FromShapeAndRate(1.1, 1.2), tolerance);

            sparsePrecDist[3] = Gamma.FromShapeAndRate(2.3, 2.4);
            sparsePrecDist[6] = Gamma.FromShapeAndRate(3.4, 4.5);
            var precDist        = sparsePrecDist.ToArray();
            var sparsePrecPoint = SparseList <double> .Constant(listSize, 0.1);

            sparsePrecPoint[3] = 5.6;
            sparsePrecPoint[6] = 0.5;
            var precPoint = sparsePrecPoint.ToArray();

            var sparseSampleDist = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 1.5), tolerance);

            sparseSampleDist[3] = Gaussian.FromMeanAndPrecision(-0.5, 2.0);
            sparseSampleDist[9] = Gaussian.FromMeanAndPrecision(1.6, 0.4);
            var sampleDist        = sparseSampleDist.ToArray();
            var sparseSamplePoint = SparseList <double> .Constant(listSize, 0.5);

            sparseSamplePoint[3] = 0.1;
            sparseSamplePoint[9] = 2.3;
            var samplePoint = sparseSamplePoint.ToArray();

            var toSparseSampleDist = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(-0.2, 0.3), tolerance);

            toSparseSampleDist[3] = Gaussian.FromMeanAndPrecision(2.1, 3.2);
            toSparseSampleDist[4] = Gaussian.FromMeanAndPrecision(1.3, 0.7);
            var toSampleDist = toSparseSampleDist.ToArray();

            var toSparsePrecDist = SparseGammaList.Constant(listSize, Gamma.FromShapeAndRate(2.3, 3.4), tolerance);

            toSparsePrecDist[3] = Gamma.FromShapeAndRate(3.4, 4.5);
            toSparsePrecDist[4] = Gamma.FromShapeAndRate(5.6, 6.7);
            var toPrecDist = toSparsePrecDist.ToArray();

            // ---------------------------
            // Check average log factor
            // ---------------------------
            calcErrMsg = "Average log factor" + calcSuffix;
            // Dist, dist, dist
            var sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSampleDist, sparseMeanDist, sparsePrecDist);
            var avgLog       = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(sampleDist[i], meanDist[i], precDist[i])).Sum();

            TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg);
            // Dist, dist, point
            sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSampleDist, sparseMeanDist, sparsePrecPoint);
            avgLog       = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(sampleDist[i], meanDist[i], precPoint[i])).Sum();
            TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg);
            // Dist, point, dist
            sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSampleDist, sparseMeanPoint, sparsePrecDist);
            avgLog       = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(sampleDist[i], meanPoint[i], precDist[i])).Sum();
            TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg);
            // Dist, point, point
            sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSampleDist, sparseMeanPoint, sparsePrecPoint);
            avgLog       = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(sampleDist[i], meanPoint[i], precPoint[i])).Sum();
            TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg);
            // Point, dist, dist
            sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSamplePoint, sparseMeanDist, sparsePrecDist);
            avgLog       = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(samplePoint[i], meanDist[i], precDist[i])).Sum();
            TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg);
            // Point, dist, point
            sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSamplePoint, sparseMeanDist, sparsePrecPoint);
            avgLog       = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(samplePoint[i], meanDist[i], precPoint[i])).Sum();
            TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg);
            // Point, point, dist
            sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSamplePoint, sparseMeanPoint, sparsePrecDist);
            avgLog       = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(samplePoint[i], meanPoint[i], precDist[i])).Sum();
            TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg);
            // Point, point, point
            sparseAvgLog = SparseGaussianListOp.AverageLogFactor(sparseSamplePoint, sparseMeanPoint, sparsePrecPoint);
            avgLog       = Util.ArrayInit(listSize, i => GaussianOp.AverageLogFactor(samplePoint[i], meanPoint[i], precPoint[i])).Sum();
            TAssert.True(System.Math.Abs(avgLog - sparseAvgLog) < tolerance, calcErrMsg);

            // ---------------------------
            // Check log average factor
            // ---------------------------
            calcErrMsg = "Log average factor" + calcSuffix;
            var sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSampleDist, sparseMeanDist, sparsePrecDist, toSparsePrecDist);
            var logAvg       = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(sampleDist[i], meanDist[i], precDist[i], toPrecDist[i])).Sum();

            TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg);
            // Dist, dist, point
            sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSampleDist, sparseMeanDist, sparsePrecPoint);
            logAvg       = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(sampleDist[i], meanDist[i], precPoint[i])).Sum();
            TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg);
            // Dist, point, dist
            sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSampleDist, sparseMeanPoint, sparsePrecDist, toSparsePrecDist);
            logAvg       = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(sampleDist[i], meanPoint[i], precDist[i], toPrecDist[i])).Sum();
            TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg);
            // Dist, point, point
            sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSampleDist, sparseMeanPoint, sparsePrecPoint);
            logAvg       = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(sampleDist[i], meanPoint[i], precPoint[i])).Sum();
            TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg);
            // Point, dist, dist
            sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSamplePoint, sparseMeanDist, sparsePrecDist, toSparsePrecDist);
            logAvg       = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(samplePoint[i], meanDist[i], precDist[i], toPrecDist[i])).Sum();
            TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg);
            // Point, dist, point
            sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSamplePoint, sparseMeanDist, sparsePrecPoint);
            logAvg       = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(samplePoint[i], meanDist[i], precPoint[i])).Sum();
            TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg);
            // Point, point, dist
            sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSamplePoint, sparseMeanPoint, sparsePrecDist);
            logAvg       = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(samplePoint[i], meanPoint[i], precDist[i])).Sum();
            TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg);
            // Point, point, point
            sparseLogAvg = SparseGaussianListOp.LogAverageFactor(sparseSamplePoint, sparseMeanPoint, sparsePrecPoint);
            logAvg       = Util.ArrayInit(listSize, i => GaussianOp.LogAverageFactor(samplePoint[i], meanPoint[i], precPoint[i])).Sum();
            TAssert.True(System.Math.Abs(logAvg - sparseLogAvg) < tolerance, calcErrMsg);

            // ---------------------------
            // Check log evidence ratio
            // ---------------------------
            calcErrMsg = "Log evidence ratio" + calcSuffix;
            var sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSampleDist, sparseMeanDist, sparsePrecDist, toSparseSampleDist, toSparsePrecDist);
            var evidRat       = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(sampleDist[i], meanDist[i], precDist[i], toSampleDist[i], toPrecDist[i])).Sum();

            TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg);
            // Dist, dist, point
            sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSampleDist, sparseMeanDist, sparsePrecPoint);
            evidRat       = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(sampleDist[i], meanDist[i], precPoint[i])).Sum();
            TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg);
            // Dist, point, dist
            sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSampleDist, sparseMeanPoint, sparsePrecDist, toSparseSampleDist, toSparsePrecDist);
            evidRat       = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(sampleDist[i], meanPoint[i], precDist[i], toSampleDist[i], toPrecDist[i])).Sum();
            TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg);
            // Dist, point, point
            sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSampleDist, sparseMeanPoint, sparsePrecPoint);
            evidRat       = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(sampleDist[i], meanPoint[i], precPoint[i])).Sum();
            TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg);
            // Point, dist, dist
            sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSamplePoint, sparseMeanDist, sparsePrecDist, toSparsePrecDist);
            evidRat       = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(samplePoint[i], meanDist[i], precDist[i], toPrecDist[i])).Sum();
            TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg);
            // Point, dist, point
            sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSamplePoint, sparseMeanDist, sparsePrecPoint);
            evidRat       = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(samplePoint[i], meanDist[i], precPoint[i])).Sum();
            TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg);
            // Point, point, dist
            sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSamplePoint, sparseMeanPoint, sparsePrecDist);
            evidRat       = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(samplePoint[i], meanPoint[i], precDist[i])).Sum();
            TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg);
            // Point, point, point
            sparseEvidRat = SparseGaussianListOp.LogEvidenceRatio(sparseSamplePoint, sparseMeanPoint, sparsePrecPoint);
            evidRat       = Util.ArrayInit(listSize, i => GaussianOp.LogEvidenceRatio(samplePoint[i], meanPoint[i], precPoint[i])).Sum();
            TAssert.True(System.Math.Abs(evidRat - sparseEvidRat) < tolerance, calcErrMsg);

            // ---------------------------
            // Check SampleAverageConditional
            // ---------------------------
            calcErrMsg     = "SampleAverageConditional" + calcSuffix;
            sparsityErrMsg = "SampleAverageConditional" + sparsitySuffix;
            // Use different common value to ensure this gets properly set
            var sparseSampleAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance);

            sparseSampleAvgConditional = SparseGaussianListOp.SampleAverageConditional(sparseSampleDist, sparseMeanDist, sparsePrecDist, toSparsePrecDist, sparseSampleAvgConditional);
            var sampleAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.SampleAverageConditional(sampleDist[i], meanDist[i], precDist[i], toPrecDist[i]));

            TAssert.True(3 == sparseSampleAvgConditional.SparseCount, sparsityErrMsg);
            TAssert.True(sparseSampleAvgConditional.MaxDiff(sampleAvgConditional) < tolerance, calcErrMsg);

            sparseSampleAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance);
            sparseSampleAvgConditional = SparseGaussianListOp.SampleAverageConditional(sparseSampleDist, sparseMeanPoint, sparsePrecDist, toSparsePrecDist, sparseSampleAvgConditional);
            sampleAvgConditional       = Util.ArrayInit(listSize, i => GaussianOp.SampleAverageConditional(sampleDist[i], meanPoint[i], precDist[i], toPrecDist[i]));
            TAssert.True(3 == sparseSampleAvgConditional.SparseCount, sparsityErrMsg);
            TAssert.True(sparseSampleAvgConditional.MaxDiff(sampleAvgConditional) < tolerance, calcErrMsg);

            sparseSampleAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance);
            sparseSampleAvgConditional = SparseGaussianListOp.SampleAverageConditional(sparseMeanDist, sparsePrecPoint, sparseSampleAvgConditional);
            sampleAvgConditional       = Util.ArrayInit(listSize, i => GaussianOp.SampleAverageConditional(meanDist[i], precPoint[i]));
            TAssert.True(2 == sparseSampleAvgConditional.SparseCount, sparsityErrMsg);
            TAssert.True(sparseSampleAvgConditional.MaxDiff(sampleAvgConditional) < tolerance, calcErrMsg);

            sparseSampleAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance);
            sparseSampleAvgConditional = SparseGaussianListOp.SampleAverageConditional(sparseMeanPoint, sparsePrecPoint, sparseSampleAvgConditional);
            sampleAvgConditional       = Util.ArrayInit(listSize, i => GaussianOp.SampleAverageConditional(meanPoint[i], precPoint[i]));
            TAssert.True(2 == sparseSampleAvgConditional.SparseCount, sparsityErrMsg);
            TAssert.True(sparseSampleAvgConditional.MaxDiff(sampleAvgConditional) < tolerance, calcErrMsg);

            // ---------------------------
            // Check MeanAverageConditional
            // ---------------------------
            calcErrMsg     = "MeanAverageConditional" + calcSuffix;
            sparsityErrMsg = "MeanAverageConditional" + sparsitySuffix;
            // Use different common value to ensure this gets properly set
            var sparseMeanAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance);

            sparseMeanAvgConditional = SparseGaussianListOp.MeanAverageConditional(sparseSampleDist, sparseMeanDist, sparsePrecDist, toSparsePrecDist, sparseMeanAvgConditional);
            var meanAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.MeanAverageConditional(sampleDist[i], meanDist[i], precDist[i], toPrecDist[i]));

            TAssert.True(3 == sparseMeanAvgConditional.SparseCount, sparsityErrMsg);
            TAssert.True(sparseMeanAvgConditional.MaxDiff(meanAvgConditional) < tolerance, calcErrMsg);

            sparseMeanAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance);
            sparseMeanAvgConditional = SparseGaussianListOp.MeanAverageConditional(sparseSamplePoint, sparseMeanDist, sparsePrecDist, toSparsePrecDist, sparseMeanAvgConditional);
            meanAvgConditional       = Util.ArrayInit(listSize, i => GaussianOp.MeanAverageConditional(samplePoint[i], meanDist[i], precDist[i], toPrecDist[i]));
            TAssert.True(3 == sparseMeanAvgConditional.SparseCount, sparsityErrMsg);
            TAssert.True(sparseMeanAvgConditional.MaxDiff(meanAvgConditional) < tolerance, calcErrMsg);

            sparseMeanAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance);
            sparseMeanAvgConditional = SparseGaussianListOp.MeanAverageConditional(sparseSampleDist, sparsePrecPoint, sparseMeanAvgConditional);
            meanAvgConditional       = Util.ArrayInit(listSize, i => GaussianOp.MeanAverageConditional(sampleDist[i], precPoint[i]));
            TAssert.True(3 == sparseMeanAvgConditional.SparseCount, sparsityErrMsg);
            TAssert.True(sparseMeanAvgConditional.MaxDiff(meanAvgConditional) < tolerance, calcErrMsg);

            sparseMeanAvgConditional = SparseGaussianList.Constant(listSize, Gaussian.FromMeanAndPrecision(0.5, 0.6), tolerance);
            sparseMeanAvgConditional = SparseGaussianListOp.MeanAverageConditional(sparseSamplePoint, sparsePrecPoint, sparseMeanAvgConditional);
            meanAvgConditional       = Util.ArrayInit(listSize, i => GaussianOp.MeanAverageConditional(samplePoint[i], precPoint[i]));
            TAssert.True(3 == sparseMeanAvgConditional.SparseCount, sparsityErrMsg);
            TAssert.True(sparseMeanAvgConditional.MaxDiff(meanAvgConditional) < tolerance, calcErrMsg);

            // ---------------------------
            // Check PrecisionAverageConditional
            // ---------------------------
            calcErrMsg     = "PrecisionAverageConditional" + calcSuffix;
            sparsityErrMsg = "PrecisionAverageConditional" + sparsitySuffix;
            // Use different common value to ensure this gets properly set
            var sparsePrecAvgConditional = SparseGammaList.Constant(listSize, Gamma.FromShapeAndRate(2.1, 3.2), tolerance);

            sparsePrecAvgConditional = SparseGaussianListOp.PrecisionAverageConditional(sparseSampleDist, sparseMeanDist, sparsePrecDist, sparsePrecAvgConditional);
            var precAvgConditional = Util.ArrayInit(listSize, i => GaussianOp.PrecisionAverageConditional(sampleDist[i], meanDist[i], precDist[i]));

            TAssert.True(3 == sparsePrecAvgConditional.SparseCount, sparsityErrMsg);
            TAssert.True(sparsePrecAvgConditional.MaxDiff(precAvgConditional) < tolerance, calcErrMsg);

            sparsePrecAvgConditional = SparseGammaList.Constant(listSize, Gamma.FromShapeAndRate(2.1, 3.2), tolerance);
            sparsePrecAvgConditional = SparseGaussianListOp.PrecisionAverageConditional(sparseSamplePoint, sparseMeanDist, sparsePrecDist, sparsePrecAvgConditional);
            precAvgConditional       = Util.ArrayInit(listSize, i => GaussianOp.PrecisionAverageConditional(Gaussian.PointMass(samplePoint[i]), meanDist[i], precDist[i]));
            TAssert.True(3 == sparsePrecAvgConditional.SparseCount, sparsityErrMsg);
            TAssert.True(sparsePrecAvgConditional.MaxDiff(precAvgConditional) < tolerance, calcErrMsg);

            sparsePrecAvgConditional = SparseGammaList.Constant(listSize, Gamma.FromShapeAndRate(2.1, 3.2), tolerance);
            sparsePrecAvgConditional = SparseGaussianListOp.PrecisionAverageConditional(sparseSampleDist, sparseMeanPoint, sparsePrecDist, sparsePrecAvgConditional);
            precAvgConditional       = Util.ArrayInit(listSize, i => GaussianOp.PrecisionAverageConditional(sampleDist[i], Gaussian.PointMass(meanPoint[i]), precDist[i]));
            TAssert.True(3 == sparsePrecAvgConditional.SparseCount, sparsityErrMsg);
            TAssert.True(sparsePrecAvgConditional.MaxDiff(precAvgConditional) < tolerance, calcErrMsg);

            sparsePrecAvgConditional = SparseGammaList.Constant(listSize, Gamma.FromShapeAndRate(2.1, 3.2), tolerance);
            sparsePrecAvgConditional = SparseGaussianListOp.PrecisionAverageConditional(sparseSamplePoint, sparseMeanPoint, sparsePrecAvgConditional);
            precAvgConditional       = Util.ArrayInit(listSize, i => GaussianOp.PrecisionAverageConditional(samplePoint[i], meanPoint[i]));
            TAssert.True(3 == sparsePrecAvgConditional.SparseCount, sparsityErrMsg);
            TAssert.True(sparsePrecAvgConditional.MaxDiff(precAvgConditional) < tolerance, calcErrMsg);
        }