```
import cmdstanpy
import matplotlib.pyplot as plt
= 0 # count of (1, 1)
y_1_1 = 0 # count of (0, 0)
y_0_0 = 0 # count of (0, 1)
y_0_1 = 0 # count of (1, 0)
y_1_0
= list('TTHHTHTTHTTTHTTTHTTTHTTHTHHTHHTHTHHTTTHHTHTHTTHTHHTTHTHHTHTTTHHTTHHTTHHHTHHTHTTHTHTTHHTHHHTTHTHTTTHHTTHTHTHTHTHTTHTHTHHHTTHTHTHHTHHHTHTHTTHTTHHTHTHTHTTHHTTHTHTTHHHTHTHTHTTHTTHHTTHTHHTHHHTTHHTHTTHTHTHTHTHTHTHHHTHTHTHTHHTHHTHTHTTHTTTHHTHTTTHTHHTHHHHTTTHHTHTHTHTHHHTTHHTHTTTHTHHTHTHTHHTHTTHTTHTHHTHTHTTT'.upper())
sequence
# Do a rolling window trick I saw somewhere on twitter.
# This implements a rollowing window of 2
# In python 3.10, this would be a great use case for match
for pairs in zip(sequence[:-1], sequence[1:]):
if pairs == ('H','H'):
+=1
y_1_1 elif pairs == ('T','H'):
+=1
y_0_1 elif pairs == ('H', 'T'):
+=1
y_1_0 else:
+=1
y_0_0
# Write the stan model as a string. We will then write it to a file
= '''
stan_code data{
int y_1_1; //number of concurrent 1s
int y_0_1; //number of 0,1 occurences
int y_1_0; //number of 1,0 occurences
int y_0_0; //number of concurrent 0s
}
transformed data{
int y[4] = {y_1_1, y_0_1, y_1_0, y_0_0};
}
parameters{
real<lower=-1, upper=1> rho;
real<lower=0, upper=1> q;
}
transformed parameters{
real<lower=0, upper=1> prob_1_1 = q + rho*(1-q);
real<lower=0, upper=1> prob_0_1 = (1-q)*(1-rho);
real<lower=0, upper=1> prob_1_0 = q*(1-rho);
real<lower=0, upper=1> prob_0_0 = 1 - q + rho*q;
simplex[4] theta = 0.5*[prob_1_1, prob_0_1, prob_1_0, prob_0_0 ]';
}
model{
q ~ beta(1, 1);
rho ~ uniform(-1, 1);
y ~ multinomial(theta);
}
generated quantities{
vector[300] yppc;
yppc[1] = bernoulli_rng(q);
for(i in 2:300){
if(yppc[i-1]==1){
yppc[i] = bernoulli_rng(prob_1_1);
}
else{
yppc[i] = bernoulli_rng(prob_1_0);
}
}
}
'''
# Write the model to a temp file
with open('model_file.stan', 'w') as model_file:
model_file.write(stan_code)
# Compile the model
= cmdstanpy.CmdStanModel(stan_file='model_file.stan')
model
# data to pass to Stan
= dict(y_1_1 = y_1_1, y_0_0 = y_0_0, y_0_1 = y_0_1, y_1_0 = y_1_0)
data
# Plotting stuff.
= plt.subplots(dpi = 120, ncols=2, figsize = (15, 5))
fig, ax
0].set_title('Auto-correlation')
ax[1].set_title('Bias')
ax[
0].set_xlim(-1, 1)
ax[1].set_xlim(0, 1)
ax[
0].axvline(0, color = 'red')
ax[1].axvline(0.5, color = 'red')
ax[
0].annotate('Uncorrelated Flips', xy=(0.475, 0.5), xycoords='axes fraction', rotation = 90)
ax[1].annotate('Unbiased Flips', xy=(0.475, 0.5), xycoords='axes fraction', rotation = 90)
ax[
# MCMC go brrrr
= model.sample(data)
fit
0].hist(fit.stan_variable('rho'), edgecolor='k', alpha = 0.5)
ax[1].hist(fit.stan_variable('q'), edgecolor='k', alpha = 0.5)
ax[
= fit.stan_variable('rho').mean()
autocorr = fit.stan_variable('q').mean()
bias
= f"Your flips have an expected correlation of {autocorr:.2f} and your coin's bias is about {bias:.2f}"
tweet
print(f"Your sequence was {''.join(sequence)}")
print(tweet)
```

`INFO:cmdstanpy:compiling stan file /Users/demetri/Documents/dpananos.github.io/posts/2022-05-07-flippin/model_file.stan to exe file /Users/demetri/Documents/dpananos.github.io/posts/2022-05-07-flippin/model_file`

`INFO:cmdstanpy:compiled model executable: /Users/demetri/Documents/dpananos.github.io/posts/2022-05-07-flippin/model_file`

`WARNING:cmdstanpy:Stan compiler has produced 1 warnings:`

```
WARNING:cmdstanpy:
--- Translating Stan model to C++ code ---
bin/stanc --o=/Users/demetri/Documents/dpananos.github.io/posts/2022-05-07-flippin/model_file.hpp /Users/demetri/Documents/dpananos.github.io/posts/2022-05-07-flippin/model_file.stan
Warning in '/Users/demetri/Documents/dpananos.github.io/posts/2022-05-07-flippin/model_file.stan', line 11, column 4: Declaration
of arrays by placing brackets after a variable name is deprecated and
will be removed in Stan 2.32.0. Instead use the array keyword before the
type. This can be changed automatically using the auto-format flag to
stanc
--- Compiling, linking C++ code ---
clang++ -std=c++1y -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT -Wno-ignored-attributes -I stan/lib/stan_math/lib/tbb_2020.3/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.9 -I stan/lib/stan_math/lib/boost_1.75.0 -I stan/lib/stan_math/lib/sundials_6.0.0/include -I stan/lib/stan_math/lib/sundials_6.0.0/src/sundials -DBOOST_DISABLE_ASSERTS -c -include-pch stan/src/stan/model/model_header.hpp.gch -x c++ -o /Users/demetri/Documents/dpananos.github.io/posts/2022-05-07-flippin/model_file.o /Users/demetri/Documents/dpananos.github.io/posts/2022-05-07-flippin/model_file.hpp
clang++ -std=c++1y -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT -Wno-ignored-attributes -I stan/lib/stan_math/lib/tbb_2020.3/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.9 -I stan/lib/stan_math/lib/boost_1.75.0 -I stan/lib/stan_math/lib/sundials_6.0.0/include -I stan/lib/stan_math/lib/sundials_6.0.0/src/sundials -DBOOST_DISABLE_ASSERTS -Wl,-L,"/Users/demetri/.cmdstan/cmdstan-2.29.2/stan/lib/stan_math/lib/tbb" -Wl,-rpath,"/Users/demetri/.cmdstan/cmdstan-2.29.2/stan/lib/stan_math/lib/tbb" /Users/demetri/Documents/dpananos.github.io/posts/2022-05-07-flippin/model_file.o src/cmdstan/main.o -Wl,-L,"/Users/demetri/.cmdstan/cmdstan-2.29.2/stan/lib/stan_math/lib/tbb" -Wl,-rpath,"/Users/demetri/.cmdstan/cmdstan-2.29.2/stan/lib/stan_math/lib/tbb" stan/lib/stan_math/lib/sundials_6.0.0/lib/libsundials_nvecserial.a stan/lib/stan_math/lib/sundials_6.0.0/lib/libsundials_cvodes.a stan/lib/stan_math/lib/sundials_6.0.0/lib/libsundials_idas.a stan/lib/stan_math/lib/sundials_6.0.0/lib/libsundials_kinsol.a stan/lib/stan_math/lib/tbb/libtbb.dylib stan/lib/stan_math/lib/tbb/libtbbmalloc.dylib stan/lib/stan_math/lib/tbb/libtbbmalloc_proxy.dylib -o /Users/demetri/Documents/dpananos.github.io/posts/2022-05-07-flippin/model_file
rm -f /Users/demetri/Documents/dpananos.github.io/posts/2022-05-07-flippin/model_file.o
```

`INFO:cmdstanpy:CmdStan start processing`

` `

` `

` `

` `

`INFO:cmdstanpy:CmdStan done processing.`

```
Your sequence was TTHHTHTTHTTTHTTTHTTTHTTHTHHTHHTHTHHTTTHHTHTHTTHTHHTTHTHHTHTTTHHTTHHTTHHHTHHTHTTHTHTTHHTHHHTTHTHTTTHHTTHTHTHTHTHTTHTHTHHHTTHTHTHHTHHHTHTHTTHTTHHTHTHTHTTHHTTHTHTTHHHTHTHTHTTHTTHHTTHTHHTHHHTTHHTHTTHTHTHTHTHTHTHHHTHTHTHTHHTHHTHTHTTHTTTHHTHTTTHTHHTHHHHTTTHHTHTHTHTHHHTTHHTHTTTHTHHTHTHTHHTHTTHTTHTHHTHTHTTT
Your flips have an expected correlation of -0.36 and your coin's bias is about 0.49
```