2
0
mirror of https://github.com/boostorg/random.git synced 2026-01-27 19:12:17 +00:00
Files
random/doc/multivariate_normal_distribution.html
Matthias Troyer aeb0efa241 Added multivariate normal example
[SVN r3062]
2006-07-05 11:50:55 +00:00

167 lines
5.9 KiB
HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Multivariate normal distribution</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<h1>Multivariate normal distribution</h1>
<h2><a name="intro">Introduction</a></h2>
This header provides a multi-variate normal distribution of correlated normally distributed random numbers.
<h3>Synopsis</h3>
<h2><a name="normal_distribution">Class template <code>multivariate_normal_distribution</code></a></h2>
<h3>Synopsis</h3>
<pre>
#include &lt;<a href="../../../boost/random/multivariate_normal_distribution.hpp">boost/random/multivariate_normal_distribution.hpp</a>&gt;
template&lt;class RealType = double&gt;
class multivariate_normal_distribution
{
public:
typedef RealType input_type;
typedef RealType result_type;
typedef numeric::ublas::vector&lt;RealType> vector_type;
typedef numeric::ublas::matrix&lt;RealType> matrix_type;
multivariate_normal_distribution(const vector_type&amp; m, const matrix_type&amp; c);
explicit multivariate_normal_distribution(const matrix_type&amp; c)
vector_type const&amp; mean() const;
matrix_type const&amp; cholesky() const;
void reset();
template&lt;class UniformRandomNumberGenerator&gt;
result_type operator()(UniformRandomNumberGenerator&amp; urng);
};
</pre>
<h3>Description</h3>
Instantiations of class template <code>multivariate_normal_distribution</code>
model a <a href="http://www.boost.org/libs/random/doc/random-concepts.html#random-dist">random
distribution</a>, producing correlated multivariate normally distributed random numbers.
Multivariate normally distributed random numbers are sequences of n random numbers, distributed with given mean values M(i),
and covariance matrix Cov(i,j).
<p>
Instead of using a sequence type as <code>result_type</code> the design decision was to keep the underlying real
number type as the <code>result_type</code>. Hence, n succesive calls are required to obtain all n multivariate
normal numbers. This removes the need to choose a specific container type as <code>result_type</code>, while still
allowing any container to be filled using, for example, <code>std::generate</code>.
The constructor of the distribution takes the Cholesky decomposition C of the covariance matrix Cov = C<SUP>T</SUP>C, and the vector of
mean values M. The algoritm used first creates a vector v of n normally distributed random numbers with 0 mean and unit
variance and then calculates the multivariate randim numbers using the equation m + C * v.
<h3>Members</h3>
<pre>
multivariate_normal_distribution(const vector_type&amp; m, const matrix_type&amp; c);
</pre>
<strong>Requires:</strong> m.size() == c.size1() &amp;&amp; c.size1() == c.size2()
<br><strong>Effects:</strong> Constructs a
<code>multivariate_normal_distribution</code> object; <code>m</code> and
<code>c</code> are the mean values and Cholesky deceomposition of the covariance matrix of the distribution.
<pre>
explicit multivariate_normal_distribution(const vector_type&amp; m,
const matrix_type&amp; c);
</pre>
<strong>Requires:</strong>c.size1() == c.size2()
<br><strong>Effects:</strong> Constructs a
<code>multivariate_normal_distribution</code> object; <code>c</code> is Cholesky deceomposition of the covariance
matrix of the distribution, with zero mean values.
<pre> vector_type const&amp; mean() const</pre>
<strong>Returns:</strong> The mean parameter of the distribution.
<pre> matrix_type const&amp; cholesky() const</pre>
<strong>Returns:</strong> The Cholesky decomposition of the covariance matrix of the distribution.
<pre>
template&lt;class UniformRandomNumberGenerator&gt;
result_type operator()(UniformRandomNumberGenerator&amp; urng);
</pre>
<strong>Returns:</strong> On the first call, it creates a vector of n multivariate normally distributed random numbers, caches it,
and returns the first value. The next n-1 calls return the next elements of the vector. The n+1-st call again creates a new vector
of multivariate normally distributed random numbers.
<pre>
void reset()
</pre>
<strong>Effects:</strong> Clears the cache of multivariate normally distributed random numbers.
The next call to <code>operator()(UniformRandomNumberGenerator&amp; urng)</code> will create a new vector
of multivariate normally distributed random numbers.
<p>
<h3>Example</h3>
In the example we print 100 pairs of correlated Gaussian random numbers, with mean
<blockquote>
<table><tr><td>-1</td></tr><tr><td>1</td></tr></table>
</blockquote>
and covariance matrix
<blockquote>
<table><tr><td>2</td><td>2</td></tr><tr><td>2</td><td>2</td></tr></table>
</blockquote>
The Cholesky decomposition (square root) of the covariance matrix in this case is
<blockquote>
<table><tr><td>1</td><td>1</td></tr><tr><td>1</td><td>1</td></tr></table>
</blockquote>
<pre>
#include &lt;boost/random/multivariate_normal_distribution.hpp>
#include &lt;boost/random.hpp>
#include &lt;iostream>
int main()
{
// Create multivariate correlated Gaussians with mean (-1, 1) and covariance
// matrix ((2,2),(2,2))
typedef boost::multivariate_normal_distribution&lt;double> distribution_type;
// the square root (Cholesky decomposition) of the covariance matrix
distribution_type::matrix_type cholesky(2,2);
cholesky(0,0)=1.;
cholesky(0,1)=1.;
cholesky(1,0)=1.;
cholesky(1,1)=1.;
// the vector of mean values
distribution_type::vector_type mean(2);
mean(0)=-1.;
mean(1)=1.;
// create the engine, distribution, and variate generator
boost::mt19937 engine;
distribution_type dist(cholesky,mean);
boost::variate_generator&lt;boost::mt19937,distribution_type>
gen(engine,dist);
// print 100 pairs of correlated normally distributed random numbers
for (int i=0; i&lt;100; ++i)
std::cout &lt;&lt; gen() &lt;&lt; " " &lt;&lt; gen() &lt;&lt; "\n";
}
</pre>
<hr>
Matthias Troyer, 2006-02-06
</body>
</html>