Stan  1.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
writer.hpp
Go to the documentation of this file.
1 #ifndef __STAN__IO__WRITER_HPP__
2 #define __STAN__IO__WRITER_HPP__
3 
5 
6 namespace stan {
7 
8  namespace io {
9 
22  template <typename T>
23  class writer {
24  private:
25  std::vector<T> data_r_;
26  std::vector<int> data_i_;
27  public:
28 
29  typedef Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> matrix_t;
30  typedef Eigen::Matrix<T,Eigen::Dynamic,1> vector_t;
31  typedef Eigen::Matrix<T,1,Eigen::Dynamic> row_vector_t;
32 
33  typedef Eigen::Array<T,Eigen::Dynamic,1> array_vec_t;
34 
39  const double CONSTRAINT_TOLERANCE;
40 
48  writer(std::vector<T>& data_r,
49  std::vector<int>& data_i)
50  : data_r_(data_r),
51  data_i_(data_i),
53  data_r_.clear();
54  data_i_.clear();
55  }
56 
60  ~writer() { }
61 
68  std::vector<T>& data_r() {
69  return data_r_;
70  }
71 
72 
79  std::vector<int>& data_i() {
80  return data_i_;
81  }
82 
88  void integer(int n) {
89  data_i_.push_back(n);
90  }
91 
99  void scalar_unconstrain(T& y) {
100  data_r_.push_back(y);
101  }
102 
115  if (y < 0.0)
116  BOOST_THROW_EXCEPTION(std::runtime_error ("y is negative"));
117  data_r_.push_back(log(y));
118  }
119 
131  void scalar_lb_unconstrain(double lb, T& y) {
132  if (y < lb)
133  BOOST_THROW_EXCEPTION(std::runtime_error ("y is lower than the lower bound"));
134  data_r_.push_back(log(y - lb));
135  }
136 
147  void scalar_ub_unconstrain(double ub, T& y) {
148  if (y > ub)
149  BOOST_THROW_EXCEPTION(std::runtime_error ("y is higher than the lower bound"));
150  data_r_.push_back(log(ub - y));
151  }
152 
165  void scalar_lub_unconstrain(double lb, double ub, T& y) {
166  if (y < lb || y > ub)
167  BOOST_THROW_EXCEPTION(std::runtime_error ("y is not between the lower and upper bounds"));
168  data_r_.push_back(stan::math::logit((y - lb) / (ub - lb)));
169  }
170 
181  void corr_unconstrain(T& y) {
182  if (y > 1.0 || y < -1.0)
183  BOOST_THROW_EXCEPTION(std::runtime_error ("y is not between -1.0 and 1.0"));
184  data_r_.push_back(atanh(y));
185  }
186 
198  void prob_unconstrain(T& y) {
199  if (y > 1.0 || y < 0.0)
200  BOOST_THROW_EXCEPTION(std::runtime_error ("y is not between 0.0 and 1.0"));
201  data_r_.push_back(stan::math::logit(y));
202  }
203 
220  if (y.size() == 0) return;
221  stan::math::check_ordered("stan::io::ordered_unconstrain(%1%)", y, "Vector");
222  data_r_.push_back(y[0]);
223  for (typename vector_t::size_type i = 1; i < y.size(); ++i) {
224  data_r_.push_back(log(y[i] - y[i-1]));
225  }
226  }
227 
244  if (y.size() == 0) return;
245  stan::math::check_positive_ordered("stan::io::positive_ordered_unconstrain(%1%)", y, "Vector");
246  data_r_.push_back(log(y[0]));
247  for (typename vector_t::size_type i = 1; i < y.size(); ++i) {
248  data_r_.push_back(log(y[i] - y[i-1]));
249  }
250  }
251 
252 
258  void vector_unconstrain(const vector_t& y) {
259  for (typename vector_t::size_type i = 0; i < y.size(); ++i)
260  data_r_.push_back(y[i]);
261  }
262 
269  for (typename vector_t::size_type i = 0; i < y.size(); ++i)
270  data_r_.push_back(y[i]);
271  }
272 
278  void matrix_unconstrain(const matrix_t& y) {
279  for (typename matrix_t::size_type j = 0; j < y.cols(); ++j)
280  for (typename matrix_t::size_type i = 0; i < y.rows(); ++i)
281  data_r_.push_back(y(i,j));
282  }
283 
284  void vector_lb_unconstrain(double lb, vector_t& y) {
285  for (int i = 0; i < y.size(); ++i)
286  scalar_lb_unconstrain(lb,y(i));
287  }
289  for (int i = 0; i < y.size(); ++i)
290  scalar_lb_unconstrain(lb,y(i));
291  }
292  void matrix_lb_unconstrain(double lb, matrix_t& y) {
293  for (typename matrix_t::size_type j = 0; j < y.cols(); ++j)
294  for (typename matrix_t::size_type i = 0; i < y.rows(); ++i)
295  scalar_lb_unconstrain(lb,y(i,j));
296  }
297 
298  void vector_ub_unconstrain(double ub, vector_t& y) {
299  for (int i = 0; i < y.size(); ++i)
300  scalar_ub_unconstrain(ub,y(i));
301  }
303  for (int i = 0; i < y.size(); ++i)
304  scalar_ub_unconstrain(ub,y(i));
305  }
306  void matrix_ub_unconstrain(double ub, matrix_t& y) {
307  for (typename matrix_t::size_type j = 0; j < y.cols(); ++j)
308  for (typename matrix_t::size_type i = 0; i < y.rows(); ++i)
309  scalar_ub_unconstrain(ub,y(i,j));
310  }
311 
312 
313  void vector_lub_unconstrain(double lb, double ub, vector_t& y) {
314  for (int i = 0; i < y.size(); ++i)
315  scalar_lub_unconstrain(lb,ub,y(i));
316  }
317  void row_vector_lub_unconstrain(double lb, double ub, row_vector_t& y) {
318  for (int i = 0; i < y.size(); ++i)
319  scalar_lub_unconstrain(lb,ub,y(i));
320  }
321  void matrix_lub_unconstrain(double lb, double ub, matrix_t& y) {
322  for (typename matrix_t::size_type j = 0; j < y.cols(); ++j)
323  for (typename matrix_t::size_type i = 0; i < y.rows(); ++i)
324  scalar_lub_unconstrain(lb,ub,y(i,j));
325  }
326 
327 
328 
344  stan::math::check_unit_vector("stan::io::unit_vector_unconstrain(%1%)", y, "Vector");
346  for (typename vector_t::size_type i = 0; i < uy.size(); ++i)
347  data_r_.push_back(uy[i]);
348  }
349 
350 
366  stan::math::check_simplex("stan::io::simplex_unconstrain(%1%)", y, "Vector");
368  for (typename vector_t::size_type i = 0; i < uy.size(); ++i)
369  data_r_.push_back(uy[i]);
370  }
371 
388  stan::math::check_corr_matrix("stan::io::corr_matrix_unconstrain(%1%)", y, "Matrix");
389  size_t k = y.rows();
390  size_t k_choose_2 = (k * (k-1)) / 2;
391  array_vec_t cpcs(k_choose_2);
392  array_vec_t sds(k);
393  bool successful = stan::prob::factor_cov_matrix(cpcs,sds,y);
394  if (!successful)
395  BOOST_THROW_EXCEPTION(std::runtime_error ("y cannot be factorized by factor_cov_matrix"));
396  for (size_t i = 0; i < k; ++i) {
397  // sds on log scale unconstrained
398  if (fabs(sds[i] - 0.0) >= CONSTRAINT_TOLERANCE)
399  BOOST_THROW_EXCEPTION(std::runtime_error ("sds on log scale are unconstrained"));
400  }
401  for (size_t i = 0; i < k_choose_2; ++i)
402  data_r_.push_back(cpcs[i]);
403  }
404 
417  typename matrix_t::size_type k = y.rows();
418  if (k == 0 || y.cols() != k)
419  BOOST_THROW_EXCEPTION(
420  std::runtime_error ("y must have elements and y must be a square matrix"));
421  typename matrix_t::size_type k_choose_2 = (k * (k-1)) / 2;
422  array_vec_t cpcs(k_choose_2);
423  array_vec_t sds(k);
424  bool successful = stan::prob::factor_cov_matrix(cpcs,sds,y);
425  if(!successful)
426  BOOST_THROW_EXCEPTION(std::runtime_error ("factor_cov_matrix failed"));
427  for (typename matrix_t::size_type i = 0; i < k_choose_2; ++i)
428  data_r_.push_back(cpcs[i]);
429  for (typename matrix_t::size_type i = 0; i < k; ++i)
430  data_r_.push_back(sds[i]);
431  }
432  };
433  }
434 
435 }
436 
437 #endif

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