-
-
Notifications
You must be signed in to change notification settings - Fork 187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/matrix input to mvn [WIP] #2540
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,8 @@ | |
namespace stan { | ||
namespace math { | ||
|
||
template <bool propto, typename T_y, typename T_loc, typename T_covar> | ||
template <bool propto, typename T_y, typename T_loc, typename T_covar, | ||
require_all_vector_t<T_y, T_loc>* = nullptr> | ||
return_type_t<T_y, T_loc, T_covar> multi_normal_lpdf(const T_y& y, | ||
const T_loc& mu, | ||
const T_covar& Sigma) { | ||
|
@@ -102,6 +103,24 @@ return_type_t<T_y, T_loc, T_covar> multi_normal_lpdf(const T_y& y, | |
return lp; | ||
} | ||
|
||
template <bool propto, typename T_y, typename T_loc, typename T_covar, | ||
require_all_not_vector_t<T_y, T_loc>* = nullptr> | ||
return_type_t<T_y, T_loc, T_covar> multi_normal_lpdf(const T_y& y, | ||
const T_loc& mu, | ||
const T_covar& Sigma) { | ||
using lp_type = return_type_t<T_y, T_loc, T_covar>; | ||
|
||
const size_t N = y.rows(); | ||
lp_type lp(0.0); | ||
|
||
for (size_t n = 0; n < N; ++n) { | ||
auto current_y = y.row(n); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this code safe? What happens if I'd like to see this fixed before it gets into the code base. Perhaps use a |
||
auto current_mu = mu.row(n); | ||
lp += multi_normal_lpdf<false>(current_y, current_mu, Sigma); | ||
} | ||
return lp; | ||
} | ||
|
||
template <typename T_y, typename T_loc, typename T_covar> | ||
inline return_type_t<T_y, T_loc, T_covar> multi_normal_lpdf( | ||
const T_y& y, const T_loc& mu, const T_covar& Sigma) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,22 @@ TEST(ProbDistributionsMultiNormal, NotVectorized) { | |
EXPECT_NO_THROW(stan::math::multi_normal_rng(mu, Sigma, rng)); | ||
} | ||
|
||
TEST(ProbDistributionsMultiNormal, MatrixNotVectorized) { | ||
using Eigen::Dynamic; | ||
using Eigen::Matrix; | ||
using std::vector; | ||
|
||
boost::random::mt19937 rng; | ||
Matrix<double, Dynamic, Dynamic> y(2, 3); | ||
y << 2.0, -2.0, 11.0, 2.0, -2.0, 11.0; | ||
Matrix<double, Dynamic, Dynamic> mu(2, 3); | ||
mu << 1.0, -1.0, 3.0, 1.0, -1.0, 3.0; | ||
Matrix<double, Dynamic, Dynamic> Sigma(3, 3); | ||
Sigma << 9.0, -3.0, 0.0, -3.0, 4.0, 0.0, 0.0, 0.0, 5.0; | ||
EXPECT_FLOAT_EQ(-23.47816, stan::math::multi_normal_lpdf(y, mu, Sigma)); | ||
EXPECT_NO_THROW(stan::math::multi_normal_rng(mu, Sigma, rng)); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a test where |
||
TEST(ProbDistributionsMultiNormal, Vectorized) { | ||
using Eigen::Dynamic; | ||
using Eigen::Matrix; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you take away the
using lp_type...
statement on 111 and declarelp
as that type? It's easier to read when that's together.