public void ValidateCumulativeDistribution(double lambda, int x, double result) { var d = new Poisson(lambda); Assert.AreEqual(d.CumulativeDistribution(x), result, 1e-12); }
public static Poisson SampleAverageConditional(double p, Poisson trialCount) { if(trialCount.Precision != 1) throw new NotImplementedException("precision != 1 not implemented"); // p(sample=k) = sum(n>=k) lambda^n/n! exp(-lambda) nchoosek(n,k) p^k (1-p)^(n-k) // = (p/(1-p))^k 1/k! exp(-lambda) sum_(n>=k) 1/(n-k)! (1-p)^n lambda^n // = (p/(1-p))^k 1/k! exp(-lambda) (1-p)^k lambda^k exp((1-p) lambda) // = p^k lambda^k 1/k! exp(-p lambda) return new Poisson(p*trialCount.Rate); }
public static Poisson TrialCountAverageConditional(Poisson sample, double p) { if (sample.Precision != 0) throw new NotImplementedException("precision != 0 not implemented"); // sum(x<=n) r^x nchoosek(n,x) p^x (1-p)^(n-x) // =propto (1-p)^n sum(x<=n) r^x nchoosek(n,x) (p/(1-p))^x // =propto (1-p)^n (1 + r p/(1-p))^n // =propto (1-p + r p)^n return new Poisson(1-p + sample.Rate*p, 0); }
/// <summary> /// Evidence message for EP /// </summary> /// <param name="sample">Incoming message from 'sample'.</param> /// <param name="to_sample">Outgoing message to 'sample'.</param> /// <returns>Logarithm of the factor's average value across the given argument distributions</returns> /// <remarks><para> /// The formula for the result is <c>log(sum_(sample) p(sample) factor(sample,mean))</c>. /// </para></remarks> public static double LogAverageFactor(Poisson sample, [Fresh] Poisson to_sample) { return to_sample.GetLogAverageOf(sample); }
public void ValidateCumulativeDistribution( [Values(1.5, 1.5, 1.5, 5.4, 5.4, 5.4, 10.8, 10.8, 10.8)] double lambda, [Values(1, 10, 20, 1, 10, 20, 1, 10, 20)] int x, [Values(0.5578254003710750000000, 0.9999994482467640000000, 1.0000000000000000000000, 0.0289061180327211000000, 0.9774863006897650000000, 0.9999997199928290000000, 0.0002407141402518290000, 0.4839692359955690000000, 0.9961800769608090000000)] double result) { var d = new Poisson(lambda); Assert.AreEqual(d.CumulativeDistribution(x), result, 1e-12); }
public void ValidateProbabilityLn(double lambda, int x, double result) { var d = new Poisson(lambda); Assert.AreEqual(d.ProbabilityLn(x), Math.Log(result), 1e-12); }
public void CanSample() { var d = new Poisson(0.3); d.Sample(); }
public void ValidateMinimum() { var d = new Poisson(0.3); Assert.AreEqual(0, d.Minimum); }
public void ValidateMaximum() { var d = new Poisson(0.3); Assert.AreEqual(int.MaxValue, d.Maximum); }
public void ValidateMedian(double lambda) { var d = new Poisson(lambda); Assert.AreEqual((int)Math.Floor(lambda + (1.0 / 3.0) - (0.02 / lambda)), d.Median); }
public void ValidateMode(double lambda) { var d = new Poisson(lambda); Assert.AreEqual((int)Math.Floor(lambda), d.Mode); }
public void ValidateSkewness(double lambda) { var d = new Poisson(lambda); Assert.AreEqual(1.0 / Math.Sqrt(lambda), d.Skewness); }
public void CanCreatePoisson(double lambda) { var d = new Poisson(lambda); Assert.AreEqual(lambda, d.Lambda); }
public void ValidateToString() { var d = new Poisson(0.3); Assert.AreEqual(String.Format("Poisson(λ = {0})", 0.3), d.ToString()); }
/// <summary> /// Evidence message for VMP /// </summary> /// <param name="sample">Incoming message from 'sample'.</param> /// <param name="mean">Incoming message from 'mean'. Must be a proper distribution. If uniform, the result will be uniform.</param> /// <returns>Average of the factor's log-value across the given argument distributions</returns> /// <remarks><para> /// The formula for the result is <c>sum_(sample,mean) p(sample,mean) log(factor(sample,mean))</c>. /// Adding up these values across all factors and variables gives the log-evidence estimate for VMP. /// </para></remarks> /// <exception cref="ImproperMessageException"><paramref name="mean"/> is not a proper distribution</exception> public static double AverageLogFactor(Poisson sample, [Proper] Gamma mean) { return sample.GetMean() * mean.GetMeanLog() - sample.GetMeanLogFactorial() - mean.GetMean(); }
public void ValidateEntropy(double lambda) { var d = new Poisson(lambda); Assert.AreEqual((0.5 * Math.Log(2 * Math.PI * Math.E * lambda)) - (1.0 / (12.0 * lambda)) - (1.0 / (24.0 * lambda * lambda)) - (19.0 / (360.0 * lambda * lambda * lambda)), d.Entropy); }
private static void test01_mono() //****************************************************************************80 // // Purpose: // // TEST01_MONO tests MONOGRID_POISSON_1D on test case 1. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 07 December 2011 // // Author: // // John Burkardt // { int it_num = 0; int k; Console.WriteLine(""); Console.WriteLine("TEST01_MONO"); Console.WriteLine(" MONOGRID_POISSON_1D solves a 1D Poisson BVP"); Console.WriteLine(" using the Gauss-Seidel method."); const double a = 0.0; const double b = 1.0; const double ua = 0.0; const double ub = 0.0; Console.WriteLine(""); Console.WriteLine(" -u''(x) = 1, for 0 < x < 1"); Console.WriteLine(" u(0) = u(1) = 0."); Console.WriteLine(" Solution is u(x) = ( -x^2 + x ) / 2"); for (k = 5; k <= 5; k++) { int n = (int)Math.Pow(2, k); double[] u = new double[n + 1]; double[] x = typeMethods.r8vec_linspace_new(n + 1, a, b); Console.WriteLine(""); Console.WriteLine(" Mesh index K = " + k + ""); Console.WriteLine(" Number of intervals N=2^K = " + n + ""); Console.WriteLine(" Number of nodes = 2^K+1 = " + (n + 1) + ""); Poisson.monogrid_poisson_1d(n, a, b, ua, ub, force1, exact1, ref it_num, ref u); Console.WriteLine(""); Console.WriteLine(" I X(I) U(I) U Exact(X(I))"); Console.WriteLine(""); int i; for (i = 0; i < n + 1; i++) { Console.WriteLine(" " + i.ToString(CultureInfo.InvariantCulture).PadLeft(4) + " " + x[i].ToString(CultureInfo.InvariantCulture).PadLeft(10) + " " + u[i].ToString(CultureInfo.InvariantCulture).PadLeft(14) + " " + exact1(x[i]).ToString(CultureInfo.InvariantCulture).PadLeft(14) + ""); } Console.WriteLine(""); double difmax = 0.0; for (i = 0; i < n + 1; i++) { difmax = Math.Max(difmax, Math.Abs(u[i] - exact1(x[i]))); } Console.WriteLine(" Maximum error = " + difmax + ""); Console.WriteLine(" Number of iterations = " + it_num + ""); } }
public PoissonPrimitive(double lambda, Random gen) { Lambda = lambda; Dist = new Poisson(Math.Floor(lambda)); Gen = gen; }
public void CanCreatePoisson([Values(1.5, 5.4, 10.8)] double lambda) { var d = new Poisson(lambda); Assert.AreEqual(lambda, d.Lambda); }
public static Poisson BAverageConditional(Poisson sum, Poisson a, Poisson b) { return(AAverageConditional(sum, b, a)); }
public static Poisson TrialCountAverageConditional(int sample, double p, Poisson trialCount) { if (trialCount.Precision != 1) throw new NotImplementedException("precision != 1 not implemented"); // p(n) =propto lambda^n/n! exp(-lambda) nchoosek(n,x) p^x (1-p)^(n-x) // =propto lambda^n (1-p)^n 1/(n-x)! // = Po(n-x; lambda*(1-p)) // E[n] = x + lambda*(1-p) return new Poisson(sample + trialCount.Rate * (1-p)) / trialCount; }
public void SetProbabilityOfOneFails(double lambda) { var d = new Poisson(0.3); Assert.Throws <ArgumentOutOfRangeException>(() => d.Lambda = lambda); }
public void SetProbabilityOfOneFails(double lambda) { var d = new Poisson(0.3); Assert.Throws<ArgumentOutOfRangeException>(() => d.Lambda = lambda); }
// Poisson case ----------------------------------------------------------------------------------------- public static Poisson SumAverageConditional(Poisson a, Poisson b) { if(a.Precision != 1 || b.Precision != 1) throw new NotImplementedException("precision != 1 not implemented"); return new Poisson(a.Rate + b.Rate); }
public static Poisson AAverageConditional(int sum, Poisson a, Poisson b) { // a has rate r // b has rate t // p(a) =propto r^a/a! t^(s-a)/(s-a)! =propto Binom(s, r/(r+t)) // E[a] = s*r/(r+t) if (a.Precision != 1 || b.Precision != 1) throw new NotImplementedException("precision != 1 not implemented"); double rate = a.Rate/(a.Rate + b.Rate); return new Poisson(sum*rate) / a; }
public static Poisson AAverageConditional(Poisson sum, Poisson a, Poisson b) { if (sum.IsPointMass) return AAverageConditional(sum.Point, a, b); // p(a) = r^a/a! sum_(s >= a) q^s t^(s-a)/(s-a)! // = r^a/a! q^a sum_(s >= a) q^(s-a) t^(s-a)/(s-a)! // = r^a/a! q^a exp(q t) if (sum.Precision != 0) throw new NotImplementedException("sum.Precision != 0 not implemented"); if (b.Precision != 1) throw new NotImplementedException("b.Precision != 1 not implemented"); return new Poisson(sum.Rate, 0); }
public static Poisson BAverageConditional(Poisson sum, Poisson a, Poisson b) { return AAverageConditional(sum, b, a); }
public void SetProbabilityOfOneFails(double lambda) { var d = new Poisson(0.3); Assert.That(() => d.Lambda = lambda, Throws.ArgumentException); }
public void ValidateSkewness([Values(1.5, 5.4, 10.8)] double lambda) { var d = new Poisson(lambda); Assert.AreEqual(1.0 / Math.Sqrt(lambda), d.Skewness); }
public void CanSampleSequence() { var d = new Poisson(0.3); var ied = d.Samples(); ied.Take(5).ToArray(); }
public void ValidateMode([Values(1.5, 5.4, 10.8)] double lambda) { var d = new Poisson(lambda); Assert.AreEqual((int)Math.Floor(lambda), d.Mode); }
/// <summary> /// Evidence message for VMP /// </summary> /// <param name="sample">Incoming message from 'sample'.</param> /// <param name="mean">Constant value for 'mean'.</param> /// <returns>Average of the factor's log-value across the given argument distributions</returns> /// <remarks><para> /// The formula for the result is <c>sum_(sample) p(sample) log(factor(sample,mean))</c>. /// Adding up these values across all factors and variables gives the log-evidence estimate for VMP. /// </para></remarks> public static double AverageLogFactor(Poisson sample, double mean) { return sample.GetMean() * mean - sample.GetMeanLogFactorial() - mean; }
public void ValidateProbabilityLn( [Values(1.5, 1.5, 1.5, 5.4, 5.4, 5.4, 10.8, 10.8, 10.8)] double lambda, [Values(1, 10, 20, 1, 10, 20, 1, 10, 20)] int x, [Values(0.334695240222645000000000000000, 0.000003545747740570180000000000, 0.000000000000000304971208961018, 0.024389537090108400000000000000, 0.026241240591792300000000000000, 0.000000825202200316548000000000, 0.000220314636840657000000000000, 0.121365183659420000000000000000, 0.003908139778574110000000000000)] double result) { var d = new Poisson(lambda); Assert.AreEqual(d.ProbabilityLn(x), Math.Log(result), 1e-12); }
public static double LogEvidenceRatio(Poisson sample) { return 0.0; }
// Generate toy data - returns indices into vocab for each doc private int[][] GenerateToyLDAData( int numTopics, int numVocab, int numDocs, int expectedDocLength, out Dirichlet[] trueTheta, out Dirichlet[] truePhi) { truePhi = new Dirichlet[numTopics]; for (int i = 0; i < numTopics; i++) { truePhi[i] = Dirichlet.Uniform(numVocab); truePhi[i].PseudoCount.SetAllElementsTo(0.0); // Draw the number of unique words in the topic int numUniqueWordsPerTopic = Poisson.Sample((double)numVocab / numTopics); if (numUniqueWordsPerTopic >= numVocab) { numUniqueWordsPerTopic = numVocab; } double expectedRepeatOfWordInTopic = ((double)numDocs) * expectedDocLength / numUniqueWordsPerTopic; int[] shuffledWordIndices = Rand.Perm(numVocab); for (int j = 0; j < numUniqueWordsPerTopic; j++) { int wordIndex = shuffledWordIndices[j]; // Draw the count for that word int cnt = Poisson.Sample(expectedRepeatOfWordInTopic); truePhi[i].PseudoCount[wordIndex] = cnt + 1.0; } } trueTheta = new Dirichlet[numDocs]; for (int i = 0; i < numDocs; i++) { trueTheta[i] = Dirichlet.Uniform(numTopics); trueTheta[i].PseudoCount.SetAllElementsTo(0.0); // Draw the number of unique topics in the doc. We expect this to be // very sparse int numUniqueTopicsPerDoc = System.Math.Min(1 + Poisson.Sample(1.0), numTopics); double expectedRepeatOfTopicInDoc = expectedDocLength / numUniqueTopicsPerDoc; int[] shuffledTopicIndices = Rand.Perm(numTopics); for (int j = 0; j < numUniqueTopicsPerDoc; j++) { int topicIndex = shuffledTopicIndices[j]; // Draw the count for that topic int cnt = Poisson.Sample(expectedRepeatOfTopicInDoc); trueTheta[i].PseudoCount[topicIndex] = cnt + 1.0; } } // Sample from the model Vector[] topicDist = new Vector[numDocs]; Vector[] wordDist = new Vector[numTopics]; for (int i = 0; i < numDocs; i++) { topicDist[i] = trueTheta[i].Sample(); } for (int i = 0; i < numTopics; i++) { wordDist[i] = truePhi[i].Sample(); } int[][] wordsInDoc = new int[numDocs][]; for (int i = 0; i < numDocs; i++) { int LengthOfDoc = Poisson.Sample((double)expectedDocLength); wordsInDoc[i] = new int[LengthOfDoc]; for (int j = 0; j < LengthOfDoc; j++) { int topic = Discrete.Sample(topicDist[i]); wordsInDoc[i][j] = Discrete.Sample(wordDist[topic]); } } return(wordsInDoc); }