/// <summary> /// Gets the integral of the product of two Gaussians. /// </summary> /// <param name="that"></param> /// <remarks> /// <c>this = N(x;m1,v1)</c>. /// <c>that = N(x;m2,v2)</c>. /// <c>int_(-infinity)^(infinity) N(x;m1,v1) N(x;m2,v2) dx = N(m1; m2, v1+v2)</c>. /// When improper, the density is redefined to be <c>exp(-0.5*x^2*(1/v) + x*(m/v))</c>, /// i.e. we drop the terms <c>exp(-m^2/(2v))/sqrt(2*pi*v)</c>. /// </remarks> /// <returns>log(N(m1;m2,v1+v2)).</returns> public double GetLogAverageOf(Gaussian that) { if (IsPointMass) { return(that.GetLogProb(Point)); } else if (that.IsPointMass) { return(GetLogProb(that.Point)); } else { // neither this nor that is a point mass. // int_x N(x;m1,v1) N(x;m2,v2) dx = N(m1;m2,v1+v2) // (m1-m2)^2/(v1+v2) = (m1-m2)^2/(v1*v2*product.Prec) // (m1-m2)^2/(v1*v2) = (m1/(v1*v2) - m2/(v1*v2))^2 *v1*v2 Gaussian product = this * that; //if (!product.IsProper()) throw new ArgumentException("The product is improper."); return(product.GetLogNormalizer() - this.GetLogNormalizer() - that.GetLogNormalizer()); } }
/// <summary> /// Get the integral of this distribution times another distribution raised to a power. /// </summary> /// <param name="that"></param> /// <param name="power"></param> /// <returns></returns> public double GetLogAverageOfPower(Gaussian that, double power) { if (IsPointMass) { return(power * that.GetLogProb(Point)); } else if (that.IsPointMass) { if (power < 0) { throw new DivideByZeroException("The exponent is negative and the distribution is a point mass"); } return(this.GetLogProb(that.Point)); } else { var product = this * (that ^ power); return(product.GetLogNormalizer() - this.GetLogNormalizer() - power * that.GetLogNormalizer()); } }