Stan  2.14.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/mat.hpp>
5 #include <stdexcept>
6 #include <vector>
7 
8 namespace stan {
9 
10  namespace io {
11 
24  template <typename T>
25  class writer {
26  private:
27  std::vector<T> data_r_;
28  std::vector<int> data_i_;
29 
30  public:
31  typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrix_t;
32  typedef Eigen::Matrix<T, Eigen::Dynamic, 1> vector_t;
33  typedef Eigen::Matrix<T, 1, Eigen::Dynamic> row_vector_t;
34 
35  typedef Eigen::Array<T, Eigen::Dynamic, 1> array_vec_t;
36 
41  const double CONSTRAINT_TOLERANCE;
42 
50  writer(std::vector<T>& data_r,
51  std::vector<int>& data_i)
52  : data_r_(data_r),
53  data_i_(data_i),
54  CONSTRAINT_TOLERANCE(1E-8) {
55  data_r_.clear();
56  data_i_.clear();
57  }
58 
62  ~writer() { }
63 
70  std::vector<T>& data_r() {
71  return data_r_;
72  }
73 
74 
81  std::vector<int>& data_i() {
82  return data_i_;
83  }
84 
90  void integer(int n) {
91  data_i_.push_back(n);
92  }
93 
101  void scalar_unconstrain(T& y) {
102  data_r_.push_back(y);
103  }
104 
117  if (y < 0.0)
118  BOOST_THROW_EXCEPTION(std::runtime_error("y is negative"));
119  data_r_.push_back(log(y));
120  }
121 
133  void scalar_lb_unconstrain(double lb, T& y) {
134  data_r_.push_back(stan::math::lb_free(y, lb));
135  }
136 
147  void scalar_ub_unconstrain(double ub, T& y) {
148  data_r_.push_back(stan::math::ub_free(y, ub));
149  }
150 
163  void scalar_lub_unconstrain(double lb, double ub, T& y) {
164  data_r_.push_back(stan::math::lub_free(y, lb, ub));
165  }
166 
177  void corr_unconstrain(T& y) {
178  data_r_.push_back(stan::math::corr_free(y));
179  }
180 
192  void prob_unconstrain(T& y) {
193  data_r_.push_back(stan::math::prob_free(y));
194  }
195 
211  void ordered_unconstrain(vector_t& y) {
212  typedef typename stan::math::index_type<vector_t>::type idx_t;
213  if (y.size() == 0) return;
214  stan::math::check_ordered("stan::io::ordered_unconstrain", "Vector", y);
215  data_r_.push_back(y[0]);
216  for (idx_t i = 1; i < y.size(); ++i) {
217  data_r_.push_back(log(y[i] - y[i-1]));
218  }
219  }
220 
237  void positive_ordered_unconstrain(vector_t& y) {
238  typedef typename stan::math::index_type<vector_t>::type idx_t;
239 
240  // reimplements pos_ordered_free in prob to avoid malloc
241  if (y.size() == 0) return;
242  stan::math::check_positive_ordered
243  ("stan::io::positive_ordered_unconstrain", "Vector", y);
244  data_r_.push_back(log(y[0]));
245  for (idx_t i = 1; i < y.size(); ++i) {
246  data_r_.push_back(log(y[i] - y[i-1]));
247  }
248  }
249 
250 
256  void vector_unconstrain(const vector_t& y) {
257  typedef typename stan::math::index_type<vector_t>::type idx_t;
258  for (idx_t i = 0; i < y.size(); ++i)
259  data_r_.push_back(y[i]);
260  }
261 
267  void row_vector_unconstrain(const vector_t& y) {
268  typedef typename stan::math::index_type<vector_t>::type idx_t;
269  for (idx_t i = 0; i < y.size(); ++i)
270  data_r_.push_back(y[i]);
271  }
272 
278  void matrix_unconstrain(const matrix_t& y) {
279  typedef typename stan::math::index_type<matrix_t>::type idx_t;
280  for (idx_t j = 0; j < y.cols(); ++j)
281  for (idx_t i = 0; i < y.rows(); ++i)
282  data_r_.push_back(y(i, j));
283  }
284 
285  void vector_lb_unconstrain(double lb, vector_t& y) {
286  typedef typename stan::math::index_type<vector_t>::type idx_t;
287  for (idx_t i = 0; i < y.size(); ++i)
288  scalar_lb_unconstrain(lb, y(i));
289  }
290  void row_vector_lb_unconstrain(double lb, row_vector_t& y) {
291  typedef typename stan::math::index_type<row_vector_t>::type idx_t;
292  for (idx_t i = 0; i < y.size(); ++i)
293  scalar_lb_unconstrain(lb, y(i));
294  }
295  void matrix_lb_unconstrain(double lb, matrix_t& y) {
296  typedef typename stan::math::index_type<matrix_t>::type idx_t;
297  for (idx_t j = 0; j < y.cols(); ++j)
298  for (idx_t i = 0; i < y.rows(); ++i)
299  scalar_lb_unconstrain(lb, y(i, j));
300  }
301 
302  void vector_ub_unconstrain(double ub, vector_t& y) {
303  typedef typename stan::math::index_type<vector_t>::type idx_t;
304  for (idx_t i = 0; i < y.size(); ++i)
305  scalar_ub_unconstrain(ub, y(i));
306  }
307  void row_vector_ub_unconstrain(double ub, row_vector_t& y) {
308  typedef typename stan::math::index_type<row_vector_t>::type idx_t;
309  for (idx_t i = 0; i < y.size(); ++i)
310  scalar_ub_unconstrain(ub, y(i));
311  }
312  void matrix_ub_unconstrain(double ub, matrix_t& y) {
313  typedef typename stan::math::index_type<matrix_t>::type idx_t;
314  for (idx_t j = 0; j < y.cols(); ++j)
315  for (idx_t i = 0; i < y.rows(); ++i)
316  scalar_ub_unconstrain(ub, y(i, j));
317  }
318 
319 
320  void vector_lub_unconstrain(double lb, double ub, vector_t& y) {
321  typedef typename stan::math::index_type<vector_t>::type idx_t;
322  for (idx_t i = 0; i < y.size(); ++i)
323  scalar_lub_unconstrain(lb, ub, y(i));
324  }
325  void row_vector_lub_unconstrain(double lb, double ub, row_vector_t& y) {
326  typedef typename stan::math::index_type<row_vector_t>::type idx_t;
327  for (idx_t i = 0; i < y.size(); ++i)
328  scalar_lub_unconstrain(lb, ub, y(i));
329  }
330  void matrix_lub_unconstrain(double lb, double ub, matrix_t& y) {
331  typedef typename stan::math::index_type<matrix_t>::type idx_t;
332  for (idx_t j = 0; j < y.cols(); ++j)
333  for (idx_t i = 0; i < y.rows(); ++i)
334  scalar_lub_unconstrain(lb, ub, y(i, j));
335  }
336 
337 
338 
354  void unit_vector_unconstrain(vector_t& y) {
355  stan::math::check_unit_vector("stan::io::unit_vector_unconstrain",
356  "Vector", y);
357  typedef typename stan::math::index_type<vector_t>::type idx_t;
358  vector_t uy = stan::math::unit_vector_free(y);
359  for (idx_t i = 0; i < uy.size(); ++i)
360  data_r_.push_back(uy[i]);
361  }
362 
363 
378  void simplex_unconstrain(vector_t& y) {
379  typedef typename stan::math::index_type<vector_t>::type idx_t;
380 
381  stan::math::check_simplex("stan::io::simplex_unconstrain", "Vector", y);
382  vector_t uy = stan::math::simplex_free(y);
383  for (idx_t i = 0; i < uy.size(); ++i)
384  data_r_.push_back(uy[i]);
385  }
386 
398  void cholesky_factor_unconstrain(matrix_t& y) {
399  typedef typename stan::math::index_type<matrix_t>::type idx_t;
400 
401  // FIXME: optimize by unrolling cholesky_factor_free
402  Eigen::Matrix<T, Eigen::Dynamic, 1> y_free
403  = stan::math::cholesky_factor_free(y);
404  for (idx_t i = 0; i < y_free.size(); ++i)
405  data_r_.push_back(y_free[i]);
406  }
407 
408 
420  void cholesky_corr_unconstrain(matrix_t& y) {
421  typedef typename stan::math::index_type<matrix_t>::type idx_t;
422 
423  // FIXME: optimize by unrolling cholesky_factor_free
424  Eigen::Matrix<T, Eigen::Dynamic, 1> y_free
425  = stan::math::cholesky_corr_free(y);
426  for (idx_t i = 0; i < y_free.size(); ++i)
427  data_r_.push_back(y_free[i]);
428  }
429 
430 
442  void cov_matrix_unconstrain(matrix_t& y) {
443  typedef typename stan::math::index_type<matrix_t>::type idx_t;
444  idx_t k = y.rows();
445  if (k == 0 || y.cols() != k)
446  BOOST_THROW_EXCEPTION(
447  std::runtime_error("y must have elements and"
448  " y must be a square matrix"));
449  idx_t k_choose_2 = (k * (k-1)) / 2;
450  array_vec_t cpcs(k_choose_2);
451  array_vec_t sds(k);
452  bool successful = stan::math::factor_cov_matrix(y, cpcs, sds);
453  if (!successful)
454  BOOST_THROW_EXCEPTION
455  (std::runtime_error("factor_cov_matrix failed"));
456  for (idx_t i = 0; i < k_choose_2; ++i)
457  data_r_.push_back(cpcs[i]);
458  for (idx_t i = 0; i < k; ++i)
459  data_r_.push_back(sds[i]);
460  }
461 
477  void corr_matrix_unconstrain(matrix_t& y) {
478  typedef typename stan::math::index_type<matrix_t>::type idx_t;
479 
480  stan::math::check_corr_matrix("stan::io::corr_matrix_unconstrain",
481  "Matrix", y);
482  idx_t k = y.rows();
483  idx_t k_choose_2 = (k * (k-1)) / 2;
484  array_vec_t cpcs(k_choose_2);
485  array_vec_t sds(k);
486  bool successful = stan::math::factor_cov_matrix(y, cpcs, sds);
487  if (!successful)
488  BOOST_THROW_EXCEPTION
489  (std::runtime_error("y cannot be factorized by factor_cov_matrix"));
490  for (idx_t i = 0; i < k; ++i) {
491  // sds on log scale unconstrained
492  if (fabs(sds[i] - 0.0) >= CONSTRAINT_TOLERANCE)
493  BOOST_THROW_EXCEPTION
494  (std::runtime_error("sds on log scale are unconstrained"));
495  }
496  for (idx_t i = 0; i < k_choose_2; ++i)
497  data_r_.push_back(cpcs[i]);
498  }
499  };
500  }
501 
502 }
503 
504 #endif
void corr_matrix_unconstrain(matrix_t &y)
Writes the unconstrained correlation matrix corresponding to the specified constrained correlation ma...
Definition: writer.hpp:477
void row_vector_ub_unconstrain(double ub, row_vector_t &y)
Definition: writer.hpp:307
void matrix_unconstrain(const matrix_t &y)
Write the specified unconstrained matrix.
Definition: writer.hpp:278
Probability, optimization and sampling library.
void integer(int n)
Write the specified integer to the sequence of integer values.
Definition: writer.hpp:90
std::vector< T > & data_r()
Return a reference to the underlying vector of real values that have been written.
Definition: writer.hpp:70
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:163
void cholesky_factor_unconstrain(matrix_t &y)
Writes the unconstrained Cholesky factor corresponding to the specified constrained matrix...
Definition: writer.hpp:398
void positive_ordered_unconstrain(vector_t &y)
Write the unconstrained vector that corresponds to the specified postiive ascendingly ordered vector...
Definition: writer.hpp:237
void unit_vector_unconstrain(vector_t &y)
Write the unconstrained vector corresponding to the specified unit_vector value.
Definition: writer.hpp:354
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:133
void scalar_unconstrain(T &y)
Write the unconstrained value corresponding to the specified scalar.
Definition: writer.hpp:101
void row_vector_unconstrain(const vector_t &y)
Write the specified unconstrained vector.
Definition: writer.hpp:267
void simplex_unconstrain(vector_t &y)
Write the unconstrained vector corresponding to the specified simplex value.
Definition: writer.hpp:378
void row_vector_lub_unconstrain(double lb, double ub, row_vector_t &y)
Definition: writer.hpp:325
void vector_ub_unconstrain(double ub, vector_t &y)
Definition: writer.hpp:302
void matrix_lub_unconstrain(double lb, double ub, matrix_t &y)
Definition: writer.hpp:330
void vector_lb_unconstrain(double lb, vector_t &y)
Definition: writer.hpp:285
void cholesky_corr_unconstrain(matrix_t &y)
Writes the unconstrained Cholesky factor for a correlation matrix corresponding to the specified cons...
Definition: writer.hpp:420
void cov_matrix_unconstrain(matrix_t &y)
Writes the unconstrained covariance matrix corresponding to the specified constrained correlation mat...
Definition: writer.hpp:442
void vector_lub_unconstrain(double lb, double ub, vector_t &y)
Definition: writer.hpp:320
void matrix_ub_unconstrain(double ub, matrix_t &y)
Definition: writer.hpp:312
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > matrix_t
Definition: writer.hpp:31
void matrix_lb_unconstrain(double lb, matrix_t &y)
Definition: writer.hpp:295
Eigen::Matrix< T, Eigen::Dynamic, 1 > vector_t
Definition: writer.hpp:32
Eigen::Matrix< T, 1, Eigen::Dynamic > row_vector_t
Definition: writer.hpp:33
std::vector< int > & data_i()
Return a reference to the underlying vector of integer values that have been written.
Definition: writer.hpp:81
void scalar_pos_unconstrain(T &y)
Write the unconstrained value corresponding to the specified positive-constrained scalar...
Definition: writer.hpp:116
~writer()
Destroy this writer.
Definition: writer.hpp:62
void vector_unconstrain(const vector_t &y)
Write the specified unconstrained vector.
Definition: writer.hpp:256
void prob_unconstrain(T &y)
Write the unconstrained value corresponding to the specified probability value.
Definition: writer.hpp:192
void corr_unconstrain(T &y)
Write the unconstrained value corresponding to the specified correlation-constrained variable...
Definition: writer.hpp:177
void ordered_unconstrain(vector_t &y)
Write the unconstrained vector that corresponds to the specified ascendingly ordered vector...
Definition: writer.hpp:211
void row_vector_lb_unconstrain(double lb, row_vector_t &y)
Definition: writer.hpp:290
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:50
void scalar_ub_unconstrain(double ub, T &y)
Write the unconstrained value corresponding to the specified lower-bounded value. ...
Definition: writer.hpp:147
A stream-based writer for integer, scalar, vector, matrix and array data types, which transforms from...
Definition: writer.hpp:25
Eigen::Array< T, Eigen::Dynamic, 1 > array_vec_t
Definition: writer.hpp:35
const double CONSTRAINT_TOLERANCE
This is the tolerance for checking arithmetic bounds in rank and in simplexes.
Definition: writer.hpp:41

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