# Basic Algebra Solver Example Code

**URL:** <https://discourse.mc-stan.org/t/basic-algebra-solver-example-code/27575>\
**Category:** Interfaces\
**Created:** [May 21, 2022, 12:52am UTC](https://discourse.mc-stan.org/t/basic-algebra-solver-example-code/27575 "2022-05-21T00:52:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![astrophysaxist](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/astrophysaxist/32/14323_2.png) [@astrophysaxist](https://discourse.mc-stan.org/u/astrophysaxist)\
**Post date:** [May 21, 2022, 12:52am UTC](https://discourse.mc-stan.org/t/basic-algebra-solver-example-code/27575/1 "2022-05-21T00:52:47Z")

</div>

Hi, I’ve been playing around with Stan in R with brms and eventually moved on to PyStan and then CmdStanPy for some simple linear regression models. I’ve gotten to the point that I want to incorporate some more advanced features of Stan, namely the Newton solver to find the root of an equation. I’ve read the Stan manual sections on the solver, but I have not been able to get the example discussed ([12 Solving Algebraic Equations | Stan User’s Guide](https://mc-stan.org/docs/2_29/stan-users-guide/algebra-solver.html)) to work.

I’m convinced I’m missing something **really** basic. For example, to get results in the algebra solver section, how should I specify the data, model, parameters block? Or do you even need to for the example given in the text. It’d help me a lot to understand if I could see a more detailed (full?) example of the Algebra Solver example in the manual. Thanks.

---

<div class="post-metadata">

**Author:** ![astrophysaxist](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/astrophysaxist/32/14323_2.png) [@astrophysaxist](https://discourse.mc-stan.org/u/astrophysaxist)\
**Post date:** [May 24, 2022, 2:50pm UTC](https://discourse.mc-stan.org/t/basic-algebra-solver-example-code/27575/2 "2022-05-24T14:50:20Z")

</div>

I had some time to experiment more, so I’ll add an update to this post. Looks like the answer to my question is that indeed the model and data blocks are necessary. I was able get the example from the Stan User Guide 12.3 to work, and I coded it up in a Jupyter notebook, see below and attached html.  
[solver\_example.html](https://discourse.mc-stan.org/uploads/short-url/tfUD0Dpsap7u8yOXL90FrxnZwmV.html) (579.3 KB)

> **Solver Example with CmdStanPy**
>
> ```no-highlight
> import os
> import cmdstanpy
> from cmdstanpy import cmdstan_path, CmdStanModel
> import numpy as np
> 
> ```
> 
> ## Specify the Stan model file
> 
> ```no-highlight
> stan_file = './example_12_3_stan.stan'
> 
> ```
> 
> ```no-highlight
> with open(stan_file,'w') as f:
> f.write("""functions {
> 
> vector system(vector y, // unknowns
> vector theta, // parameters
> data array[] real x_r, // data (real)
> array[] int x_i) { // data (integer)
> vector[2] z;
> z[1] = y[1] - theta[1];
> z[2] = y[1] * y[2] - theta[2];
> return z;
> }
> 
> }
> 
> data {
> int N;
> vector[N] Y;
> }
> 
> transformed data {
> vector[2] y_guess;
> y_guess[1] = 1;
> y_guess[2] = 1;
> array[1] real x_r;
> array[1] int x_i;
> }
> 
> parameters {
> real alpha;
> }
> 
> transformed parameters {
> vector[2] theta;
> theta[1] = 3;
> theta[2] = 6;
> vector[2] y;
> y = algebra_solver_newton(system, y_guess, theta, x_r, x_i);
> }
> 
> model {
> 
> Y ~ normal(alpha,0.1);
>  
> 
> }
> 
> """)
> 
> ```
> 
> ## Specify the input data dictionary
> 
> ```no-highlight
> y = np.random.normal(loc=0,scale=0.1,size=10)
> data_dict = {'N':10, 'Y':y}
> 
> ```
> 
> ## Instantiate the model; compiles the Stan program as needed.
> 
> ```no-highlight
> model = CmdStanModel(stan_file=stan_file)
> 
> ```
> 
> ## Fit the model
> 
> ```no-highlight
> fit = model.sample(data=data_dict)
> 
> ```
> 
> ## Observe the summary
> 
> ```no-highlight
> fit.summary()
> 
> ```
> 
> Solver found solution to system of equation and values are stored in vector y. `y[1]` and `y[2]` are the solutions 3.0 and 2.0 as described in the example text.

---

<div class="post-metadata">

**Author:** ![betanalpha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/betanalpha/32/4_2.png) [@betanalpha](https://discourse.mc-stan.org/u/betanalpha)\
**Post date:** [June 14, 2022, 2:20am UTC](https://discourse.mc-stan.org/t/basic-algebra-solver-example-code/27575/3 "2022-06-14T02:20:17Z")

</div>

If you’re just trying to run the `algebra_solver`, but not for evaluating the target density in Markov chain Monte Carlo, then you can use the `fixed_param` sampler which doesn’t require a `parameters` or `model` block. See for example the “\*\_tune.stan” programs in [Some Ruminations on Containment Prior Modeling](https://betanalpha.github.io/assets/case_studies/some_containment_prior_models.html). The configuration of the `fixed_param` sampler will be similar although slightly different for CmdStanPy.
