コード例 #1
0
ファイル: SparseDistributionList.cs プロジェクト: 0xCM/arrows
        /// <summary>
        /// Returns a new instance of the <see cref="SparseDistributionList{TDist,TDomain,TThis}"/> class from a sparse list of distributions.
        /// </summary>
        /// <param name="distributions">The sparse list of distributions.</param>
        /// <returns>The new instance.</returns>
        public static TThis FromSparseList(ISparseList <TDist> distributions)
        {
            var result = new TThis();

            result.SetTo(distributions);
            return(result);
        }
コード例 #2
0
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="SparseGaussianListOp"]/message_doc[@name="LogEvidenceRatio(SparseGaussianList, ISparseList{double}, SparseGammaList, SparseGaussianList, SparseGammaList)"]/*'/>
        public static double LogEvidenceRatio(
            [SkipIfUniform] SparseGaussianList sample, ISparseList <double> mean, [SkipIfUniform] SparseGammaList precision, [Fresh] SparseGaussianList to_sample, SparseGammaList to_precision)
        {
            Func <Gaussian, double, Gamma, Gaussian, Gamma, double> f = GaussianOp.LogEvidenceRatio;

            return(f.Map(sample, mean, precision, to_sample, to_precision).EnumerableSum(x => x));
        }
コード例 #3
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);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        /// <summary>VMP message to <c>sample</c>.</summary>
        /// <param name="probTrue">Constant value for <c>probTrue</c>.</param>
        /// <param name="result">Modified to contain the outgoing message.</param>
        /// <returns>
        ///   <paramref name="result" />
        /// </returns>
        /// <remarks>
        ///   <para>The outgoing message is the factor viewed as a function of <c>sample</c> conditioned on the given values.</para>
        /// </remarks>
        public static BernoulliIntegerSubset SampleAverageLogarithm(ISparseList <double> probTrue, BernoulliIntegerSubset result)
        {
            Func <double, Bernoulli> f = BernoulliFromBetaOp.SampleAverageLogarithm;

            result.SetTo(f.Map(probTrue));
            return(result);
        }
コード例 #6
0
        public static ISparseList <double> Sample(ISparseList <double> means, ISparseList <double> precs)
        {
            SparseList <double> sample = SparseList <double> .FromSize(means.Count);

            sample.SetToFunction(means, precs, (m, p) => Gaussian.Sample(m, p));
            return(sample);
        }
コード例 #7
0
        /// <summary>Gibbs message to <c>sample</c>.</summary>
        /// <param name="probTrue">Constant value for <c>probTrue</c>.</param>
        /// <param name="result">Modified to contain the outgoing message.</param>
        /// <returns>
        ///   <paramref name="result" />
        /// </returns>
        /// <remarks>
        ///   <para>The outgoing message is the factor viewed as a function of <c>sample</c> conditioned on the given values.</para>
        /// </remarks>
        public static BernoulliIntegerSubset SampleConditional(ISparseList <double> probTrue, BernoulliIntegerSubset result)
        {
            Func <double, Bernoulli> f = BernoulliFromBetaOp.SampleConditional;

            result.SetTo(f.Map(probTrue));
            return(result);
        }
コード例 #8
0
        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);
        }
コード例 #9
0
ファイル: BernoulliIntegerSubset.cs プロジェクト: 0xCM/arrows
        /// <summary>
        /// Returns a new instance of the <see cref="BernoulliIntegerSubset"/> class from a sparse list of Bernoulli distributions.
        /// </summary>
        /// <param name="bernoullis">The sparse list of Bernoulli distributions.</param>
        /// <param name="tolerance">The tolerance.</param>
        /// <returns>The new <see cref="BernoulliIntegerSubset"/> instance.</returns>
        public static BernoulliIntegerSubset FromSparseList(ISparseList <Bernoulli> bernoullis, double tolerance)
        {
            var result = new BernoulliIntegerSubset();

            result.SparseBernoulliList = SparseBernoulliList.FromSparseList(bernoullis, tolerance);
            return(result);
        }
コード例 #10
0
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="SparseGaussianListOp"]/message_doc[@name="LogAverageFactor(SparseGaussianList, ISparseList{double}, SparseGammaList, SparseGammaList)"]/*'/>
        public static double LogAverageFactor(
            [SkipIfUniform] SparseGaussianList sample, ISparseList <double> mean, [SkipIfUniform] SparseGammaList precision, SparseGammaList to_precision)
        {
            Func <Gaussian, double, Gamma, Gamma, double> f = GaussianOp.LogAverageFactor;

            return(f.Map(sample, mean, precision, to_precision).EnumerableSum(x => x));
        }
コード例 #11
0
        /// <summary>
        /// CReturns a new instance of the <see cref="SparseGammaList"/> class
        /// from sparse lists of shapes and scales.
        /// </summary>
        /// <param name="shape">The shape values.</param>
        /// <param name="scale">The scale values.</param>
        /// <param name="tolerance">The tolerance for the approximation.</param>
        /// <returns>The new <see cref="SparseGammaList"/> instance.</returns>
        public static SparseGammaList FromShapeAndScale(
            ISparseList <double> shape, ISparseList <double> scale, double tolerance)
        {
            var result = FromSize(shape.Count, tolerance);

            result.SetToFunction(shape, scale, (s, sc) => Gamma.FromShapeAndScale(s, sc));
            return(result);
        }
コード例 #12
0
        /// <summary>
        /// Returns a new instance of the <see cref="SparseGaussianList"/> class from sparse mean and precision lists.
        /// </summary>
        /// <param name="mean">The desired mean.</param>
        /// <param name="precision">The desired precision.</param>
        /// <param name="tolerance">The tolerance for the approximation.</param>
        /// <returns>The new <see cref="SparseGaussianList"/> instance.</returns>
        public static SparseGaussianList FromMeanAndPrecision(
            ISparseList <double> mean, ISparseList <double> precision, double tolerance)
        {
            var result = FromSize(mean.Count, tolerance);

            result.SetToFunction(mean, precision, (m, p) => Gaussian.FromMeanAndPrecision(m, p));
            return(result);
        }
コード例 #13
0
        /// <summary>
        /// Returns a new instance of instance of the <see cref="SparseBetaList"/> class
        /// from sparse lists of true and false counts.
        /// </summary>
        /// <param name="trueCounts">The true counts.</param>
        /// <param name="falseCounts">The false counts.</param>
        /// <param name="tolerance">The tolerance for the approximation.</param>
        /// <returns>A new instance of the <see cref="SparseBetaList"/> class.</returns>
        public static SparseBetaList FromCounts(
            ISparseList <double> trueCounts, ISparseList <double> falseCounts, double tolerance)
        {
            var result = FromSize(trueCounts.Count, tolerance);

            result.SetToFunction(trueCounts, falseCounts, (tc, fc) => new Beta(tc, fc));
            return(result);
        }
コード例 #14
0
ファイル: BernoulliIntegerSubset.cs プロジェクト: 0xCM/arrows
        /// <summary>
        /// Returns a new instance of the <see cref="BernoulliIntegerSubset"/> class of a given size
        /// from a sparse list of probability true.
        /// </summary>
        /// <param name="probTrue">The sparse list of probability of true.</param>
        /// <param name="tolerance">The tolerance for the approximation.</param>
        /// <returns>The new <see cref="BernoulliIntegerSubset"/> instance.</returns>
        public static BernoulliIntegerSubset FromProbTrue(
            ISparseList <double> probTrue, double tolerance)
        {
            BernoulliIntegerSubset result = new BernoulliIntegerSubset();

            result.SparseBernoulliList = SparseBernoulliList.FromProbTrue(probTrue, tolerance);
            return(result);
        }
コード例 #15
0
        /// <summary>
        /// Returns a new instance of the <see cref="SparseBernoulliList"/> class
        /// of a given size from a sparse list of probability true.
        /// </summary>
        /// <param name="probTrue">The sparse list of probability of true.</param>
        /// <param name="tolerance">The tolerance for the approximation.</param>
        /// <returns>The new <see cref="SparseBernoulliList"/> instance.</returns>
        public static SparseBernoulliList FromProbTrue(
            ISparseList <double> probTrue, double tolerance)
        {
            var result = FromSize(probTrue.Count, tolerance);

            result.SetToFunction(probTrue, pt => new Bernoulli(pt));
            return(result);
        }
コード例 #16
0
        /// <summary>
        /// Returns a new instance of the <see cref="SparseBernoulliList"/> class
        /// of a given size from a sparse list of log odds.
        /// </summary>
        /// <param name="logOdds">The sparse list of log odds.</param>
        /// <param name="tolerance">The tolerance for the approximation.</param>
        /// <returns>The new <see cref="SparseBernoulliList"/> instance.</returns>
        public static SparseBernoulliList FromLogOdds(
            ISparseList <double> logOdds, double tolerance)
        {
            var result = FromSize(logOdds.Count, tolerance);

            result.SetToFunction(logOdds, lodds => Bernoulli.FromLogOdds(lodds));
            return(result);
        }
コード例 #17
0
ファイル: BernoulliIntegerSubset.cs プロジェクト: 0xCM/arrows
        /// <summary>
        /// Returns a new instance of the <see cref="BernoulliIntegerSubset"/> class of a given size
        /// from a sparse list of log odds.
        /// </summary>
        /// <param name="logOdds">The sparse list of log odds.</param>
        /// <param name="tolerance">The tolerance for the approximation.</param>
        /// <returns>The new <see cref="BernoulliIntegerSubset"/> instance.</returns>
        public static BernoulliIntegerSubset FromLogOdds(
            ISparseList <double> logOdds, double tolerance)
        {
            BernoulliIntegerSubset result = new BernoulliIntegerSubset();

            result.SparseBernoulliList = SparseBernoulliList.FromLogOdds(logOdds, tolerance);
            return(result);
        }
コード例 #18
0
ファイル: SparseDistributionList.cs プロジェクト: 0xCM/arrows
        /// <summary>
        /// Returns a new instance of the <see cref="SparseDistributionList{TDist,TDomain,TThis}"/> class from a sparse list of distributions.
        /// </summary>
        /// <param name="distributions">The sparse list of distributions.</param>
        /// <param name="tolerance">The tolerance.</param>
        /// <returns>The new instance.</returns>
        public static TThis FromSparseList(ISparseList <TDist> distributions, double tolerance)
        {
            var result = new TThis();

            result.Count     = distributions.Count;
            result.Tolerance = tolerance;
            result.SetTo(distributions);
            return(result);
        }
コード例 #19
0
ファイル: SparseDistributionList.cs プロジェクト: 0xCM/arrows
        /// <summary>
        /// Samples from this sparse distribution list.
        /// </summary>
        /// <param name="result">Where to put the result.</param>
        /// <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> result)
        {
            Argument.CheckIfValid(result.Count == this.Dimension, string.Format("Result list does not match dimension of this distribution. Expected {0}, got {1}.", this.Dimension, result.Count));
            IEnumerator <TDist> e = GetEnumerator();
            int i = 0;

            while (e.MoveNext())
            {
                result[i++] = e.Current.Sample();
            }

            return(result);
        }
コード例 #20
0
ファイル: SparseDistributionList.cs プロジェクト: 0xCM/arrows
        /// <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);
        }
コード例 #21
0
ファイル: BernoulliIntegerSubset.cs プロジェクト: 0xCM/arrows
        public static IList <int> Sample(ISparseList <double> probTrue)
        {
            var result = new bool[probTrue.Count];
            var list   = new List <int>();
            int i      = 0;

            foreach (double d in probTrue)
            {
                if (Bernoulli.Sample(d))
                {
                    list.Add(i);
                }

                i++;
            }

            return(list);
        }
コード例 #22
0
 /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="SparseBernoulliFromBetaOp"]/message_doc[@name="ProbTrueAverageConditional(ISparseList{bool}, SparseBetaList)"]/*'/>
 public static SparseBetaList ProbTrueAverageConditional(ISparseList <bool> sample, SparseBetaList result)
 {
     result.SetToFunction(sample, s => BernoulliFromBetaOp.ProbTrueAverageConditional(s));
     return(result);
 }
コード例 #23
0
 /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="SparseBernoulliFromBetaOp"]/message_doc[@name="SampleAverageConditional(ISparseList{double}, SparseBernoulliList)"]/*'/>
 public static SparseBernoulliList SampleAverageConditional(ISparseList <double> probTrue, SparseBernoulliList result)
 {
     result.SetToFunction(probTrue, pt => BernoulliFromBetaOp.SampleAverageConditional(pt));
     return(result);
 }
コード例 #24
0
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="SparseBernoulliFromBetaOp"]/message_doc[@name="LogEvidenceRatio(ISparseList{bool}, ISparseList{double})"]/*'/>
        public static double LogEvidenceRatio(ISparseList <bool> sample, ISparseList <double> probTrue)
        {
            Func <bool, double, double> f = BernoulliFromBetaOp.LogEvidenceRatio;

            return(f.Map(sample, probTrue).EnumerableSum(x => x));
        }
コード例 #25
0
 public static double LogEvidenceRatio(SparseBernoulliList sample, ISparseList <double> probTrue)
 {
     return(0.0);
 }
コード例 #26
0
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="SparseBernoulliFromBetaOp"]/message_doc[@name="LogAverageFactor(ISparseList{bool}, SparseBetaList)"]/*'/>
        public static double LogAverageFactor(ISparseList <bool> sample, SparseBetaList probTrue)
        {
            Func <bool, Beta, double> f = BernoulliFromBetaOp.LogAverageFactor;

            return(f.Map(sample, probTrue).EnumerableSum(x => x));
        }
コード例 #27
0
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="SparseBernoulliFromBetaOp"]/message_doc[@name="AverageLogFactor(ISparseList{bool}, ISparseList{double})"]/*'/>
        public static double AverageLogFactor(ISparseList <bool> sample, ISparseList <double> probTrue)
        {
            Func <bool, double, double> f = BernoulliFromBetaOp.AverageLogFactor;

            return(f.Map(sample, probTrue).EnumerableSum(x => x));
        }
コード例 #28
0
 /// <summary>
 /// Returns a new instance of the <see cref="SparseBernoulliList"/> class of a given size
 /// from a sparse list of log odds.
 /// </summary>
 /// <param name="logOdds">The sparse list of log odds.</param>
 /// <returns>The new <see cref="SparseBernoulliList"/> instance.</returns>
 public static SparseBernoulliList FromLogOdds(
     ISparseList <double> logOdds)
 {
     return(SparseBernoulliList.FromLogOdds(logOdds, SparseBernoulliList.DefaultTolerance));
 }
コード例 #29
0
 /// <summary>
 /// Returns a new instance of the <see cref="SparseBernoulliList"/> class of a given size
 /// from a sparse list of probability true.
 /// </summary>
 /// <param name="probTrue">The sparse list of probability of true.</param>
 /// <returns>The new <see cref="SparseBernoulliList"/> instance.</returns>
 public static SparseBernoulliList FromProbTrue(
     ISparseList <double> probTrue)
 {
     return(SparseBernoulliList.FromProbTrue(probTrue, SparseBernoulliList.DefaultTolerance));
 }
コード例 #30
0
ファイル: BernoulliIntegerSubset.cs プロジェクト: 0xCM/arrows
 /// <summary>
 /// Initializes a new instance of the <see cref="BernoulliIntegerSubset"/> class from a sparse list of <see cref="Bernoulli"/> distributions.
 /// </summary>
 /// <param name="bernoullis">The sparse list of Bernoulli distributions.</param>
 protected BernoulliIntegerSubset(ISparseList <Bernoulli> bernoullis)
 {
     SparseBernoulliList = SparseBernoulliList.FromSparseList(bernoullis);
 }