1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
// SPDX-License-Identifier: Apache-2.0
//
// Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
// Copyright 2008-2016 National ICT Australia (NICTA)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
//! \addtogroup glue_mvnrnd
//! @{
// implementation based on:
// James E. Gentle.
// Generation of Random Numbers.
// Computational Statistics, pp. 305-331, 2009.
// http://dx.doi.org/10.1007/978-0-387-98144-4_7
template<typename T1, typename T2>
inline
void
glue_mvnrnd_vec::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_mvnrnd_vec>& expr)
{
arma_extra_debug_sigprint();
const bool status = glue_mvnrnd::apply_direct(out, expr.A, expr.B, uword(1));
if(status == false)
{
out.soft_reset();
arma_stop_runtime_error("mvnrnd(): given covariance matrix is not symmetric positive semi-definite");
}
}
template<typename T1, typename T2>
inline
void
glue_mvnrnd::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_mvnrnd>& expr)
{
arma_extra_debug_sigprint();
const bool status = glue_mvnrnd::apply_direct(out, expr.A, expr.B, expr.aux_uword);
if(status == false)
{
out.soft_reset();
arma_stop_runtime_error("mvnrnd(): given covariance matrix is not symmetric positive semi-definite");
}
}
template<typename T1, typename T2>
inline
bool
glue_mvnrnd::apply_direct(Mat<typename T1::elem_type>& out, const Base<typename T1::elem_type,T1>& M, const Base<typename T1::elem_type,T2>& C, const uword N)
{
arma_extra_debug_sigprint();
typedef typename T1::elem_type eT;
const quasi_unwrap<T1> UM(M.get_ref());
const quasi_unwrap<T2> UC(C.get_ref());
arma_debug_check( (UM.M.is_colvec() == false) && (UM.M.is_empty() == false), "mvnrnd(): given mean must be a column vector" );
arma_debug_check( (UC.M.is_square() == false), "mvnrnd(): given covariance matrix must be square sized" );
arma_debug_check( (UM.M.n_rows != UC.M.n_rows), "mvnrnd(): number of rows in given mean vector and covariance matrix must match" );
if( UM.M.is_empty() || UC.M.is_empty() )
{
out.set_size(0,N);
return true;
}
if((arma_config::debug) && (auxlib::rudimentary_sym_check(UC.M) == false))
{
arma_debug_warn_level(1, "mvnrnd(): given matrix is not symmetric");
}
bool status = false;
if(UM.is_alias(out) || UC.is_alias(out))
{
Mat<eT> tmp;
status = glue_mvnrnd::apply_noalias(tmp, UM.M, UC.M, N);
out.steal_mem(tmp);
}
else
{
status = glue_mvnrnd::apply_noalias(out, UM.M, UC.M, N);
}
return status;
}
template<typename eT>
inline
bool
glue_mvnrnd::apply_noalias(Mat<eT>& out, const Mat<eT>& M, const Mat<eT>& C, const uword N)
{
arma_extra_debug_sigprint();
Mat<eT> D;
const bool chol_status = op_chol::apply_direct(D, C, 1); // '1' means "lower triangular"
if(chol_status == false)
{
// C is not symmetric positive definite, so find approximate square root of C
Col<eT> eigval; // NOTE: eT is constrained to be real (ie. float or double) in fn_mvnrnd.hpp
Mat<eT> eigvec;
const bool eig_status = eig_sym_helper(eigval, eigvec, C, 'd', "mvnrnd()");
if(eig_status == false) { return false; }
eT* eigval_mem = eigval.memptr();
const uword eigval_n_elem = eigval.n_elem;
// since we're doing an approximation, tolerate tiny negative eigenvalues
const eT tol = eT(-100) * Datum<eT>::eps * norm(C, "fro");
if(arma_isfinite(tol) == false) { return false; }
for(uword i=0; i<eigval_n_elem; ++i)
{
const eT val = eigval_mem[i];
if( (val < tol) || (arma_isfinite(val) == false) ) { return false; }
}
for(uword i=0; i<eigval_n_elem; ++i) { if(eigval_mem[i] < eT(0)) { eigval_mem[i] = eT(0); } }
Mat<eT> DD = eigvec * diagmat(sqrt(eigval));
D.steal_mem(DD);
}
out = D * randn< Mat<eT> >(M.n_rows, N);
if(N == 1)
{
out += M;
}
else
if(N > 1)
{
out.each_col() += M;
}
return true;
}
//! @}
|