Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
stan::io::random_var_context Class Reference

#include "stan/io/random_var_context.hpp"

Inheritance diagram for stan::io::random_var_context:
stan::io::var_context

Public Member Functions

template<class Model , class RNG >
 random_var_context (Model &model, RNG &rng, double init_radius, bool init_zero)
 
 ~random_var_context ()
 
bool contains_r (const std::string &name) const
 
std::vector< double > vals_r (const std::string &name) const
 
std::vector< size_t > dims_r (const std::string &name) const
 
bool contains_i (const std::string &name) const
 
std::vector< intvals_i (const std::string &name) const
 
std::vector< size_t > dims_i (const std::string &name) const
 
void names_r (std::vector< std::string > &names) const
 
void names_i (std::vector< std::string > &names) const
 
std::vector< double > get_unconstrained () const
 
void add_vec (std::stringstream &msg, const std::vector< size_t > &dims) const
 
void validate_dims (const std::string &stage, const std::string &name, const std::string &base_type, const std::vector< size_t > &dims_declared) const
 

Static Public Member Functions

static std::vector< size_t > to_vec ()
 
static std::vector< size_t > to_vec (size_t n1)
 
static std::vector< size_t > to_vec (size_t n1, size_t n2)
 
static std::vector< size_t > to_vec (size_t n1, size_t n2, size_t n3)
 
static std::vector< size_t > to_vec (size_t n1, size_t n2, size_t n3, size_t n4)
 
static std::vector< size_t > to_vec (size_t n1, size_t n2, size_t n3, size_t n4, size_t n5)
 
static std::vector< size_t > to_vec (size_t n1, size_t n2, size_t n3, size_t n4, size_t n5, size_t n6)
 
static std::vector< size_t > to_vec (size_t n1, size_t n2, size_t n3, size_t n4, size_t n5, size_t n6, size_t n7)
 
static std::vector< size_t > to_vec (size_t n1, size_t n2, size_t n3, size_t n4, size_t n5, size_t n6, size_t n7, size_t n8)
 

Private Member Functions

size_t dim_size (const std::vector< size_t > &dim)
 
std::vector< std::vector< double > > constrained_to_vals_r (const std::vector< double > &constrained, const std::vector< std::vector< size_t > > &dims)
 

Private Attributes

std::vector< std::string > names_
 
std::vector< std::vector< size_t > > dims_
 
std::vector< double > unconstrained_params_
 
std::vector< std::vector< double > > vals_r_
 

Detailed Description

This is an implementation of a var_context that initializes the unconstrained values randomly. This is used for initialization.

Definition at line 18 of file random_var_context.hpp.

Constructor & Destructor Documentation

template<class Model , class RNG >
stan::io::random_var_context::random_var_context ( Model model,
RNG &  rng,
double  init_radius,
bool  init_zero 
)
inline

Constructs a random var_context.

On construction, this var_context will generate random numbers on the unconstrained scale for the model provided. Once generated, the class is immutable.

This class only generates values for the parameters in the Stan program and does not generate values for transformed parameters or generated quantities.

Template Parameters
ModelModel class
RNGRandom number generator type
Parameters
[in]modelinstantiated model to generate variables for
[in,out]rngpseudo-random number generator
[in]init_radiusthe unconstrained variables are uniform draws from -init_radius to init_radius.
[in]init_zeroindicates whether all unconstrained variables should be initialized at 0. When init_zero is false, init_radius must be greater than 0.

Definition at line 42 of file random_var_context.hpp.

References constrained_to_vals_r(), dims_, MECModelEnuComparisons::i, sanity_check_grl::keep, getGoodRuns4SAM::n, names_, num, gen_flatrecord::size, unconstrained_params_, and vals_r_.

46  : unconstrained_params_(model.num_params_r()) {
47  size_t num_unconstrained_ = model.num_params_r();
48  model.get_param_names(names_);
49  model.get_dims(dims_);
50 
51  // cutting names_ and dims_ down to just the constrained parameters
52  std::vector<std::string> constrained_params_names;
53  model.constrained_param_names(constrained_params_names, false, false);
54  size_t keep = constrained_params_names.size();
55  size_t i = 0;
56  size_t num = 0;
57  for (i = 0; i < dims_.size(); ++i) {
58  size_t size = 1;
59  for (size_t n = 0; n < dims_[i].size(); ++n)
60  size *= dims_[i][n];
61  num += size;
62  if (num > keep)
63  break;
64  }
65  dims_.erase(dims_.begin() + i, dims_.end());
66  names_.erase(names_.begin() + i, names_.end());
67 
68  if (init_zero) {
69  for (size_t n = 0; n < num_unconstrained_; ++n)
70  unconstrained_params_[n] = 0.0;
71  } else {
72  boost::random::uniform_real_distribution<double>
73  unif(-init_radius, init_radius);
74  for (size_t n = 0; n < num_unconstrained_; ++n)
75  unconstrained_params_[n] = unif(rng);
76  }
77 
78  std::vector<double> constrained_params;
79  std::vector<int> int_params;
80  model.write_array(rng,
81  unconstrained_params_, int_params,
82  constrained_params,
83  false, false, 0);
84 
85  vals_r_ = constrained_to_vals_r(constrained_params, dims_);
86  }
std::vector< std::string > names_
std::vector< double > unconstrained_params_
std::vector< std::vector< size_t > > dims_
std::vector< std::vector< double > > constrained_to_vals_r(const std::vector< double > &constrained, const std::vector< std::vector< size_t > > &dims)
int num
Definition: f2_nu.C:119
std::vector< std::vector< double > > vals_r_
const XML_Char XML_Content * model
Definition: expat.h:151
stan::io::random_var_context::~random_var_context ( )
inline

Destructor.

Definition at line 91 of file random_var_context.hpp.

91 {}

Member Function Documentation

void stan::io::var_context::add_vec ( std::stringstream &  msg,
const std::vector< size_t > &  dims 
) const
inlineinherited

Definition at line 114 of file var_context.hpp.

References MECModelEnuComparisons::i.

Referenced by stan::io::var_context::validate_dims().

115  {
116  msg << '(';
117  for (size_t i = 0; i < dims.size(); ++i) {
118  if (i > 0) msg << ',';
119  msg << dims[i];
120  }
121  msg << ')';
122  }
std::vector<std::vector<double> > stan::io::random_var_context::constrained_to_vals_r ( const std::vector< double > &  constrained,
const std::vector< std::vector< size_t > > &  dims 
)
inlineprivate

Returns a vector of constrained values in the format expected out of the vals_r() function.

Parameters
[in]constrainedconstrained parameter values
[in]dimsdimensions of each of the parameter values
Returns
constrained values reshaped to be returned in the vals_r function

Definition at line 244 of file random_var_context.hpp.

References dim_size(), fetch_tb_beamline_files::dims, MECModelEnuComparisons::i, gen_flatrecord::size, febshutoff_auto::start, and vals_r().

Referenced by random_var_context().

245  {
246  std::vector<std::vector<double> > vals_r(dims.size());
247 
248  std::vector<double>::const_iterator start = constrained.begin();
249  for (size_t i = 0; i < dims.size(); ++i) {
250  size_t size = dim_size(dims[i]);
251  vals_r[i] = std::vector<double>(start, start + size);
252  start += size;
253  }
254  return vals_r;
255  }
size_t dim_size(const std::vector< size_t > &dim)
std::vector< double > vals_r(const std::string &name) const
bool stan::io::random_var_context::contains_i ( const std::string &  name) const
inlinevirtual

Return true if the specified variable name has integer values. Always returns false.

Parameters
nameName of variable.
Returns
false

Implements stan::io::var_context.

Definition at line 144 of file random_var_context.hpp.

Referenced by TEST_F().

144  {
145  return false;
146  }
bool stan::io::random_var_context::contains_r ( const std::string &  name) const
inlinevirtual

Return true if the specified variable name is defined. Will return true if the name matches a parameter in the model.

Parameters
nameName of variable.
Returns
true if the name is a parameter in the model.

Implements stan::io::var_context.

Definition at line 102 of file random_var_context.hpp.

References names_.

Referenced by TEST_F().

102  {
103  return std::find(names_.begin(), names_.end(), name) != names_.end();
104  }
const XML_Char * name
Definition: expat.h:151
std::vector< std::string > names_
size_t stan::io::random_var_context::dim_size ( const std::vector< size_t > &  dim)
inlineprivate

Computes the size of a variable based on the dim provided.

Parameters
dimdimension of the variable
Returns
total size of the variable

Definition at line 227 of file random_var_context.hpp.

References calib::j, and gen_flatrecord::size.

Referenced by constrained_to_vals_r().

227  {
228  size_t size = 1;
229  for (size_t j = 0; j < dim.size(); ++j)
230  size *= dim[j];
231  return size;
232  }
const double j
Definition: BetheBloch.cxx:29
std::vector<size_t> stan::io::random_var_context::dims_i ( const std::string &  name) const
inlinevirtual

Return the dimensions of the specified floating point variable. Returns an empty vector.

Parameters
nameName of variable.
Returns
empty vector

Implements stan::io::var_context.

Definition at line 166 of file random_var_context.hpp.

Referenced by TEST_F().

166  {
167  std::vector<size_t> empty_dims_i;
168  return empty_dims_i;
169  }
std::vector<size_t> stan::io::random_var_context::dims_r ( const std::string &  name) const
inlinevirtual

Returns the dimensions of the variable

Parameters
nameName of variable.
Returns
the dimensions of the variable if it exists; an empty vector is returned otherwise

Implements stan::io::var_context.

Definition at line 129 of file random_var_context.hpp.

References dims_, PandAna.Demos.demo0::loc, and names_.

Referenced by TEST_F().

129  {
130  std::vector<std::string>::const_iterator loc
131  = std::find(names_.begin(), names_.end(), name);
132  if (loc == names_.end())
133  return std::vector<size_t>();
134  return dims_[loc - names_.begin()];
135  }
const XML_Char * name
Definition: expat.h:151
std::vector< std::string > names_
std::vector< std::vector< size_t > > dims_
std::vector<double> stan::io::random_var_context::get_unconstrained ( ) const
inline

Return the random initialization on the unconstrained scale.

Returns
the unconstrained parameters

Definition at line 197 of file random_var_context.hpp.

References unconstrained_params_.

Referenced by stan::services::util::initialize().

197  {
198  return unconstrained_params_;
199  }
std::vector< double > unconstrained_params_
void stan::io::random_var_context::names_i ( std::vector< std::string > &  names) const
inlinevirtual

Fill a list of the names of the integer variables in the context. This context has no variables.

Parameters
namesVector to store the list of names in.

Implements stan::io::var_context.

Definition at line 188 of file random_var_context.hpp.

Referenced by TEST_F().

188  {
189  names.clear();
190  }
void stan::io::random_var_context::names_r ( std::vector< std::string > &  names) const
inlinevirtual

Fill a list of the names of the floating point variables in the context. This will return the names of the parameters in the model.

Parameters
namesVector to store the list of names in.

Implements stan::io::var_context.

Definition at line 178 of file random_var_context.hpp.

References names_.

Referenced by TEST_F().

178  {
179  names = names_;
180  }
std::vector< std::string > names_
static std::vector<size_t> stan::io::var_context::to_vec ( )
inlinestaticinherited

Definition at line 179 of file var_context.hpp.

Referenced by stan::services::util::read_dense_inv_metric(), stan::services::util::read_diag_inv_metric(), and TEST().

179  {
180  return std::vector<size_t>();
181  }
static std::vector<size_t> stan::io::var_context::to_vec ( size_t  n1)
inlinestaticinherited

Definition at line 182 of file var_context.hpp.

References make_training::n1, and registry_explorer::v.

182  {
183  std::vector<size_t> v(1);
184  v[0] = n1;
185  return v;
186  }
static std::vector<size_t> stan::io::var_context::to_vec ( size_t  n1,
size_t  n2 
)
inlinestaticinherited

Definition at line 187 of file var_context.hpp.

References make_training::n1, and registry_explorer::v.

187  {
188  std::vector<size_t> v(2);
189  v[0] = n1;
190  v[1] = n2;
191  return v;
192  }
static std::vector<size_t> stan::io::var_context::to_vec ( size_t  n1,
size_t  n2,
size_t  n3 
)
inlinestaticinherited

Definition at line 193 of file var_context.hpp.

References make_training::n1, and registry_explorer::v.

194  {
195  std::vector<size_t> v(3);
196  v[0] = n1;
197  v[1] = n2;
198  v[2] = n3;
199  return v;
200  }
static std::vector<size_t> stan::io::var_context::to_vec ( size_t  n1,
size_t  n2,
size_t  n3,
size_t  n4 
)
inlinestaticinherited

Definition at line 201 of file var_context.hpp.

References make_training::n1, and registry_explorer::v.

202  {
203  std::vector<size_t> v(4);
204  v[0] = n1;
205  v[1] = n2;
206  v[2] = n3;
207  v[3] = n4;
208  return v;
209  }
static std::vector<size_t> stan::io::var_context::to_vec ( size_t  n1,
size_t  n2,
size_t  n3,
size_t  n4,
size_t  n5 
)
inlinestaticinherited

Definition at line 210 of file var_context.hpp.

References make_training::n1, and registry_explorer::v.

212  {
213  std::vector<size_t> v(5);
214  v[0] = n1;
215  v[1] = n2;
216  v[2] = n3;
217  v[3] = n4;
218  v[4] = n5;
219  return v;
220  }
static std::vector<size_t> stan::io::var_context::to_vec ( size_t  n1,
size_t  n2,
size_t  n3,
size_t  n4,
size_t  n5,
size_t  n6 
)
inlinestaticinherited

Definition at line 221 of file var_context.hpp.

References make_training::n1, and registry_explorer::v.

223  {
224  std::vector<size_t> v(6);
225  v[0] = n1;
226  v[1] = n2;
227  v[2] = n3;
228  v[3] = n4;
229  v[4] = n5;
230  v[5] = n6;
231  return v;
232  }
static std::vector<size_t> stan::io::var_context::to_vec ( size_t  n1,
size_t  n2,
size_t  n3,
size_t  n4,
size_t  n5,
size_t  n6,
size_t  n7 
)
inlinestaticinherited

Definition at line 233 of file var_context.hpp.

References make_training::n1, and registry_explorer::v.

236  {
237  std::vector<size_t> v(7);
238  v[0] = n1;
239  v[1] = n2;
240  v[2] = n3;
241  v[3] = n4;
242  v[4] = n5;
243  v[5] = n6;
244  v[6] = n7;
245  return v;
246  }
static std::vector<size_t> stan::io::var_context::to_vec ( size_t  n1,
size_t  n2,
size_t  n3,
size_t  n4,
size_t  n5,
size_t  n6,
size_t  n7,
size_t  n8 
)
inlinestaticinherited

Definition at line 247 of file var_context.hpp.

References make_training::n1, and registry_explorer::v.

250  {
251  std::vector<size_t> v(8);
252  v[0] = n1;
253  v[1] = n2;
254  v[2] = n3;
255  v[3] = n4;
256  v[4] = n5;
257  v[5] = n6;
258  v[6] = n7;
259  v[7] = n8;
260  return v;
261  }
void stan::io::var_context::validate_dims ( const std::string &  stage,
const std::string &  name,
const std::string &  base_type,
const std::vector< size_t > &  dims_declared 
) const
inlineinherited

Definition at line 124 of file var_context.hpp.

References stan::io::var_context::add_vec(), stan::io::var_context::contains_i(), stan::io::var_context::contains_r(), fetch_tb_beamline_files::dims, stan::io::var_context::dims_r(), MECModelEnuComparisons::i, and lem_server::msg.

Referenced by stan::services::util::read_dense_inv_metric(), stan::services::util::read_diag_inv_metric(), and TEST().

127  {
128  bool is_int_type = base_type == "int";
129  if (is_int_type) {
130  if (!contains_i(name)) {
131  std::stringstream msg;
132  msg << (contains_r(name)
133  ? "int variable contained non-int values"
134  : "variable does not exist" )
135  << "; processing stage=" << stage
136  << "; variable name=" << name
137  << "; base type=" << base_type;
138  throw std::runtime_error(msg.str());
139  }
140  } else {
141  if (!contains_r(name)) {
142  std::stringstream msg;
143  msg << "variable does not exist"
144  << "; processing stage=" << stage
145  << "; variable name=" << name
146  << "; base type=" << base_type;
147  throw std::runtime_error(msg.str());
148  }
149  }
150  std::vector<size_t> dims = dims_r(name);
151  if (dims.size() != dims_declared.size()) {
152  std::stringstream msg;
153  msg << "mismatch in number dimensions declared and found in context"
154  << "; processing stage=" << stage
155  << "; variable name=" << name
156  << "; dims declared=";
157  add_vec(msg, dims_declared);
158  msg << "; dims found=";
159  add_vec(msg, dims);
160  throw std::runtime_error(msg.str());
161  }
162  for (size_t i = 0; i < dims.size(); ++i) {
163  if (dims_declared[i] != dims[i]) {
164  std::stringstream msg;
165  msg << "mismatch in dimension declared and found in context"
166  << "; processing stage=" << stage
167  << "; variable name=" << name
168  << "; position="
169  << i
170  << "; dims declared=";
171  add_vec(msg, dims_declared);
172  msg << "; dims found=";
173  add_vec(msg, dims);
174  throw std::runtime_error(msg.str());
175  }
176  }
177  }
const XML_Char * name
Definition: expat.h:151
virtual bool contains_r(const std::string &name) const =0
virtual bool contains_i(const std::string &name) const =0
void add_vec(std::stringstream &msg, const std::vector< size_t > &dims) const
virtual std::vector< size_t > dims_r(const std::string &name) const =0
std::vector<int> stan::io::random_var_context::vals_i ( const std::string &  name) const
inlinevirtual

Returns an empty vector.

Parameters
nameName of variable.
Returns
empty vector

Implements stan::io::var_context.

Definition at line 154 of file random_var_context.hpp.

Referenced by TEST_F().

154  {
155  std::vector<int> empty_vals_i;
156  return empty_vals_i;
157  }
std::vector<double> stan::io::random_var_context::vals_r ( const std::string &  name) const
inlinevirtual

Returns the values of the constrained variables.

Parameters
nameName of variable.
Returns
the constrained values if the variable is in the var_context; an empty vector is returned otherwise

Implements stan::io::var_context.

Definition at line 114 of file random_var_context.hpp.

References PandAna.Demos.demo0::loc, names_, and vals_r_.

Referenced by constrained_to_vals_r(), and TEST_F().

114  {
115  std::vector<std::string>::const_iterator loc
116  = std::find(names_.begin(), names_.end(), name);
117  if (loc == names_.end())
118  return std::vector<double>();
119  return vals_r_[loc - names_.begin()];
120  }
const XML_Char * name
Definition: expat.h:151
std::vector< std::string > names_
std::vector< std::vector< double > > vals_r_

Member Data Documentation

std::vector<std::vector<size_t> > stan::io::random_var_context::dims_
private

Dimensions of parameters in the model

Definition at line 209 of file random_var_context.hpp.

Referenced by dims_r(), and random_var_context().

std::vector<std::string> stan::io::random_var_context::names_
private

Parameter names in the model

Definition at line 205 of file random_var_context.hpp.

Referenced by contains_r(), dims_r(), names_r(), random_var_context(), and vals_r().

std::vector<double> stan::io::random_var_context::unconstrained_params_
private

Random parameter values of the model in the unconstrained space

Definition at line 214 of file random_var_context.hpp.

Referenced by get_unconstrained(), and random_var_context().

std::vector<std::vector<double> > stan::io::random_var_context::vals_r_
private

Random parameter values of the model in the constrained space

Definition at line 219 of file random_var_context.hpp.

Referenced by random_var_context(), and vals_r().


The documentation for this class was generated from the following file: