/// <summary> /// VMP message to 'd' /// </summary> /// <param name="exp">Incoming message from 'exp'. Must be a proper distribution. If uniform, the result will be uniform.</param> /// <param name="d">Incoming message from 'd'. Must be a proper distribution. If uniform, the result will be uniform.</param> /// <param name="to_d">Previous outgoing message to 'd'.</param> /// <returns>The outgoing VMP message to the 'd' argument</returns> /// <remarks><para> /// The outgoing message is the factor viewed as a function of 'd' with 'exp' integrated out. /// The formula is <c>sum_exp p(exp) factor(exp,d)</c>. /// </para></remarks> /// <exception cref="ImproperMessageException"><paramref name="exp"/> is not a proper distribution</exception> /// <exception cref="ImproperMessageException"><paramref name="d"/> is not a proper distribution</exception> public static Gaussian DAverageLogarithm([Proper] Gamma exp, [Proper, Stochastic] Gaussian d, Gaussian to_d) { if (exp.IsPointMass) { return(ExpOp.DAverageLogarithm(exp.Point)); } double m, v; d.GetMeanAndVariance(out m, out v); Gaussian msg = new Gaussian(); double mu, s2; var prior = d / to_d; prior.GetMeanAndVariance(out mu, out s2); var z = Vector.Zero(2); z[0] = m; z[1] = Math.Log(v); double startingValue = GradientAndValueAtPoint(mu, s2, z, exp.Shape, exp.Rate, null); var s = new BFGS(); int evalCounter = 0; s.MaximumStep = 1e3; s.MaximumIterations = 100; s.Epsilon = 1e-5; s.convergenceCriteria = BFGS.ConvergenceCriteria.Objective; z = s.Run(z, 1.0, delegate(Vector y, ref Vector grad) { evalCounter++; return(GradientAndValueAtPoint(mu, s2, y, exp.Shape, exp.Rate, grad)); }); m = z[0]; v = Math.Exp(z[1]); to_d.SetMeanAndVariance(m, v); to_d.SetToRatio(to_d, prior); double endValue = GradientAndValueAtPoint(mu, s2, z, exp.Shape, exp.Rate, null); //Console.WriteLine("Went from {0} to {1} in {2} steps, {3} evals", startingValue, endValue, s.IterationsPerformed, evalCounter); if (startingValue < endValue) { Console.WriteLine("Warning: BFGS resulted in an increased objective function"); } return(to_d); /* ---------------- NEWTON ITERATION VERSION 1 ------------------- * double meanTimesPrec, prec; * d.GetNatural(out meanTimesPrec, out prec); * Matrix K = new Matrix(2, 2); * K[0, 0]=1/prec; // d2K by dmu^2 * K[1, 0]=K[0, 1]=-meanTimesPrec/(prec*prec); * K[1, 1]=meanTimesPrec*meanTimesPrec/Math.Pow(prec, 3)+1/(2*prec*prec); * double[,,] Kprime = new double[2, 2, 2]; * Kprime[0, 0, 0]=0; * Kprime[0, 0, 1]=Kprime[0, 1, 0]=Kprime[1, 0, 0]=-1/(prec*prec); * Kprime[0, 1, 1]=Kprime[1, 1, 0]=Kprime[1, 0, 1]=2*meanTimesPrec/Math.Pow(prec, 3); * Kprime[1, 1, 1]=-3*meanTimesPrec*meanTimesPrec/Math.Pow(prec, 4)-1/Math.Pow(prec, 3); * Vector gradS = new Vector(2); * gradS[0]=(exp.Shape-1)/prec-exp.Rate/prec*Math.Exp((meanTimesPrec+.5)/prec); * gradS[1]=-(exp.Shape-1)*meanTimesPrec/(prec*prec)+exp.Rate*(meanTimesPrec+.5)/(prec*prec)*Math.Exp((meanTimesPrec+.5)/prec); * Matrix grad2S = new Matrix(2, 2); * grad2S[0, 0]=-exp.Rate/(prec*prec)*Math.Exp((meanTimesPrec+.5)/prec); * grad2S[0, 1]=grad2S[1, 0]=-(exp.Shape-1)/(prec*prec)+exp.Rate*(1/(prec*prec)+(meanTimesPrec+.5)/Math.Pow(prec, 3))*Math.Exp((meanTimesPrec+.5)/prec); * grad2S[1, 1]=2*(exp.Shape-1)*meanTimesPrec/Math.Pow(prec, 3)-exp.Rate*(meanTimesPrec+.5)/(prec*prec)*(2/prec+(meanTimesPrec+.5)/(prec*prec))*Math.Exp((meanTimesPrec+.5)/prec); * Vector phi = new Vector(new double[] { result.MeanTimesPrecision, result.Precision }); * Vector gradKL = K*phi-gradS; * Matrix hessianKL = K - grad2S; * for (int i=0; i<2; i++) * for (int j=0; j<2; j++) * for (int k=0; k<2; k++) * hessianKL[i, j]+=Kprime[i, j, k]*phi[k]; * double step = 1; * Vector direction = GammaFromShapeAndRate.twoByTwoInverse(hessianKL)*gradKL; * Vector newPhi = phi - step * direction; * result.SetNatural(newPhi[0], newPhi[1]); * return result; * * ---------------- NEWTON ITERATION VERSION 2 ------------------- * double mean, variance; * d.GetMeanAndVariance(out mean, out variance); * Gaussian prior = d / result; * double mean1, variance1; * prior.GetMeanAndVariance(out mean1, out variance1); * Vector gradKL = new Vector(2); * gradKL[0]=-(exp.Shape-1)+exp.Rate*Math.Exp(mean+variance/2)+mean/variance1-mean1/variance1; * gradKL[1]=-1/(2*variance)+exp.Rate*Math.Exp(mean+variance/2)+1/(2*variance1); * Matrix hessianKL = new Matrix(2, 2); * hessianKL[0, 0]=exp.Rate*Math.Exp(mean+variance/2)+1/variance1; * hessianKL[0, 1]=hessianKL[1, 0]=.5*exp.Rate*Math.Exp(mean+variance/2); * hessianKL[1, 1]=1/(2*variance*variance)+exp.Rate*Math.Exp(mean+variance/2)/4; * result.GetMeanAndVariance(out mean, out variance); * if (double.IsInfinity(variance)) * variance=1000; * Vector theta = new Vector(new double[] { mean, variance }); * theta -= GammaFromShapeAndRate.twoByTwoInverse(hessianKL)*gradKL; * result.SetMeanAndVariance(theta[0], theta[1]); * return result; * ----------------------------------------------------------------- */ }
/// <summary>EP message to <c>log</c>.</summary> /// <param name="log">Incoming message from <c>log</c>. Must be a proper distribution. If uniform, the result will be uniform.</param> /// <param name="d">Incoming message from <c>d</c>. Must be a proper distribution. If uniform, the result will be uniform.</param> /// <param name="result">Modified to contain the outgoing message.</param> /// <returns> /// <paramref name="result" /> /// </returns> /// <remarks> /// <para>The outgoing message is a distribution matching the moments of <c>log</c> as the random arguments are varied. The formula is <c>proj[p(log) sum_(d) p(d) factor(log,d)]/p(log)</c>.</para> /// </remarks> /// <exception cref="ImproperMessageException"> /// <paramref name="log" /> is not a proper distribution.</exception> /// <exception cref="ImproperMessageException"> /// <paramref name="d" /> is not a proper distribution.</exception> public static Gaussian LogAverageConditional([Proper] Gaussian log, [SkipIfUniform] Gamma d, Gaussian result) { var g = Gamma.FromShapeAndRate(d.Shape + 1, d.Rate); return(ExpOp.DAverageConditional(g, log, result)); }
/// <summary>Evidence message for EP.</summary> /// <param name="log">Incoming message from <c>log</c>.</param> /// <param name="d">Incoming message from <c>d</c>.</param> /// <param name="to_log">Outgoing message to <c>log</c>.</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_(log,d) p(log,d) factor(log,d))</c>.</para> /// </remarks> public static double LogAverageFactor(Gaussian log, Gamma d, [Fresh] Gaussian to_log) { Gamma g = Gamma.FromShapeAndRate(d.Shape + 1, d.Rate); return(d.Shape / d.Rate * ExpOp.LogAverageFactor(g, log, to_log)); }
/// <summary>EP message to <c>d</c>.</summary> /// <param name="log">Incoming message from <c>log</c>. Must be a proper distribution. If uniform, the result will be uniform.</param> /// <param name="d">Incoming message from <c>d</c>.</param> /// <param name="to_log">Previous outgoing message to <c>log</c>.</param> /// <returns>The outgoing EP message to the <c>d</c> argument.</returns> /// <remarks> /// <para>The outgoing message is a distribution matching the moments of <c>d</c> as the random arguments are varied. The formula is <c>proj[p(d) sum_(log) p(log) factor(log,d)]/p(d)</c>.</para> /// </remarks> /// <exception cref="ImproperMessageException"> /// <paramref name="log" /> is not a proper distribution.</exception> public static Gamma DAverageConditional([Proper] Gaussian log, Gamma d, Gaussian to_log) { var g = Gamma.FromShapeAndRate(d.Shape + 1, d.Rate); return(ExpOp.ExpAverageConditional(g, log, to_log)); }