1 #ifndef STAN_MATH_PRIM_MAT_PROB_GAUSSIAN_DLM_OBS_LOG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_GAUSSIAN_DLM_OBS_LOG_HPP
4 #include <boost/random/normal_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
70 template <
bool propto,
72 typename T_F,
typename T_G,
73 typename T_V,
typename T_W,
74 typename T_m0,
typename T_C0
78 typename return_type<T_F, T_G, T_V, T_W, T_m0, T_C0>::type >::type
80 <T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
82 <T_F, Eigen::Dynamic, Eigen::Dynamic>& F,
84 <T_G, Eigen::Dynamic, Eigen::Dynamic>& G,
86 <T_V, Eigen::Dynamic, Eigen::Dynamic>& V,
88 <T_W, Eigen::Dynamic, Eigen::Dynamic>& W,
89 const Eigen::Matrix<T_m0, Eigen::Dynamic, 1>& m0,
91 <T_C0, Eigen::Dynamic, Eigen::Dynamic>& C0) {
92 static const char*
function(
"stan::math::gaussian_dlm_obs_log");
122 "columns of F", F.cols(),
123 "rows of y", y.rows());
125 "rows of F", F.rows(),
126 "rows of G", G.rows());
133 "rows of V", V.rows(),
134 "rows of y", y.rows());
140 "rows of W", W.rows(),
141 "rows of G", G.rows());
147 "size of m0", m0.size(),
148 "rows of G", G.rows());
152 "rows of C0", C0.rows(),
153 "rows of G", G.rows());
157 if (y.cols() == 0 || y.rows() == 0)
165 Eigen::Matrix<T_lp, Eigen::Dynamic, 1> m(n);
166 Eigen::Matrix<T_lp, Eigen::Dynamic, Eigen::Dynamic> C(n, n);
169 for (
int i = 0; i < m0.size(); i++) {
172 for (
int i = 0; i < C0.rows(); i++) {
173 for (
int j = 0; j < C0.cols(); j++) {
178 Eigen::Matrix<typename return_type<T_y>::type,
179 Eigen::Dynamic, 1> yi(r);
180 Eigen::Matrix<T_lp, Eigen::Dynamic, 1> a(n);
181 Eigen::Matrix<T_lp, Eigen::Dynamic, Eigen::Dynamic> R(n, n);
182 Eigen::Matrix<T_lp, Eigen::Dynamic, 1> f(r);
183 Eigen::Matrix<T_lp, Eigen::Dynamic, Eigen::Dynamic> Q(r, r);
184 Eigen::Matrix<T_lp, Eigen::Dynamic, Eigen::Dynamic> Q_inv(r, r);
185 Eigen::Matrix<T_lp, Eigen::Dynamic, 1>
e(r);
186 Eigen::Matrix<T_lp, Eigen::Dynamic, Eigen::Dynamic> A(n, r);
188 for (
int i = 0; i < y.cols(); i++) {
216 template <
typename T_y,
217 typename T_F,
typename T_G,
218 typename T_V,
typename T_W,
219 typename T_m0,
typename T_C0
226 <T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
228 <T_F, Eigen::Dynamic, Eigen::Dynamic>& F,
230 <T_G, Eigen::Dynamic, Eigen::Dynamic>& G,
232 <T_V, Eigen::Dynamic, Eigen::Dynamic>& V,
234 <T_W, Eigen::Dynamic, Eigen::Dynamic>& W,
235 const Eigen::Matrix<T_m0, Eigen::Dynamic, 1>& m0,
237 <T_C0, Eigen::Dynamic, Eigen::Dynamic>& C0) {
238 return gaussian_dlm_obs_log<false>(y, F, G, V, W, m0, C0);
276 template <
bool propto,
278 typename T_F,
typename T_G,
279 typename T_V,
typename T_W,
280 typename T_m0,
typename T_C0
286 <T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
288 <T_F, Eigen::Dynamic, Eigen::Dynamic>& F,
290 <T_G, Eigen::Dynamic, Eigen::Dynamic>& G,
291 const Eigen::Matrix<T_V, Eigen::Dynamic, 1>& V,
293 <T_W, Eigen::Dynamic, Eigen::Dynamic>& W,
294 const Eigen::Matrix<T_m0, Eigen::Dynamic, 1>& m0,
296 <T_C0, Eigen::Dynamic, Eigen::Dynamic>& C0) {
297 static const char*
function(
"stan::math::gaussian_dlm_obs_log");
329 "columns of F", F.cols(),
330 "rows of y", y.rows());
332 "rows of F", F.rows(),
333 "rows of G", G.rows());
338 "rows of G", G.rows(),
339 "columns of G", G.cols());
345 "size of V", V.size(),
346 "rows of y", y.rows());
353 "rows of W", W.rows(),
354 "rows of G", G.rows());
360 "size of m0", m0.size(),
361 "rows of G", G.rows());
367 "rows of C0", C0.rows(),
368 "rows of G", G.rows());
372 if (y.cols() == 0 || y.rows() == 0)
384 Eigen::Matrix<T_lp, Eigen::Dynamic, 1> A(n);
385 Eigen::Matrix<T_lp, Eigen::Dynamic, 1> Fj(n);
386 Eigen::Matrix<T_lp, Eigen::Dynamic, 1> m(n);
387 Eigen::Matrix<T_lp, Eigen::Dynamic, Eigen::Dynamic> C(n, n);
390 for (
int i = 0; i < m0.size(); i++) {
393 for (
int i = 0; i < C0.rows(); i++) {
394 for (
int j = 0; j < C0.cols(); j++) {
399 for (
int i = 0; i < y.cols(); i++) {
404 for (
int j = 0; j < y.rows(); ++j) {
408 for (
int k = 0; k < F.rows(); ++k) {
427 lp -= 0.5 * (
log(Q) +
pow(e, 2) * Q_inv);
434 template <
typename T_y,
435 typename T_F,
typename T_G,
436 typename T_V,
typename T_W,
437 typename T_m0,
typename T_C0>
442 (
const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
443 const Eigen::Matrix<T_F, Eigen::Dynamic, Eigen::Dynamic>& F,
444 const Eigen::Matrix<T_G, Eigen::Dynamic, Eigen::Dynamic>& G,
445 const Eigen::Matrix<T_V, Eigen::Dynamic, 1>& V,
446 const Eigen::Matrix<T_W, Eigen::Dynamic, Eigen::Dynamic>& W,
447 const Eigen::Matrix<T_m0, Eigen::Dynamic, 1>& m0,
448 const Eigen::Matrix<T_C0, Eigen::Dynamic, Eigen::Dynamic>& C0) {
449 return gaussian_dlm_obs_log<false>(y, F, G, V, W, m0, C0);
bool check_not_nan(const char *function, const char *name, const T_y &y)
Return true if y is not NaN.
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R, C > subtract(const Eigen::Matrix< T1, R, C > &m1, const Eigen::Matrix< T2, R, C > &m2)
Return the result of subtracting the second specified matrix from the first specified matrix...
return_type< T_y, typename return_type< T_F, T_G, T_V, T_W, T_m0, T_C0 >::type >::type gaussian_dlm_obs_log(const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const Eigen::Matrix< T_F, Eigen::Dynamic, Eigen::Dynamic > &F, const Eigen::Matrix< T_G, Eigen::Dynamic, Eigen::Dynamic > &G, const Eigen::Matrix< T_V, Eigen::Dynamic, Eigen::Dynamic > &V, const Eigen::Matrix< T_W, Eigen::Dynamic, Eigen::Dynamic > &W, const Eigen::Matrix< T_m0, Eigen::Dynamic, 1 > &m0, const Eigen::Matrix< T_C0, Eigen::Dynamic, Eigen::Dynamic > &C0)
The log of a Gaussian dynamic linear model (GDLM).
fvar< T > log(const fvar< T > &x)
Eigen::Matrix< fvar< T >, R1, C1 > multiply(const Eigen::Matrix< fvar< T >, R1, C1 > &m, const fvar< T > &c)
Metaprogram to calculate the base scalar return type resulting from promoting all the scalar types of...
bool check_cov_matrix(const char *function, const char *name, const Eigen::Matrix< T_y, Dynamic, Dynamic > &y)
Return true if the specified matrix is a valid covariance matrix.
Eigen::Matrix< fvar< T >, R, R > tcrossprod(const Eigen::Matrix< fvar< T >, R, C > &m)
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Eigen::Matrix< T, CB, CB > quad_form_sym(const Eigen::Matrix< T, RA, CA > &A, const Eigen::Matrix< T, RB, CB > &B)
boost::math::tools::promote_args< typename scalar_type< T1 >::type, typename scalar_type< T2 >::type, typename scalar_type< T3 >::type, typename scalar_type< T4 >::type, typename scalar_type< T5 >::type, typename scalar_type< T6 >::type >::type type
stan::math::fvar< T > trace_quad_form(const Eigen::Matrix< stan::math::fvar< T >, RA, CA > &A, const Eigen::Matrix< stan::math::fvar< T >, RB, CB > &B)
bool check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Return true if the provided sizes match.
fvar< T > dot_product(const Eigen::Matrix< fvar< T >, R1, C1 > &v1, const Eigen::Matrix< fvar< T >, R2, C2 > &v2)
bool check_spsd_matrix(const char *function, const char *name, const Eigen::Matrix< T_y, Dynamic, Dynamic > &y)
Return true if the specified matrix is a square, symmetric, and positive semi-definite.
double e()
Return the base of the natural logarithm.
bool check_finite(const char *function, const char *name, const T_y &y)
Return true if y is finite.
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > inverse_spd(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m)
Returns the inverse of the specified symmetric, pos/neg-definite matrix.
const double NEG_LOG_TWO_PI
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R, C > add(const Eigen::Matrix< T1, R, C > &m1, const Eigen::Matrix< T2, R, C > &m2)
Return the sum of the specified matrices.
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
bool check_nonnegative(const char *function, const char *name, const T_y &y)
Return true if y is non-negative.
Eigen::Matrix< T, C, R > transpose(const Eigen::Matrix< T, R, C > &m)
bool check_square(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is square.
T log_determinant_spd(const Eigen::Matrix< T, R, C > &m)
Returns the log absolute determinant of the specified square matrix.