1 #ifndef STAN_SERVICES_SAMPLE_FIXED_PARAM_HPP
2 #define STAN_SERVICES_SAMPLE_FIXED_PARAM_HPP
3 
4 #include <stan/callbacks/interrupt.hpp>
5 #include <stan/callbacks/logger.hpp>
6 #include <stan/callbacks/writer.hpp>
7 #include <stan/math/prim/mat/fun/Eigen.hpp>
8 #include <stan/mcmc/fixed_param_sampler.hpp>
9 #include <stan/services/error_codes.hpp>
10 #include <stan/services/util/mcmc_writer.hpp>
11 #include <stan/services/util/generate_transitions.hpp>
12 #include <stan/services/util/create_rng.hpp>
13 #include <stan/services/util/initialize.hpp>
14 #include <vector>
15 
16 namespace stan {
17   namespace services {
18     namespace sample {
19 
20       /**
21        * Runs the fixed parameter sampler.
22        *
23        * The fixed parameter sampler sets the parameters randomly once
24        * on the unconstrained scale, then runs the model for the number
25        * of iterations specified with the parameters fixed.
26        *
27        * @tparam Model Model class
28        * @param[in] model Input model to test (with data already instantiated)
29        * @param[in] init var context for initialization
30        * @param[in] random_seed random seed for the random number generator
31        * @param[in] chain chain id to advance the pseudo random number generator
32        * @param[in] init_radius radius to initialize
33        * @param[in] num_samples Number of samples
34        * @param[in] num_thin Number to thin the samples
35        * @param[in] refresh Controls the output
36        * @param[in,out] interrupt Callback for interrupts
37        * @param[in,out] logger Logger for messages
38        * @param[in,out] init_writer Writer callback for unconstrained inits
39        * @param[in,out] sample_writer Writer for draws
40        * @param[in,out] diagnostic_writer Writer for diagnostic information
41        * @return error_codes::OK if successful
42        */
43       template <class Model>
fixed_param(Model & model,stan::io::var_context & init,unsigned int random_seed,unsigned int chain,double init_radius,int num_samples,int num_thin,int refresh,callbacks::interrupt & interrupt,callbacks::logger & logger,callbacks::writer & init_writer,callbacks::writer & sample_writer,callbacks::writer & diagnostic_writer)44       int fixed_param(Model& model, stan::io::var_context& init,
45                       unsigned int random_seed, unsigned int chain,
46                       double init_radius, int num_samples,
47                       int num_thin, int refresh,
48                       callbacks::interrupt& interrupt,
49                       callbacks::logger& logger,
50                       callbacks::writer& init_writer,
51                       callbacks::writer& sample_writer,
52                       callbacks::writer& diagnostic_writer) {
53         boost::ecuyer1988 rng = util::create_rng(random_seed, chain);
54 
55         std::vector<int> disc_vector;
56         std::vector<double> cont_vector
57           = util::initialize(model, init, rng, init_radius, false,
58                              logger, init_writer);
59 
60         stan::mcmc::fixed_param_sampler sampler;
61         util::mcmc_writer
62           writer(sample_writer, diagnostic_writer, logger);
63         Eigen::VectorXd cont_params(cont_vector.size());
64         for (size_t i = 0; i < cont_vector.size(); i++)
65           cont_params[i] = cont_vector[i];
66         stan::mcmc::sample s(cont_params, 0, 0);
67 
68         // Headers
69         writer.write_sample_names(s, sampler, model);
70         writer.write_diagnostic_names(s, sampler, model);
71 
72         clock_t start = clock();
73 
74         util::generate_transitions(sampler, num_samples, 0, num_samples,
75                                    num_thin, refresh, true, false, writer,
76                                    s, model, rng, interrupt, logger);
77         clock_t end = clock();
78 
79         double sampleDeltaT = static_cast<double>(end - start) / CLOCKS_PER_SEC;
80         writer.write_timing(0.0, sampleDeltaT);
81 
82         return error_codes::OK;
83       }
84 
85     }
86   }
87 }
88 #endif
89