Stan  2.10.0
probability, sampling & optimization
writer.hpp
Go to the documentation of this file.
1 #ifndef STAN_IO_WRITER_HPP
2 #define STAN_IO_WRITER_HPP
3 
4 #include <stan/math/prim/arr/meta/index_type.hpp>
5 #include <stan/math/prim/mat/meta/index_type.hpp>
6 #include <stan/math/prim/scal/meta/index_type.hpp>
7 #include <stan/math/prim/mat/err/check_corr_matrix.hpp>
8 #include <stan/math/prim/mat/err/check_ordered.hpp>
9 #include <stan/math/prim/mat/err/check_positive_ordered.hpp>
10 #include <stan/math/prim/mat/err/check_simplex.hpp>
11 #include <stan/math/prim/mat/err/check_unit_vector.hpp>
12 #include <stan/math/prim/mat/fun/cholesky_corr_free.hpp>
13 #include <stan/math/prim/mat/fun/cholesky_factor_free.hpp>
14 #include <stan/math/prim/mat/fun/factor_cov_matrix.hpp>
15 #include <stan/math/prim/mat/fun/simplex_free.hpp>
16 #include <stan/math/prim/mat/fun/unit_vector_free.hpp>
17 #include <stan/math/prim/scal/fun/prob_free.hpp>
18 #include <stan/math/prim/scal/fun/lb_free.hpp>
19 #include <stan/math/prim/scal/fun/lub_free.hpp>
20 #include <stdexcept>
21 #include <vector>
22 
23 namespace stan {
24 
25  namespace io {
26 
39  template <typename T>
40  class writer {
41  private:
42  std::vector<T> data_r_;
43  std::vector<int> data_i_;
44 
45  public:
46  typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrix_t;
47  typedef Eigen::Matrix<T, Eigen::Dynamic, 1> vector_t;
48  typedef Eigen::Matrix<T, 1, Eigen::Dynamic> row_vector_t;
49 
50  typedef Eigen::Array<T, Eigen::Dynamic, 1> array_vec_t;
51 
56  const double CONSTRAINT_TOLERANCE;
57 
65  writer(std::vector<T>& data_r,
66  std::vector<int>& data_i)
67  : data_r_(data_r),
68  data_i_(data_i),
69  CONSTRAINT_TOLERANCE(1E-8) {
70  data_r_.clear();
71  data_i_.clear();
72  }
73 
77  ~writer() { }
78 
85  std::vector<T>& data_r() {
86  return data_r_;
87  }
88 
89 
96  std::vector<int>& data_i() {
97  return data_i_;
98  }
99 
105  void integer(int n) {
106  data_i_.push_back(n);
107  }
108 
116  void scalar_unconstrain(T& y) {
117  data_r_.push_back(y);
118  }
119 
132  if (y < 0.0)
133  BOOST_THROW_EXCEPTION(std::runtime_error("y is negative"));
134  data_r_.push_back(log(y));
135  }
136 
148  void scalar_lb_unconstrain(double lb, T& y) {
149  data_r_.push_back(stan::math::lb_free(y, lb));
150  }
151 
162  void scalar_ub_unconstrain(double ub, T& y) {
163  data_r_.push_back(stan::math::ub_free(y, ub));
164  }
165 
178  void scalar_lub_unconstrain(double lb, double ub, T& y) {
179  data_r_.push_back(stan::math::lub_free(y, lb, ub));
180  }
181 
192  void corr_unconstrain(T& y) {
193  data_r_.push_back(stan::math::corr_free(y));
194  }
195 
207  void prob_unconstrain(T& y) {
208  data_r_.push_back(stan::math::prob_free(y));
209  }
210 
226  void ordered_unconstrain(vector_t& y) {
227  typedef typename stan::math::index_type<vector_t>::type idx_t;
228  if (y.size() == 0) return;
229  stan::math::check_ordered("stan::io::ordered_unconstrain", "Vector", y);
230  data_r_.push_back(y[0]);
231  for (idx_t i = 1; i < y.size(); ++i) {
232  data_r_.push_back(log(y[i] - y[i-1]));
233  }
234  }
235 
252  void positive_ordered_unconstrain(vector_t& y) {
253  typedef typename stan::math::index_type<vector_t>::type idx_t;
254 
255  // reimplements pos_ordered_free in prob to avoid malloc
256  if (y.size() == 0) return;
257  stan::math::check_positive_ordered
258  ("stan::io::positive_ordered_unconstrain", "Vector", y);
259  data_r_.push_back(log(y[0]));
260  for (idx_t i = 1; i < y.size(); ++i) {
261  data_r_.push_back(log(y[i] - y[i-1]));
262  }
263  }
264 
265 
271  void vector_unconstrain(const vector_t& y) {
272  typedef typename stan::math::index_type<vector_t>::type idx_t;
273  for (idx_t i = 0; i < y.size(); ++i)
274  data_r_.push_back(y[i]);
275  }
276 
282  void row_vector_unconstrain(const vector_t& y) {
283  typedef typename stan::math::index_type<vector_t>::type idx_t;
284  for (idx_t i = 0; i < y.size(); ++i)
285  data_r_.push_back(y[i]);
286  }
287 
293  void matrix_unconstrain(const matrix_t& y) {
294  typedef typename stan::math::index_type<matrix_t>::type idx_t;
295  for (idx_t j = 0; j < y.cols(); ++j)
296  for (idx_t i = 0; i < y.rows(); ++i)
297  data_r_.push_back(y(i, j));
298  }
299 
300  void vector_lb_unconstrain(double lb, vector_t& y) {
301  typedef typename stan::math::index_type<vector_t>::type idx_t;
302  for (idx_t i = 0; i < y.size(); ++i)
303  scalar_lb_unconstrain(lb, y(i));
304  }
305  void row_vector_lb_unconstrain(double lb, row_vector_t& y) {
306  typedef typename stan::math::index_type<row_vector_t>::type idx_t;
307  for (idx_t i = 0; i < y.size(); ++i)
308  scalar_lb_unconstrain(lb, y(i));
309  }
310  void matrix_lb_unconstrain(double lb, matrix_t& y) {
311  typedef typename stan::math::index_type<matrix_t>::type idx_t;
312  for (idx_t j = 0; j < y.cols(); ++j)
313  for (idx_t i = 0; i < y.rows(); ++i)
314  scalar_lb_unconstrain(lb, y(i, j));
315  }
316 
317  void vector_ub_unconstrain(double ub, vector_t& y) {
318  typedef typename stan::math::index_type<vector_t>::type idx_t;
319  for (idx_t i = 0; i < y.size(); ++i)
320  scalar_ub_unconstrain(ub, y(i));
321  }
322  void row_vector_ub_unconstrain(double ub, row_vector_t& y) {
323  typedef typename stan::math::index_type<row_vector_t>::type idx_t;
324  for (idx_t i = 0; i < y.size(); ++i)
325  scalar_ub_unconstrain(ub, y(i));
326  }
327  void matrix_ub_unconstrain(double ub, matrix_t& y) {
328  typedef typename stan::math::index_type<matrix_t>::type idx_t;
329  for (idx_t j = 0; j < y.cols(); ++j)
330  for (idx_t i = 0; i < y.rows(); ++i)
331  scalar_ub_unconstrain(ub, y(i, j));
332  }
333 
334 
335  void vector_lub_unconstrain(double lb, double ub, vector_t& y) {
336  typedef typename stan::math::index_type<vector_t>::type idx_t;
337  for (idx_t i = 0; i < y.size(); ++i)
338  scalar_lub_unconstrain(lb, ub, y(i));
339  }
340  void row_vector_lub_unconstrain(double lb, double ub, row_vector_t& y) {
341  typedef typename stan::math::index_type<row_vector_t>::type idx_t;
342  for (idx_t i = 0; i < y.size(); ++i)
343  scalar_lub_unconstrain(lb, ub, y(i));
344  }
345  void matrix_lub_unconstrain(double lb, double ub, matrix_t& y) {
346  typedef typename stan::math::index_type<matrix_t>::type idx_t;
347  for (idx_t j = 0; j < y.cols(); ++j)
348  for (idx_t i = 0; i < y.rows(); ++i)
349  scalar_lub_unconstrain(lb, ub, y(i, j));
350  }
351 
352 
353 
369  void unit_vector_unconstrain(vector_t& y) {
370  stan::math::check_unit_vector("stan::io::unit_vector_unconstrain",
371  "Vector", y);
372  typedef typename stan::math::index_type<vector_t>::type idx_t;
373  vector_t uy = stan::math::unit_vector_free(y);
374  for (idx_t i = 0; i < uy.size(); ++i)
375  data_r_.push_back(uy[i]);
376  }
377 
378 
393  void simplex_unconstrain(vector_t& y) {
394  typedef typename stan::math::index_type<vector_t>::type idx_t;
395 
396  stan::math::check_simplex("stan::io::simplex_unconstrain", "Vector", y);
397  vector_t uy = stan::math::simplex_free(y);
398  for (idx_t i = 0; i < uy.size(); ++i)
399  data_r_.push_back(uy[i]);
400  }
401 
413  void cholesky_factor_unconstrain(matrix_t& y) {
414  typedef typename stan::math::index_type<matrix_t>::type idx_t;
415 
416  // FIXME: optimize by unrolling cholesky_factor_free
417  Eigen::Matrix<T, Eigen::Dynamic, 1> y_free
418  = stan::math::cholesky_factor_free(y);
419  for (idx_t i = 0; i < y_free.size(); ++i)
420  data_r_.push_back(y_free[i]);
421  }
422 
423 
435  void cholesky_corr_unconstrain(matrix_t& y) {
436  typedef typename stan::math::index_type<matrix_t>::type idx_t;
437 
438  // FIXME: optimize by unrolling cholesky_factor_free
439  Eigen::Matrix<T, Eigen::Dynamic, 1> y_free
440  = stan::math::cholesky_corr_free(y);
441  for (idx_t i = 0; i < y_free.size(); ++i)
442  data_r_.push_back(y_free[i]);
443  }
444 
445 
457  void cov_matrix_unconstrain(matrix_t& y) {
458  typedef typename stan::math::index_type<matrix_t>::type idx_t;
459  idx_t k = y.rows();
460  if (k == 0 || y.cols() != k)
461  BOOST_THROW_EXCEPTION(
462  std::runtime_error("y must have elements and"
463  " y must be a square matrix"));
464  idx_t k_choose_2 = (k * (k-1)) / 2;
465  array_vec_t cpcs(k_choose_2);
466  array_vec_t sds(k);
467  bool successful = stan::math::factor_cov_matrix(y, cpcs, sds);
468  if (!successful)
469  BOOST_THROW_EXCEPTION
470  (std::runtime_error("factor_cov_matrix failed"));
471  for (idx_t i = 0; i < k_choose_2; ++i)
472  data_r_.push_back(cpcs[i]);
473  for (idx_t i = 0; i < k; ++i)
474  data_r_.push_back(sds[i]);
475  }
476 
492  void corr_matrix_unconstrain(matrix_t& y) {
493  typedef typename stan::math::index_type<matrix_t>::type idx_t;
494 
495  stan::math::check_corr_matrix("stan::io::corr_matrix_unconstrain",
496  "Matrix", y);
497  idx_t k = y.rows();
498  idx_t k_choose_2 = (k * (k-1)) / 2;
499  array_vec_t cpcs(k_choose_2);
500  array_vec_t sds(k);
501  bool successful = stan::math::factor_cov_matrix(y, cpcs, sds);
502  if (!successful)
503  BOOST_THROW_EXCEPTION
504  (std::runtime_error("y cannot be factorized by factor_cov_matrix"));
505  for (idx_t i = 0; i < k; ++i) {
506  // sds on log scale unconstrained
507  if (fabs(sds[i] - 0.0) >= CONSTRAINT_TOLERANCE)
508  BOOST_THROW_EXCEPTION
509  (std::runtime_error("sds on log scale are unconstrained"));
510  }
511  for (idx_t i = 0; i < k_choose_2; ++i)
512  data_r_.push_back(cpcs[i]);
513  }
514  };
515  }
516 
517 }
518 
519 #endif
void corr_matrix_unconstrain(matrix_t &y)
Writes the unconstrained correlation matrix corresponding to the specified constrained correlation ma...
Definition: writer.hpp:492
void row_vector_ub_unconstrain(double ub, row_vector_t &y)
Definition: writer.hpp:322
void matrix_unconstrain(const matrix_t &y)
Write the specified unconstrained matrix.
Definition: writer.hpp:293
Probability, optimization and sampling library.
void integer(int n)
Write the specified integer to the sequence of integer values.
Definition: writer.hpp:105
std::vector< T > & data_r()
Return a reference to the underlying vector of real values that have been written.
Definition: writer.hpp:85
void scalar_lub_unconstrain(double lb, double ub, T &y)
Write the unconstrained value corresponding to the specified value with the specified bounds...
Definition: writer.hpp:178
void cholesky_factor_unconstrain(matrix_t &y)
Writes the unconstrained Cholesky factor corresponding to the specified constrained matrix...
Definition: writer.hpp:413
void positive_ordered_unconstrain(vector_t &y)
Write the unconstrained vector that corresponds to the specified postiive ascendingly ordered vector...
Definition: writer.hpp:252
void unit_vector_unconstrain(vector_t &y)
Write the unconstrained vector corresponding to the specified unit_vector value.
Definition: writer.hpp:369
void scalar_lb_unconstrain(double lb, T &y)
Return the unconstrained version of the specified input, which is constrained to be above the specifi...
Definition: writer.hpp:148
void scalar_unconstrain(T &y)
Write the unconstrained value corresponding to the specified scalar.
Definition: writer.hpp:116
void row_vector_unconstrain(const vector_t &y)
Write the specified unconstrained vector.
Definition: writer.hpp:282
void simplex_unconstrain(vector_t &y)
Write the unconstrained vector corresponding to the specified simplex value.
Definition: writer.hpp:393
void row_vector_lub_unconstrain(double lb, double ub, row_vector_t &y)
Definition: writer.hpp:340
void vector_ub_unconstrain(double ub, vector_t &y)
Definition: writer.hpp:317
void matrix_lub_unconstrain(double lb, double ub, matrix_t &y)
Definition: writer.hpp:345
void vector_lb_unconstrain(double lb, vector_t &y)
Definition: writer.hpp:300
void cholesky_corr_unconstrain(matrix_t &y)
Writes the unconstrained Cholesky factor for a correlation matrix corresponding to the specified cons...
Definition: writer.hpp:435
void cov_matrix_unconstrain(matrix_t &y)
Writes the unconstrained covariance matrix corresponding to the specified constrained correlation mat...
Definition: writer.hpp:457
void vector_lub_unconstrain(double lb, double ub, vector_t &y)
Definition: writer.hpp:335
void matrix_ub_unconstrain(double ub, matrix_t &y)
Definition: writer.hpp:327
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > matrix_t
Definition: writer.hpp:46
void matrix_lb_unconstrain(double lb, matrix_t &y)
Definition: writer.hpp:310
Eigen::Matrix< T, Eigen::Dynamic, 1 > vector_t
Definition: writer.hpp:47
Eigen::Matrix< T, 1, Eigen::Dynamic > row_vector_t
Definition: writer.hpp:48
std::vector< int > & data_i()
Return a reference to the underlying vector of integer values that have been written.
Definition: writer.hpp:96
void scalar_pos_unconstrain(T &y)
Write the unconstrained value corresponding to the specified positive-constrained scalar...
Definition: writer.hpp:131
~writer()
Destroy this writer.
Definition: writer.hpp:77
void vector_unconstrain(const vector_t &y)
Write the specified unconstrained vector.
Definition: writer.hpp:271
void prob_unconstrain(T &y)
Write the unconstrained value corresponding to the specified probability value.
Definition: writer.hpp:207
void corr_unconstrain(T &y)
Write the unconstrained value corresponding to the specified correlation-constrained variable...
Definition: writer.hpp:192
void ordered_unconstrain(vector_t &y)
Write the unconstrained vector that corresponds to the specified ascendingly ordered vector...
Definition: writer.hpp:226
void row_vector_lb_unconstrain(double lb, row_vector_t &y)
Definition: writer.hpp:305
writer(std::vector< T > &data_r, std::vector< int > &data_i)
Construct a writer that writes to the specified scalar and integer vectors.
Definition: writer.hpp:65
void scalar_ub_unconstrain(double ub, T &y)
Write the unconstrained value corresponding to the specified lower-bounded value. ...
Definition: writer.hpp:162
A stream-based writer for integer, scalar, vector, matrix and array data types, which transforms from...
Definition: writer.hpp:40
Eigen::Array< T, Eigen::Dynamic, 1 > array_vec_t
Definition: writer.hpp:50
const double CONSTRAINT_TOLERANCE
This is the tolerance for checking arithmetic bounds in rank and in simplexes.
Definition: writer.hpp:56

     [ Stan Home Page ] © 2011–2016, Stan Development Team.