Stan  1.0
probability, sampling & optimization
Public Member Functions | Protected Attributes | List of all members
stan::model::prob_grad Class Referenceabstract

The prob_grad class represents densities with fixed numbers of discrete and scalar parameters and the gradient with respet to the scalar parameters. More...

#include <prob_grad.hpp>

Inheritance diagram for stan::model::prob_grad:
stan::model::prob_grad_ad stan::model::stanc_model

Public Member Functions

 prob_grad (size_t num_params_r)
 
 prob_grad (size_t num_params_r, std::vector< std::pair< int, int > > &param_ranges_i)
 
virtual ~prob_grad ()
 
void set_num_params_r (size_t num_params_r)
 
void setparam_ranges_i__ (std::vector< std::pair< int, int > > param_ranges_i)
 
virtual size_t num_params_r ()
 
virtual size_t num_params_i ()
 
std::pair< int, int > param_range_i (size_t idx)
 
void set_param_range_i_lower (size_t idx, int low)
 
void set_param_range_i_upper (size_t idx, int up)
 
int param_range_i_lower (size_t idx)
 
int param_range_i_upper (size_t idx)
 
virtual void init (std::vector< double > &params_r, std::vector< int > &params_i)
 
virtual double grad_log_prob (std::vector< double > &params_r, std::vector< int > &params_i, std::vector< double > &gradient, std::ostream *output_stream=0)=0
 
virtual double log_prob (std::vector< double > &params_r, std::vector< int > &params_i, std::ostream *output_stream=0)=0
 
virtual double grad_hess_log_prob (std::vector< double > &params_r, std::vector< int > &params_i, std::vector< double > &gradient, std::vector< double > &hessian, std::ostream *output_stream=0)
 Evaluate the log-probability, its gradient, and its Hessian at params_r. More...
 
virtual double log_prob_star (size_t idx, int val, std::vector< double > &params_r, std::vector< int > &params_i, std::ostream *output_stream=0)
 
virtual void write_csv (std::vector< double > &params_r, std::vector< int > &params_i, std::ostream &o, std::ostream *output_stream=0)
 Write the parameters on a single line in CSV format. More...
 
void finite_diff_grad (std::vector< double > &params_r, std::vector< int > &params_i, std::vector< double > &grad, double epsilon=1e-6, std::ostream *output_stream=0)
 Compute the gradient using finite differences for the specified parameters, writing the result into the specified gradient, using the specified perturbation. More...
 
int test_gradients (std::vector< double > &params_r, std::vector< int > &params_i, double epsilon=1e-6, double error=1e-6, std::ostream &o=std::cout, std::ostream *output_stream=0)
 Test the grad_log_prob() function's ability to produce accurate gradients using finite differences. More...
 

Protected Attributes

size_t num_params_r__
 
std::vector< std::pair< int, int > > param_ranges_i__
 

Detailed Description

The prob_grad class represents densities with fixed numbers of discrete and scalar parameters and the gradient with respet to the scalar parameters.

Definition at line 24 of file prob_grad.hpp.

Constructor & Destructor Documentation

◆ prob_grad() [1/2]

stan::model::prob_grad::prob_grad ( size_t  num_params_r)
inline

Definition at line 31 of file prob_grad.hpp.

◆ prob_grad() [2/2]

stan::model::prob_grad::prob_grad ( size_t  num_params_r,
std::vector< std::pair< int, int > > &  param_ranges_i 
)
inline

Definition at line 36 of file prob_grad.hpp.

◆ ~prob_grad()

virtual stan::model::prob_grad::~prob_grad ( )
inlinevirtual

Definition at line 42 of file prob_grad.hpp.

Member Function Documentation

◆ finite_diff_grad()

void stan::model::prob_grad::finite_diff_grad ( std::vector< double > &  params_r,
std::vector< int > &  params_i,
std::vector< double > &  grad,
double  epsilon = 1e-6,
std::ostream *  output_stream = 0 
)
inline

Compute the gradient using finite differences for the specified parameters, writing the result into the specified gradient, using the specified perturbation.

Parameters
params_rReal-valued parameters.
params_iInteger-valued parameters.
[out]gradVector into which gradient is written.
epsilon
[in,out]output_stream

Definition at line 201 of file prob_grad.hpp.

◆ grad_hess_log_prob()

virtual double stan::model::prob_grad::grad_hess_log_prob ( std::vector< double > &  params_r,
std::vector< int > &  params_i,
std::vector< double > &  gradient,
std::vector< double > &  hessian,
std::ostream *  output_stream = 0 
)
inlinevirtual

Evaluate the log-probability, its gradient, and its Hessian at params_r.

This default version computes the Hessian numerically by finite-differencing the gradient, at a cost of O(params_r.size()^2).

Parameters
params_rReal-valued parameter vector.
params_iInteger-valued parameter vector.
gradientVector to write gradient to.
hessianVector to write gradient to. hessian[i*D + j] gives the element at the ith row and jth column of the Hessian (where D=params_r.size()).
output_streamStream to which print statements in Stan programs are written, default is 0

Definition at line 112 of file prob_grad.hpp.

◆ grad_log_prob()

virtual double stan::model::prob_grad::grad_log_prob ( std::vector< double > &  params_r,
std::vector< int > &  params_i,
std::vector< double > &  gradient,
std::ostream *  output_stream = 0 
)
pure virtual

Implemented in stan::model::prob_grad_ad.

◆ init()

virtual void stan::model::prob_grad::init ( std::vector< double > &  params_r,
std::vector< int > &  params_i 
)
inlinevirtual

Definition at line 80 of file prob_grad.hpp.

◆ log_prob()

virtual double stan::model::prob_grad::log_prob ( std::vector< double > &  params_r,
std::vector< int > &  params_i,
std::ostream *  output_stream = 0 
)
pure virtual

Implemented in stan::model::prob_grad_ad.

◆ log_prob_star()

virtual double stan::model::prob_grad::log_prob_star ( size_t  idx,
int  val,
std::vector< double > &  params_r,
std::vector< int > &  params_i,
std::ostream *  output_stream = 0 
)
inlinevirtual

Definition at line 144 of file prob_grad.hpp.

◆ num_params_i()

virtual size_t stan::model::prob_grad::num_params_i ( )
inlinevirtual

Definition at line 56 of file prob_grad.hpp.

◆ num_params_r()

virtual size_t stan::model::prob_grad::num_params_r ( )
inlinevirtual

Definition at line 52 of file prob_grad.hpp.

◆ param_range_i()

std::pair<int,int> stan::model::prob_grad::param_range_i ( size_t  idx)
inline

Definition at line 60 of file prob_grad.hpp.

◆ param_range_i_lower()

int stan::model::prob_grad::param_range_i_lower ( size_t  idx)
inline

Definition at line 72 of file prob_grad.hpp.

◆ param_range_i_upper()

int stan::model::prob_grad::param_range_i_upper ( size_t  idx)
inline

Definition at line 76 of file prob_grad.hpp.

◆ set_num_params_r()

void stan::model::prob_grad::set_num_params_r ( size_t  num_params_r)
inline

Definition at line 44 of file prob_grad.hpp.

◆ set_param_range_i_lower()

void stan::model::prob_grad::set_param_range_i_lower ( size_t  idx,
int  low 
)
inline

Definition at line 64 of file prob_grad.hpp.

◆ set_param_range_i_upper()

void stan::model::prob_grad::set_param_range_i_upper ( size_t  idx,
int  up 
)
inline

Definition at line 68 of file prob_grad.hpp.

◆ setparam_ranges_i__()

void stan::model::prob_grad::setparam_ranges_i__ ( std::vector< std::pair< int, int > >  param_ranges_i)
inline

Definition at line 48 of file prob_grad.hpp.

◆ test_gradients()

int stan::model::prob_grad::test_gradients ( std::vector< double > &  params_r,
std::vector< int > &  params_i,
double  epsilon = 1e-6,
double  error = 1e-6,
std::ostream &  o = std::cout,
std::ostream *  output_stream = 0 
)
inline

Test the grad_log_prob() function's ability to produce accurate gradients using finite differences.

This shouldn't be necessary when using autodiff, but is useful for finding bugs in hand-written code (or agrad).

Parameters
params_rReal-valued parameter vector.
params_iInteger-valued parameter vector.
epsilonReal-valued scalar saying how much to perturb
errorReal-valued scalar saying how much error to allow
oOutput stream for messages. params_r. Defaults to 1e-6.
output_streamStream to which Stan programs write.
Returns
number of failed gradient comparisons versus allowed error, so 0 if all gradients pass

Definition at line 236 of file prob_grad.hpp.

◆ write_csv()

virtual void stan::model::prob_grad::write_csv ( std::vector< double > &  params_r,
std::vector< int > &  params_i,
std::ostream &  o,
std::ostream *  output_stream = 0 
)
inlinevirtual

Write the parameters on a single line in CSV format.

The implementation in this abstract base class writes out the free parameters as viwed by HMC. Subclasses may extend this class and override this implementation to return constrained values which may differ in both number and scale from the raw unconstrained parameters.

Parameters
params_rReal-valued parameter vector.
params_iInteger-valued parameter vector. @params o Stream to which CSV file is written
output_streamStream to which print statements in Stan programs are written, default is 0

Definition at line 177 of file prob_grad.hpp.

Member Data Documentation

◆ num_params_r__

size_t stan::model::prob_grad::num_params_r__
protected

Definition at line 26 of file prob_grad.hpp.

◆ param_ranges_i__

std::vector<std::pair<int,int> > stan::model::prob_grad::param_ranges_i__
protected

Definition at line 27 of file prob_grad.hpp.


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

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