22 "cells" : [
33 {
44 "cell_type" : " markdown" ,
5+ "metadata" : {},
56 "source" : [
67 " # Advanced Topic: Using External C++ Functions\n " ,
78 " \n " ,
89 " This is based on the relevant portion of the CmdStan documentation [here](https://mc-stan.org/docs/cmdstan-guide/using-external-cpp-code.html)"
9- ],
10- "metadata" : {}
10+ ]
1111 },
1212 {
1313 "cell_type" : " markdown" ,
14+ "metadata" : {},
1415 "source" : [
1516 " Consider the following Stan model, based on the bernoulli example."
16- ],
17- "metadata" : {}
17+ ]
1818 },
1919 {
2020 "cell_type" : " code" ,
2121 "execution_count" : null ,
22+ "metadata" : {"nbsphinx" : " hidden" },
23+ "outputs" : [],
2224 "source" : [
2325 " import os\n " ,
24- " from cmdstanpy import cmdstan_path, CmdStanModel\n " ,
26+ " try:\n " ,
27+ " os.remove('bernoulli_external')\n " ,
28+ " except:\n " ,
29+ " pass"
30+ ]
31+ },
32+ {
33+ "cell_type" : " code" ,
34+ "execution_count" : null ,
35+ "metadata" : {},
36+ "outputs" : [],
37+ "source" : [
38+ " from cmdstanpy import CmdStanModel\n " ,
2539 " model_external = CmdStanModel(stan_file='bernoulli_external.stan', compile=False)\n " ,
2640 " print(model_external.code())"
27- ],
28- "outputs" : [],
29- "metadata" : {}
41+ ]
3042 },
3143 {
3244 "cell_type" : " markdown" ,
45+ "metadata" : {},
3346 "source" : [
3447 " As you can see, it features a function declaration for `make_odds`, but no definition. If we try to compile this, we will get an error. "
35- ],
36- "metadata" : {}
48+ ]
3749 },
3850 {
3951 "cell_type" : " code" ,
4052 "execution_count" : null ,
53+ "metadata" : {},
54+ "outputs" : [],
4155 "source" : [
4256 " model_external.compile()"
43- ],
44- "outputs" : [],
45- "metadata" : {}
57+ ]
4658 },
4759 {
4860 "cell_type" : " markdown" ,
61+ "metadata" : {},
4962 "source" : [
5063 " Even enabling the `--allow_undefined` flag to stanc3 will not allow this model to be compiled quite yet."
51- ],
52- "metadata" : {}
64+ ]
5365 },
5466 {
5567 "cell_type" : " code" ,
5668 "execution_count" : null ,
69+ "metadata" : {},
70+ "outputs" : [],
5771 "source" : [
5872 " model_external.compile(stanc_options={'allow_undefined':True})"
59- ],
60- "outputs" : [],
61- "metadata" : {}
73+ ]
6274 },
6375 {
6476 "cell_type" : " markdown" ,
77+ "metadata" : {},
6578 "source" : [
6679 " To resolve this, we need to both tell the Stan compiler an undefined function is okay **and** let C++ know what it should be. \n " ,
6780 " \n " ,
6881 " We can provide a definition in a C++ header file by using the `user_header` argument to either the CmdStanModel constructor or the `compile` method. \n " ,
6982 " \n " ,
7083 " This will enables the `allow_undefined` flag automatically."
71- ],
72- "metadata" : {}
84+ ]
7385 },
7486 {
7587 "cell_type" : " code" ,
7688 "execution_count" : null ,
89+ "metadata" : {},
90+ "outputs" : [],
7791 "source" : [
7892 " model_external.compile(user_header='make_odds.hpp')"
79- ],
80- "outputs" : [],
81- "metadata" : {}
93+ ]
8294 },
8395 {
8496 "cell_type" : " markdown" ,
97+ "metadata" : {},
8598 "source" : [
8699 " We can then run this model and inspect the output"
87- ],
88- "metadata" : {}
100+ ]
89101 },
90102 {
91103 "cell_type" : " code" ,
92104 "execution_count" : null ,
105+ "metadata" : {},
106+ "outputs" : [],
93107 "source" : [
94108 " fit = model_external.sample(data={'N':10, 'y':[0,1,0,0,0,0,0,0,0,1]})\n " ,
95109 " fit.stan_variable('odds')"
96- ],
97- "outputs" : [],
98- "metadata" : {}
110+ ]
99111 },
100112 {
101113 "cell_type" : " markdown" ,
114+ "metadata" : {},
102115 "source" : [
103116 " The contents of this header file are a bit complicated unless you are familiar with the C++ internals of Stan, so they are presented without comment:\n " ,
104117 " \n " ,
114127 " }\n " ,
115128 " }\n " ,
116129 " ```"
117- ],
118- "metadata" : {}
130+ ]
119131 }
120132 ],
121133 "metadata" : {
122- "orig_nbformat" : 4 ,
134+ "interpreter" : {
135+ "hash" : " 8765ce46b013071999fc1966b52035a7309a0da7551e066cc0f0fa23e83d4f60"
136+ },
137+ "kernelspec" : {
138+ "display_name" : " Python 3.9.5 64-bit ('stan': conda)" ,
139+ "name" : " python3"
140+ },
123141 "language_info" : {
124- "name" : " python" ,
125- "version" : " 3.9.5" ,
126- "mimetype" : " text/x-python" ,
127142 "codemirror_mode" : {
128143 "name" : " ipython" ,
129144 "version" : 3
130145 },
131- "pygments_lexer" : " ipython3" ,
146+ "file_extension" : " .py" ,
147+ "mimetype" : " text/x-python" ,
148+ "name" : " python" ,
132149 "nbconvert_exporter" : " python" ,
133- "file_extension" : " .py"
134- },
135- "kernelspec" : {
136- "name" : " python3" ,
137- "display_name" : " Python 3.9.5 64-bit ('stan': conda)"
150+ "pygments_lexer" : " ipython3" ,
151+ "version" : " 3.9.5"
138152 },
139- "interpreter" : {
140- "hash" : " d31ce8e45781476cfd394e192e0962028add96ff436d4fd4e560a347d206b9cb"
141- }
153+ "orig_nbformat" : 4
142154 },
143155 "nbformat" : 4 ,
144156 "nbformat_minor" : 2
145- }
157+ }
0 commit comments