Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
print.hpp
Go to the documentation of this file.
1 #ifndef STAN__COMMAND__PRINT_HPP
2 #define STAN__COMMAND__PRINT_HPP
3 
4 #include <algorithm>
5 #include <iostream>
6 #include <iomanip>
7 #include <ios>
8 #include <stan/mcmc/chains.hpp>
9 
10 void compute_width_and_precision(double value, int sig_figs, int& width, int& precision) {
11 
12  double abs_value = std::fabs(value);
13 
14  if (value == 0) {
15  width = sig_figs;
16  precision = sig_figs;
17  }
18  else if (abs_value >= 1) {
19  int int_part = std::ceil(log10(abs_value) + 1e-6);
20  width = int_part >= sig_figs ? int_part : sig_figs + 1;
21  precision = int_part >= sig_figs ? 0 : sig_figs - int_part;
22  }
23  else {
24  int frac_part = std::fabs(std::floor(log10(abs_value)));
25  width = 1 + frac_part + sig_figs;
26  precision = frac_part + sig_figs - 1;
27  }
28 
29  if (value < 0) ++width;
30 
31 }
32 
33 int compute_width(double value, int sig_figs) {
34  int width;
35  int precision;
36  compute_width_and_precision(value, sig_figs, width, precision);
37  return width;
38 }
39 
40 int compute_precision(double value, int sig_figs, bool scientific) {
41 
42  if (scientific) {
43  return sig_figs - 1;
44  }
45  else {
46  int width;
47  int precision;
48  compute_width_and_precision(value, sig_figs, width, precision);
49  return precision;
50  }
51 
52 }
53 
54 int calculate_column_width(const Eigen::VectorXd& x,
55  const std::string& name,
56  const int sig_figs,
57  std::ios_base::fmtflags& format) {
58 
59  int padding = 2;
60 
61  // Fixed Precision
62  size_t fixed_threshold = 8;
63  size_t max_fixed_width = 0;
64 
65  for (int i = 0; i < x.size(); ++i) {
66  size_t width = compute_width(x[i], sig_figs);
67  max_fixed_width = width > max_fixed_width ? width : max_fixed_width;
68  }
69 
70  if (max_fixed_width + padding < fixed_threshold) {
71  format = std::ios_base::fixed;
72  max_fixed_width = name.length() > max_fixed_width ? name.length() : max_fixed_width;
73  return max_fixed_width + padding;
74  }
75 
76  // Scientific Notation
77  size_t scientific_width = sig_figs + 1 + 4; // Decimal place + exponent
78  if (x.minCoeff() < 0) ++scientific_width;
79 
80  scientific_width = name.length() > scientific_width ? name.length() : scientific_width;
81 
82  format = std::ios_base::scientific;
83  return scientific_width + padding;
84 
85 }
86 
87 Eigen::VectorXi calculate_column_widths(const Eigen::MatrixXd& values,
88  const Eigen::Matrix<std::string, Eigen::Dynamic, 1>& headers,
89  const int sig_figs,
90  Eigen::Matrix<std::ios_base::fmtflags, Eigen::Dynamic, 1>& formats) {
91  int n = values.cols();
92  Eigen::VectorXi column_widths(n);
93  formats.resize(n);
94  for (int i = 0; i < n; i++) {
95  column_widths(i) = calculate_column_width(values.col(i), headers(i), sig_figs, formats(i));
96  }
97  return column_widths;
98 }
99 
100 void print_usage() {
101 
102  std::cout << "USAGE: print <filename 1> [<filename 2> ... <filename N>]"
103  << std::endl
104  << std::endl;
105 
106  std::cout << "OPTIONS:" << std::endl << std::endl;
107  std::cout << " --autocorr=<chain_index>\tAppend the autocorrelations for the given chain"
108  << std::endl
109  << std::endl;
110  std::cout << " --sig_figs=<int>\tSet significant figures of output (Defaults to 2)"
111  << std::endl
112  << std::endl;
113 
114 }
115 
116 bool is_matrix(const std::string& parameter_name) {
117  return (parameter_name.find("[") != std::string::npos);
118 }
119 
120 std::string base_param_name(stan::mcmc::chains<>& chains, const int index) {
121  std::string name = chains.param_name(index);
122  return name.substr(0, name.find("["));
123 }
124 
125 std::string matrix_index(stan::mcmc::chains<>& chains, const int index) {
126  std::string name = chains.param_name(index);
127  return name.substr(name.find("["));
128 }
129 
130 std::vector<int> dimensions(stan::mcmc::chains<>& chains, const int start_index) {
131  std::vector<int> dims;
132  int dim;
133 
134  std::string name = base_param_name(chains, start_index);
135  int last_matrix_element = start_index;
136  while (last_matrix_element+1 < chains.num_params()) {
137  if (base_param_name(chains, last_matrix_element+1) == name)
138  last_matrix_element++;
139  else
140  break;
141  }
142 
143  std::stringstream ss(matrix_index(chains, last_matrix_element));
144  ss.get();
145  ss >> dim;
146 
147  dims.push_back(dim);
148  while (ss.get() == ',') {
149  ss >> dim;
150  dims.push_back(dim);
151  }
152  return dims;
153 }
154 
155 // next 1-based index in row major order
156 void next_index(std::vector<int>& index, const std::vector<int>& dims) {
157  if (dims.size() != index.size())
158  throw std::domain_error("next_index: size mismatch");
159  if (dims.size() == 0)
160  return;
161  index[index.size()-1]++;
162 
163  for (int i = index.size()-1; i > 0; i--) {
164  if (index[i] > dims[i]) {
165  index[i-1]++;
166  index[i] = 1;
167  }
168  }
169 
170  for (size_t n = 0; n < dims.size(); n++) {
171  if (index[n] <= 0 || index[n] > dims[n]) {
172  std::stringstream message_stream("");
173  message_stream << "next_index: index[" << n << "] out of bounds. "
174  << "dims[" << n << "] = " << dims[n] << "; "
175  << "index[" << n << "] = " << index[n];
176  throw std::domain_error(message_stream.str());
177  }
178  }
179 }
180 
181 // return the flat 0-based index of a column major order matrix based on the
182 // 1-based index
183 int matrix_index(std::vector<int>& index, const std::vector<int>& dims) {
184  if (dims.size() != index.size())
185  throw std::domain_error("next_index: size mismatch");
186  if (dims.size() == 0)
187  return 0;
188  for (size_t n = 0; n < dims.size(); n++) {
189  if (index[n] <= 0 || index[n] > dims[n]) {
190  std::stringstream message_stream("");
191  message_stream << "matrix_index: index[" << n << "] out of bounds. "
192  << "dims[" << n << "] = " << dims[n] << "; "
193  << "index[" << n << "] = " << index[n];
194  throw std::domain_error(message_stream.str());
195  }
196  }
197 
198  int offset = 0;
199  int prod = 1;
200  for (size_t i = 0; i < dims.size(); i++) {
201  offset += (index[i]-1) * prod;
202  prod *= dims[i];
203  }
204  return offset;
205 }
206 
207 #endif
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:18
const std::string & param_name(int j) const
Definition: chains.hpp:352
fvar< T > floor(const fvar< T > &x)
Definition: floor.hpp:15
fvar< T > ceil(const fvar< T > &x)
Definition: ceil.hpp:15
void dims(const T &x, std::vector< int > &result)
Definition: dims.hpp:13
T prod(const std::vector< T > &v)
Returns the product of the coefficients of the specified standard vector.
Definition: prod.hpp:17
An mcmc::chains object stores parameter names and dimensionalities along with samples from multiple c...
Definition: chains.hpp:55
int num_params() const
Definition: chains.hpp:344
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:86
fvar< T > log10(const fvar< T > &x)
Definition: log10.hpp:15

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