Exemplo n.º 1
0
 typename boost::math::tools::promote_args<T_y,T_loc,T_scale,T_shape>::type
 lkj_cov_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
             const Eigen::Matrix<T_loc,Eigen::Dynamic,1>& mu,
             const Eigen::Matrix<T_scale,Eigen::Dynamic,1>& sigma,
             const T_shape& eta,
             const Policy&) {
   static const char* function = "stan::prob::lkj_cov_log(%1%)";
   
   using stan::math::check_size_match;
   using stan::math::check_finite;
   using stan::math::check_positive;
   using boost::math::tools::promote_args;
   
   typename promote_args<T_y,T_loc,T_scale,T_shape>::type lp(0.0);
   if (!check_size_match(function, 
       mu.rows(), "Rows of location parameter",
       sigma.rows(), "columns of scale parameter",
       &lp, Policy()))
     return lp;
   if (!check_size_match(function, 
       y.rows(), "Rows of random variable",
       y.cols(), "columns of random variable",
       &lp, Policy()))
   return lp;
   if (!check_size_match(function, 
       y.rows(), "Rows of random variable",
       mu.rows(), "rows of location parameter",
       &lp, Policy()))
     return lp;
   if (!check_positive(function, eta, "Shape parameter", &lp, Policy()))
     return lp;
   if (!check_finite(function, mu, "Location parameter", &lp, Policy()))
     return lp;
   if (!check_finite(function, sigma, "Scale parameter", &lp, Policy()))
     return lp;
   // FIXME: build vectorized versions
   for (int m = 0; m < y.rows(); ++m)
     for (int n = 0; n < y.cols(); ++n)
       if (!check_finite(function, y(m,n), "Covariance matrix", &lp, Policy()))
         return lp;
   
   const unsigned int K = y.rows();
   const Eigen::Array<T_y,Eigen::Dynamic,1> sds
     = y.diagonal().array().sqrt();
   for (unsigned int k = 0; k < K; k++) {
     lp += lognormal_log<propto>(sds(k), mu(k), sigma(k), Policy());
   }
   if (stan::is_constant<typename stan::scalar_type<T_shape> >::value
       && eta == 1.0) {
     // no need to rescale y into a correlation matrix
     lp += lkj_corr_log<propto,T_y,T_shape,Policy>(y, eta, Policy()); 
     return lp;
   }
   Eigen::DiagonalMatrix<T_y,Eigen::Dynamic> D(K);
   D.diagonal() = sds.inverse();
   lp += lkj_corr_log<propto,T_y,T_shape,Policy>(D * y * D, eta, Policy());
   return lp;
 }
Exemplo n.º 2
0
    typename boost::math::tools::promote_args<T_y,T_loc,T_scale,T_shape>::type
    lkj_cov_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
                const T_loc& mu, 
                const T_scale& sigma, 
                const T_shape& eta, 
                const Policy&) {
      static const char* function = "stan::prob::lkj_cov_log(%1%)";

      using stan::math::check_finite;
      using stan::math::check_positive;
      using boost::math::tools::promote_args;
      
      typename promote_args<T_y,T_loc,T_scale,T_shape>::type lp(0.0);
      if (!check_positive(function, eta, "Shape parameter", &lp, Policy()))
        return lp;
      if (!check_finite(function, mu, "Location parameter", &lp, Policy()))
        return lp;
      if (!check_finite(function, sigma, "Scale parameter", 
                        &lp, Policy()))
        return lp;
      
      const unsigned int K = y.rows();
      const Eigen::Array<T_y,Eigen::Dynamic,1> sds
        = y.diagonal().array().sqrt();
      for (unsigned int k = 0; k < K; k++) {
        lp += lognormal_log<propto>(sds(k), mu, sigma, Policy());
      }
      if (stan::is_constant<typename stan::scalar_type<T_shape> >::value
          && eta == 1.0) {
        // no need to rescale y into a correlation matrix
        lp += lkj_corr_log<propto>(y,eta,Policy()); 
        return lp;
      }
      Eigen::DiagonalMatrix<T_y,Eigen::Dynamic> D(K);
      D.diagonal() = sds.inverse();
      lp += lkj_corr_log<propto,T_y,T_shape,Policy>(D * y * D, eta, Policy());
      return lp;
    }