/// <summary> /// Samples an inverse Wishart distributed random variable by sampling /// a Wishart random variable and inverting the matrix. /// </summary> /// <param name="rnd">The random number generator to use.</param> /// <param name="nu">The degrees of freedom.</param> /// <param name="s">The scale matrix.</param> /// <returns>a sample from the distribution.</returns> public static Matrix<double> Sample(Random rnd, double nu, Matrix<double> s) { if (Control.CheckDistributionParameters && !IsValidParameterSet(nu, s)) { throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); } var r = Wishart.Sample(rnd, nu, s.Inverse()); return r.Inverse(); }
/// <summary> /// Samples an inverse Wishart distributed random variable by sampling /// a Wishart random variable and inverting the matrix. /// </summary> /// <param name="rnd">The random number generator to use.</param> /// <param name="degreesOfFreedom">The degree of freedom (ν) for the inverse Wishart distribution.</param> /// <param name="scale">The scale matrix (Ψ) for the inverse Wishart distribution.</param> /// <returns>a sample from the distribution.</returns> public static Matrix <double> Sample(System.Random rnd, double degreesOfFreedom, Matrix <double> scale) { if (Control.CheckDistributionParameters && !IsValidParameterSet(degreesOfFreedom, scale)) { throw new ArgumentException(Resource.InvalidDistributionParameters); } var r = Wishart.Sample(rnd, degreesOfFreedom, scale.Inverse()); return(r.Inverse()); }
public void CanCreateWishart(double nu, int order) { var matrix = Matrix<double>.Build.RandomPositiveDefinite(order, 1); var d = new Wishart(nu, matrix); Assert.AreEqual(nu, d.DegreesOfFreedom); for (var i = 0; i < d.Scale.RowCount; i++) { for (var j = 0; j < d.Scale.ColumnCount; j++) { Assert.AreEqual(matrix[i, j], d.Scale[i, j]); } } }
public void CanGetS() { const int Order = 2; var matrix = Matrix<double>.Build.RandomPositiveDefinite(Order, 1); var d = new Wishart(1.0, matrix); for (var i = 0; i < Order; i++) { for (var j = 0; j < Order; j++) { Assert.AreEqual(matrix[i, j], d.Scale[i, j]); } } }
public void CanGetNu(double nu) { var d = new Wishart(nu, Matrix<double>.Build.RandomPositiveDefinite(2, 1)); Assert.AreEqual(nu, d.DegreesOfFreedom); }
public void ValidateVariance(double nu, int order) { var d = new Wishart(nu, Matrix<double>.Build.RandomPositiveDefinite(order, 1)); var variance = d.Variance; for (var i = 0; i < d.Scale.RowCount; i++) { for (var j = 0; j < d.Scale.ColumnCount; j++) { Assert.AreEqual(nu * ((d.Scale[i, j] * d.Scale[i, j]) + (d.Scale[i, i] * d.Scale[j, j])), variance[i, j]); } } }
public void ValidateToString() { var d = new Wishart(1.0, Matrix<double>.Build.RandomPositiveDefinite(2, 1)); Assert.AreEqual("Wishart(DegreesOfFreedom = 1, Rows = 2, Columns = 2)", d.ToString()); }
public void ValidateMode(double nu, int order) { var d = new Wishart(nu, Matrix<double>.Build.RandomPositiveDefinite(order, 1)); var mode = d.Mode; for (var i = 0; i < d.Scale.RowCount; i++) { for (var j = 0; j < d.Scale.ColumnCount; j++) { Assert.AreEqual((nu - d.Scale.RowCount - 1.0) * d.Scale[i, j], mode[i, j]); } } }
public void ValidateDensity(double nu, double density) { const int Order = 1; var matrix = new DenseMatrix(Order); matrix[0, 0] = 1; var x = new DenseMatrix(Order); x[0, 0] = 5; var d = new Wishart(nu, matrix); AssertHelpers.AlmostEqualRelative(density, d.Density(x), 16); }
public void HasRandomSource() { var d = new Wishart(1.0, Matrix<double>.Build.RandomPositiveDefinite(2, 1)); Assert.IsNotNull(d.RandomSource); }
public void FailSetRandomSourceWithNullReference() { var d = new Wishart(1.0, Matrix<double>.Build.RandomPositiveDefinite(2, 1)); Assert.Throws<ArgumentNullException>(() => d.RandomSource = null); }
public void CanSample() { var d = new Wishart(1.0, Matrix<double>.Build.RandomPositiveDefinite(2, 1)); d.Sample(); }
public void ValidateDensity(double nu, double density) { var matrix = Matrix<double>.Build.Dense(1, 1, 1.0); var x = Matrix<double>.Build.Dense(1, 1, 5.0); var d = new Wishart(nu, matrix); AssertHelpers.AlmostEqualRelative(density, d.Density(x), 16); }