Stan  1.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 
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  }
54 
58  ~writer() { }
59 
66  std::vector<T>& data_r() {
67  return data_r_;
68  }
69 
70 
77  std::vector<int>& data_i() {
78  return data_i_;
79  }
80 
86  void integer(int n) {
87  data_i_.push_back(n);
88  }
89 
97  void scalar_unconstrain(T& y) {
98  data_r_.push_back(y);
99  }
100 
113  if (y < 0.0)
114  BOOST_THROW_EXCEPTION(std::runtime_error ("y is negative"));
115  data_r_.push_back(log(y));
116  }
117 
129  void scalar_lb_unconstrain(double lb, T& y) {
130  if (y < lb)
131  BOOST_THROW_EXCEPTION(std::runtime_error ("y is lower than the lower bound"));
132  data_r_.push_back(log(y - lb));
133  }
134 
145  void scalar_ub_unconstrain(double ub, T& y) {
146  if (y > ub)
147  BOOST_THROW_EXCEPTION(std::runtime_error ("y is higher than the lower bound"));
148  data_r_.push_back(log(ub - y));
149  }
150 
163  void scalar_lub_unconstrain(double lb, double ub, T& y) {
164  if (y < lb || y > ub)
165  BOOST_THROW_EXCEPTION(std::runtime_error ("y is not between the lower and upper bounds"));
166  data_r_.push_back(stan::math::logit((y - lb) / (ub - lb)));
167  }
168 
179  void corr_unconstrain(T& y) {
180  if (y > 1.0 || y < -1.0)
181  BOOST_THROW_EXCEPTION(std::runtime_error ("y is not between -1.0 and 1.0"));
182  data_r_.push_back(atanh(y));
183  }
184 
196  void prob_unconstrain(T& y) {
197  if (y > 1.0 || y < 0.0)
198  BOOST_THROW_EXCEPTION(std::runtime_error ("y is not between 0.0 and 1.0"));
199  data_r_.push_back(stan::math::logit(y));
200  }
201 
218  if (y.size() == 0) return;
219  stan::math::check_ordered("stan::io::ordered_unconstrain(%1%)", y, "Vector");
220  data_r_.push_back(y[0]);
221  for (typename vector_t::size_type i = 1; i < y.size(); ++i) {
222  data_r_.push_back(log(y[i] - y[i-1]));
223  }
224  }
225 
242  if (y.size() == 0) return;
243  stan::math::check_positive_ordered("stan::io::positive_ordered_unconstrain(%1%)", y, "Vector");
244  data_r_.push_back(log(y[0]));
245  for (typename vector_t::size_type 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  for (typename vector_t::size_type i = 0; i < y.size(); ++i)
258  data_r_.push_back(y[i]);
259  }
260 
267  for (typename vector_t::size_type i = 0; i < y.size(); ++i)
268  data_r_.push_back(y[i]);
269  }
270 
276  void matrix_unconstrain(const matrix_t& y) {
277  for (typename matrix_t::size_type i = 0; i < y.rows(); ++i)
278  for (typename matrix_t::size_type j = 0; j < y.cols(); ++j)
279  data_r_.push_back(y(i,j));
280  }
281 
282  void vector_lb_unconstrain(double lb, vector_t& y) {
283  for (int i = 0; i < y.size(); ++i)
284  scalar_lb_unconstrain(lb,y(i));
285  }
287  for (int i = 0; i < y.size(); ++i)
288  scalar_lb_unconstrain(lb,y(i));
289  }
290  void matrix_lb_unconstrain(double lb, matrix_t& y) {
291  for (int i = 0; i < y.rows(); ++i)
292  for (int j = 0; j < y.cols(); ++j)
293  scalar_lb_unconstrain(lb,y(i,j));
294  }
295 
296  void vector_ub_unconstrain(double ub, vector_t& y) {
297  for (int i = 0; i < y.size(); ++i)
298  scalar_ub_unconstrain(ub,y(i));
299  }
301  for (int i = 0; i < y.size(); ++i)
302  scalar_ub_unconstrain(ub,y(i));
303  }
304  void matrix_ub_unconstrain(double ub, matrix_t& y) {
305  for (int i = 0; i < y.rows(); ++i)
306  for (int j = 0; j < y.cols(); ++j)
307  scalar_ub_unconstrain(ub,y(i,j));
308  }
309 
310 
311  void vector_lub_unconstrain(double lb, double ub, vector_t& y) {
312  for (int i = 0; i < y.size(); ++i)
313  scalar_lub_unconstrain(lb,ub,y(i));
314  }
315  void row_vector_lub_unconstrain(double lb, double ub, row_vector_t& y) {
316  for (int i = 0; i < y.size(); ++i)
317  scalar_lub_unconstrain(lb,ub,y(i));
318  }
319  void matrix_lub_unconstrain(double lb, double ub, matrix_t& y) {
320  for (int i = 0; i < y.rows(); ++i)
321  for (int j = 0; j < y.cols(); ++j)
322  scalar_lub_unconstrain(lb,ub,y(i,j));
323  }
324 
325 
326 
343  stan::math::check_simplex("stan::io::simplex_unconstrain(%1%)", y, "Vector");
344  typename vector_t::size_type k_minus_1 = y.size() - 1;
345  double log_y_k = log(y[k_minus_1]);
346  for (typename vector_t::size_type i = 0; i < k_minus_1; ++i) {
347  data_r_.push_back(log(y[i]) - log_y_k);
348  }
349  }
350 
367  stan::math::check_corr_matrix("stan::io::corr_matrix_unconstrain(%1%)", y, "Matrix");
368  size_t k = y.rows();
369  size_t k_choose_2 = (k * (k-1)) / 2;
370  array_vec_t cpcs(k_choose_2);
371  array_vec_t sds(k);
372  bool successful = stan::prob::factor_cov_matrix(cpcs,sds,y);
373  if (!successful)
374  BOOST_THROW_EXCEPTION(std::runtime_error ("y cannot be factorized by factor_cov_matrix"));
375  for (size_t i = 0; i < k; ++i) {
376  // sds on log scale unconstrained
377  if (fabs(sds[i] - 0.0) >= CONSTRAINT_TOLERANCE)
378  BOOST_THROW_EXCEPTION(std::runtime_error ("sds on log scale are unconstrained"));
379  }
380  for (size_t i = 0; i < k_choose_2; ++i)
381  data_r_.push_back(cpcs[i]);
382  }
383 
396  typename matrix_t::size_type k = y.rows();
397  if (k == 0 || y.cols() != k)
398  BOOST_THROW_EXCEPTION(
399  std::runtime_error ("y must have elements and y must be a square matrix"));
400  typename matrix_t::size_type k_choose_2 = (k * (k-1)) / 2;
401  array_vec_t cpcs(k_choose_2);
402  array_vec_t sds(k);
403  bool successful = stan::prob::factor_cov_matrix(cpcs,sds,y);
404  if(!successful)
405  BOOST_THROW_EXCEPTION(std::runtime_error ("factor_cov_matrix failed"));
406  for (typename matrix_t::size_type i = 0; i < k_choose_2; ++i)
407  data_r_.push_back(cpcs[i]);
408  for (typename matrix_t::size_type i = 0; i < k; ++i)
409  data_r_.push_back(sds[i]);
410  }
411  };
412  }
413 
414 }
415 
416 #endif
internal::traits< Derived >::Index size_type
A stream-based writer for integer, scalar, vector, matrix and array data types, which transforms from...
Definition: writer.hpp:23
void corr_unconstrain(T &y)
Write the unconstrained value corresponding to the specified correlation-constrained variable.
Definition: writer.hpp:179
const double CONSTRAINT_TOLERANCE
This is the tolerance for checking arithmetic bounds in rank and in simplexes.
Definition: writer.hpp:39
void row_vector_lub_unconstrain(double lb, double ub, row_vector_t &y)
Definition: writer.hpp:315
void scalar_pos_unconstrain(T &y)
Write the unconstrained value corresponding to the specified positive-constrained scalar.
Definition: writer.hpp:112
void matrix_ub_unconstrain(double ub, matrix_t &y)
Definition: writer.hpp:304
void matrix_lub_unconstrain(double lb, double ub, matrix_t &y)
Definition: writer.hpp:319
void matrix_unconstrain(const matrix_t &y)
Write the specified unconstrained matrix.
Definition: writer.hpp:276
void scalar_unconstrain(T &y)
Write the unconstrained value corresponding to the specified scalar.
Definition: writer.hpp:97
void row_vector_lb_unconstrain(double lb, row_vector_t &y)
Definition: writer.hpp:286
void vector_lub_unconstrain(double lb, double ub, vector_t &y)
Definition: writer.hpp:311
void cov_matrix_unconstrain(matrix_t &y)
Writes the unconstrained covariance matrix corresponding to the specified constrained correlation mat...
Definition: writer.hpp:395
void prob_unconstrain(T &y)
Write the unconstrained value corresponding to the specified probability value.
Definition: writer.hpp:196
void vector_ub_unconstrain(double ub, vector_t &y)
Definition: writer.hpp:296
Eigen::Matrix< T, Eigen::Dynamic, 1 > vector_t
Definition: writer.hpp:30
Eigen::Matrix< T, 1, Eigen::Dynamic > row_vector_t
Definition: writer.hpp:31
void integer(int n)
Write the specified integer to the sequence of integer values.
Definition: writer.hpp:86
void vector_unconstrain(const vector_t &y)
Write the specified unconstrained vector.
Definition: writer.hpp:256
std::vector< int > & data_i()
Return a reference to the underlying vector of integer values that have been written.
Definition: writer.hpp:77
void matrix_lb_unconstrain(double lb, matrix_t &y)
Definition: writer.hpp:290
void scalar_ub_unconstrain(double ub, T &y)
Write the unconstrained value corresponding to the specified lower-bounded value.
Definition: writer.hpp:145
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:48
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:129
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
Eigen::Array< T, Eigen::Dynamic, 1 > array_vec_t
Definition: writer.hpp:33
~writer()
Destroy this writer.
Definition: writer.hpp:58
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > matrix_t
Definition: writer.hpp:29
void row_vector_unconstrain(const vector_t &y)
Write the specified unconstrained vector.
Definition: writer.hpp:266
void vector_lb_unconstrain(double lb, vector_t &y)
Definition: writer.hpp:282
void positive_ordered_unconstrain(vector_t &y)
Write the unconstrained vector that corresponds to the specified postiive ascendingly ordered vector.
Definition: writer.hpp:241
void simplex_unconstrain(vector_t &y)
Write the unconstrained vector corresponding to the specified simplex value.
Definition: writer.hpp:342
void corr_matrix_unconstrain(matrix_t &y)
Writes the unconstrained correlation matrix corresponding to the specified constrained correlation ma...
Definition: writer.hpp:366
void ordered_unconstrain(vector_t &y)
Write the unconstrained vector that corresponds to the specified ascendingly ordered vector.
Definition: writer.hpp:217
std::vector< T > & data_r()
Return a reference to the underlying vector of real values that have been written.
Definition: writer.hpp:66
void row_vector_ub_unconstrain(double ub, row_vector_t &y)
Definition: writer.hpp:300
var fabs(const var &a)
Return the absolute value of the variable (cmath).
Definition: agrad.hpp:2023
var log(const var &a)
Return the natural log of the specified variable (cmath).
Definition: agrad.hpp:1730
var atanh(const stan::agrad::var &a)
The inverse hyperbolic tangent function for variables (C99).
bool check_corr_matrix(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const char *name, T_result *result, const Policy &)
Return true if the specified matrix is a valid correlation matrix.
bool check_simplex(const char *function, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta, const char *name, T_result *result, const Policy &)
Return true if the specified vector is simplex.
boost::math::tools::promote_args< T >::type logit(T a)
Returns the logit function applied to the argument.
bool check_ordered(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, 1 > &y, const char *name, T_result *result, const Policy &)
Return true if the specified vector is sorted into increasing order.
bool check_positive_ordered(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, 1 > &y, const char *name, T_result *result, const Policy &)
Return true if the specified vector contains only non-negative values and is sorted into increasing o...
const double E
The base of the natural logarithm, .
Definition: constants.hpp:14
bool factor_cov_matrix(Eigen::Array< T, Eigen::Dynamic, 1 > &CPCs, Eigen::Array< T, Eigen::Dynamic, 1 > &sds, const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &Sigma)
This function is intended to make starting values, given a covariance matrix Sigma.
Definition: transform.hpp:42
Probability, optimization and sampling library.
Definition: agrad.cpp:6

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