# What is syntex difference between pystan and cmdstanpy

**URL:** <https://discourse.mc-stan.org/t/what-is-syntex-difference-between-pystan-and-cmdstanpy/35260>\
**Category:** Modeling\
**Created:** [May 26, 2024, 8:36am UTC](https://discourse.mc-stan.org/t/what-is-syntex-difference-between-pystan-and-cmdstanpy/35260 "2024-05-26T08:36:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nij\_PADARIYA](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nij_padariya/32/18540_2.png) [@Nij\_PADARIYA](https://discourse.mc-stan.org/u/Nij_PADARIYA)\
**Post date:** [May 26, 2024, 8:36am UTC](https://discourse.mc-stan.org/t/what-is-syntex-difference-between-pystan-and-cmdstanpy/35260/1 "2024-05-26T08:36:02Z")

</div>

import stan

schools\_code = “”"  
data {  
int\<lower=0\> J; // number of schools  
array[J] real y; // estimated treatment effects  
array[J] real\<lower=0\> sigma; // standard error of effect estimates  
}  
parameters {  
real mu; // population treatment effect  
real\<lower=0\> tau; // standard deviation in treatment effects  
vector[J] eta; // unscaled deviation from mu by school  
}  
transformed parameters {  
vector[J] theta = mu + tau \* eta; // school treatment effects  
}  
model {  
target += normal\_lpdf(eta | 0, 1); // prior log-density  
target += normal\_lpdf(y | theta, sigma); // log-likelihood  
}  
“”"

schools\_data = {“J”: 8,  
“y”: [28, 8, -3, 7, -1, 1, 18, 12],  
“sigma”: [15, 10, 16, 11, 9, 11, 10, 18]}

posterior = stan.build(schools\_code, data=schools\_data)  
fit = posterior.sample(num\_chains=4, num\_samples=1000)  
eta = fit[“eta”] # array with shape (8, 4000)  
df = fit.to\_frame() # pandas `DataFrame, requires pandas

how to write same code in CmdStanPy

---

<div class="post-metadata">

**Author:** ![Garren\_Hermanus](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/garren_hermanus/32/17356_2.png) [@Garren\_Hermanus](https://discourse.mc-stan.org/u/Garren_Hermanus)\
**Post date:** [May 27, 2024, 12:11am UTC](https://discourse.mc-stan.org/t/what-is-syntex-difference-between-pystan-and-cmdstanpy/35260/2 "2024-05-27T00:11:02Z")

</div>

Hi, a generic example of using cmdstanpy is as follows:

```no-highlight
import cmdstanpy

# write stan code
model_code = '''
    // Write valid stan code here
'''

# save the model_code to a .stan file
with open('path/to/save/model.stan', 'w') as f:
   f.write(model_code)

# compile stan model
model = cmdstanpy.CmdStanModel(stan_file='path/to/save/model_code')

# define dictionary with data
 data = {...}

# sample
fit = model.sample(data=data, chains=4, iter_warmup=1000, iter_sampling=1000)

# Extract posterior samples
posterior_dict = fit.stan_variables() # dict by default

```

See the [cmdstanpy documentation](https://cmdstanpy.readthedocs.io/en/v1.2.0/users-guide/examples/MCMC%20Sampling.html)

---

<div class="post-metadata">

**Author:** ![Nij\_PADARIYA](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nij_padariya/32/18540_2.png) [@Nij\_PADARIYA](https://discourse.mc-stan.org/u/Nij_PADARIYA)\
**Post date:** [May 27, 2024, 3:47am UTC](https://discourse.mc-stan.org/t/what-is-syntex-difference-between-pystan-and-cmdstanpy/35260/3 "2024-05-27T03:47:11Z")

</div>

pystan and cmdstanpy will give same output or not ?
