# Sampling within another C++ program, how to construct \`var\_context\`?

**URL:** <https://discourse.mc-stan.org/t/sampling-within-another-c-program-how-to-construct-var-context/29351>\
**Category:** General\
**Tags:** c++api\
**Created:** [November 5, 2022, 12:12pm UTC](https://discourse.mc-stan.org/t/sampling-within-another-c-program-how-to-construct-var-context/29351 "2022-11-05T12:12:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sam\_Watson](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/sam_watson/32/6987_2.png) [@Sam\_Watson](https://discourse.mc-stan.org/u/Sam_Watson)\
**Post date:** [November 5, 2022, 12:12pm UTC](https://discourse.mc-stan.org/t/sampling-within-another-c-program-how-to-construct-var-context/29351/1 "2022-11-05T12:12:05Z")

</div>

I am trying to write a C++ program within which posterior samples are generated using Stan’s sampler and then those samples go on to be part of another calculation, rather than returning the samples and calling a new function externally.

I have generated the .hpp file using stanc, which defines a new class, e.g. `gaussian_model`. I then assume I can generate a new model as something like

```nohighlight
gaussian_model model(stan::io::var_context &context, unsigned int seed, ...);

```

then pass `model` as an argument to `stan::services::sample::hmc_nuts_diag_e_adapt`. However, I am having trouble working out, or finding examples, about how to construct a `stan::io::var_context` object. The `array_var_context` class seems to have constructors that match more the type of data that might be input and I could translate the data into `std::vector`, but how do you generate the `var_context` from the `array_var_context`?. I can also generate a JSON file of the data for the model or other required format. Is there a way of generating a `var_context` given these files?

The sampler can be called with `stan::services::sample::hmc_nuts_diag_e_adapt`, which takes the model object as an argument, and also another `stan::io::var_context` object. Is this right that this `var_context` is the initial values for the parameters and chains? If so, what structure should these be in?

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [November 5, 2022, 4:01pm UTC](https://discourse.mc-stan.org/t/sampling-within-another-c-program-how-to-construct-var-context/29351/2 "2022-11-05T16:01:04Z")

</div>

The best reference here would be the cmdstan source: [cmdstan/command.hpp at develop · stan-dev/cmdstan · GitHub](https://github.com/stan-dev/cmdstan/blob/develop/src/cmdstan/command.hpp)

This shows how `cmdstan` takes input data and inits, and constructs the `var_contexts` for sampling

---

<div class="post-metadata">

**Author:** ![Sam\_Watson](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/sam_watson/32/6987_2.png) [@Sam\_Watson](https://discourse.mc-stan.org/u/Sam_Watson)\
**Post date:** [November 6, 2022, 8:46am UTC](https://discourse.mc-stan.org/t/sampling-within-another-c-program-how-to-construct-var-context/29351/3 "2022-11-06T08:46:33Z")

</div>

Thanks @andrjohns, that’s what i was looking for. Do you know if there are any plans to document the C++ API?

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [November 6, 2022, 1:14pm UTC](https://discourse.mc-stan.org/t/sampling-within-another-c-program-how-to-construct-var-context/29351/4 "2022-11-06T13:14:10Z")

</div>

I’ll defer this to @WardBrian or @Bob_Carpenter since they’re much more familiar with the current doc and plans

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [November 6, 2022, 1:40pm UTC](https://discourse.mc-stan.org/t/sampling-within-another-c-program-how-to-construct-var-context/29351/5 "2022-11-06T13:40:49Z")

</div>

I believe the stan-dev/stan repository has documentation if you run “make doxygen”. It is not hosted anywhere to my knowledge.

To answer a few of your questions:

`array_var_context` is a subclass of `var_context`. Once you have created it, you can simply pass it directly as a var\_context, no further steps needed.

The same is true for the JSON var context in src/stan/io/json/json\_data.hpp, which can be used for JSON files
