/// <summary> /// Evaluates the probability density function for the Wishart distribution. /// </summary> /// <param name="x">The matrix at which to evaluate the density at.</param> /// <exception cref="ArgumentOutOfRangeException">If the argument does not have the same dimensions as the scale matrix.</exception> /// <returns>the density at <paramref name="x"/>.</returns> public double Density(Matrix <double> x) { var p = _scale.RowCount; if (x.RowCount != p || x.ColumnCount != p) { throw Matrix.DimensionsDontMatch <ArgumentOutOfRangeException>(x, _scale, "x"); } var dX = x.Determinant(); var siX = _chol.Solve(x); // Compute the multivariate Gamma function. var gp = Math.Pow(Constants.Pi, p * (p - 1.0) / 4.0); for (var j = 1; j <= p; j++) { gp *= SpecialFunctions.Gamma((_degreesOfFreedom + 1.0 - j) / 2.0); } return(Math.Pow(dX, (_degreesOfFreedom - p - 1.0) / 2.0) * Math.Exp(-0.5 * siX.Trace()) / Math.Pow(2.0, _degreesOfFreedom * p / 2.0) / Math.Pow(_chol.Determinant, _degreesOfFreedom / 2.0) / gp); }
/// <summary> /// Evaluates the probability density function for the Wishart distribution. /// </summary> /// <param name="x">The matrix at which to evaluate the density at.</param> /// <exception cref="ArgumentOutOfRangeException">If the argument does not have the same dimensions as the scale matrix.</exception> /// <returns>the density at <paramref name="x"/>.</returns> public double Density(Matrix <double> x) { var p = _s.RowCount; if (x.RowCount != p || x.ColumnCount != p) { throw new ArgumentOutOfRangeException("x", Resources.ArgumentMatrixDimensions); } var dX = x.Determinant(); var siX = _chol.Solve(x); // Compute the multivariate Gamma function. var gp = Math.Pow(Constants.Pi, p * (p - 1.0) / 4.0); for (var j = 1; j <= p; j++) { gp *= SpecialFunctions.Gamma((_nu + 1.0 - j) / 2.0); } return(Math.Pow(dX, (_nu - p - 1.0) / 2.0) * Math.Exp(-0.5 * siX.Trace()) / Math.Pow(2.0, _nu * p / 2.0) / Math.Pow(_chol.Determinant, _nu / 2.0) / gp); }
public void ShouldSolve() { var a = new double[, ] { { 6.0, 15.0, 55.0 }, { 15.0, 55.0, 225.0 }, { 55.0, 225.0, 979.0 } }; var rhs = new double[] { 9.5, 50.0, 237.0 }; var solution = Cholesky.Solve(a, rhs); var expected = new double[] { -0.5, -1.0, 0.5 }; Assert.AreEqual(expected.Length, solution.Length); for (var i = 0; i < solution.Length; ++i) { Assert.AreEqual(expected[i], solution[i], 0.000001); } }