# Pystan error with optimizing

**URL:** <https://discourse.mc-stan.org/t/pystan-error-with-optimizing/11236>\
**Category:** PyStan\
**Created:** [October 4, 2019, 2:57am UTC](https://discourse.mc-stan.org/t/pystan-error-with-optimizing/11236 "2019-10-04T02:57:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tuee](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tuee/32/5984_2.png) [@Tuee](https://discourse.mc-stan.org/u/Tuee)\
**Post date:** [October 4, 2019, 2:57am UTC](https://discourse.mc-stan.org/t/pystan-error-with-optimizing/11236/1 "2019-10-04T02:57:34Z")

</div>

I’m getting this error intermittenly (with simulated data, as I change sample sizes etc). What does it mean?

![image](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/5/509c30db820f22225d3c94c814bd305149207217.png)

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [October 4, 2019, 5:23am UTC](https://discourse.mc-stan.org/t/pystan-error-with-optimizing/11236/2 "2019-10-04T05:23:52Z")

</div>

Can you check the terminal (where you started jupyter)

---

<div class="post-metadata">

**Author:** ![Tuee](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tuee/32/5984_2.png) [@Tuee](https://discourse.mc-stan.org/u/Tuee)\
**Post date:** [October 8, 2019, 4:25pm UTC](https://discourse.mc-stan.org/t/pystan-error-with-optimizing/11236/3 "2019-10-08T16:25:44Z")

</div>

```
Initial log joint probability = -2.26469e+09
    Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes 
       1 -2.26469e+09 0 4.55166e+09 0.001 0.001 12   
Error evaluating model log probability: Non-finite gradient.
Error evaluating model log probability: Non-finite gradient.
Error evaluating model log probability: Non-finite gradient.
Error evaluating model log probability: Non-finite gradient.
Error evaluating model log probability: Non-finite gradient.
Error evaluating model log probability: Non-finite gradient.
Error evaluating model log probability: Non-finite gradient.
Error evaluating model log probability: Non-finite gradient.
Error evaluating model log probability: Non-finite gradient.
Error evaluating model log probability: Non-finite gradient.
Error evaluating model log probability: Non-finite gradient.

Optimization terminated with error: 
  Line search failed to achieve a sufficient decrease, no more progress can be made

```

But then running the Jupyter cell again worked:

```
Initial log joint probability = -1.49048e+07
    Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes 
Error evaluating model log probability: Non-finite gradient.
Error evaluating model log probability: Non-finite gradient.
Error evaluating model log probability: Non-finite gradient.
Error evaluating model log probability: Non-finite gradient.
Error evaluating model log probability: Non-finite gradient.
Error evaluating model log probability: Non-finite gradient.

       8 -6237.88 0.00378842 0.0742458 1 1 41   
Optimization terminated normally: 
  Convergence detected: relative gradient magnitude is below tolerance

```

---

<div class="post-metadata">

**Author:** ![Tuee](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tuee/32/5984_2.png) [@Tuee](https://discourse.mc-stan.org/u/Tuee)\
**Post date:** [October 8, 2019, 4:38pm UTC](https://discourse.mc-stan.org/t/pystan-error-with-optimizing/11236/4 "2019-10-08T16:38:07Z")

</div>

Also if it helps-- when I give pystan.optimizing an explicit seed it doesn’t seem to fail. If I don’t give it a seed, it fails roughly 1/3 of the time (observed from repeatedly re-running the same Jupyter cell).

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [October 8, 2019, 6:56pm UTC](https://discourse.mc-stan.org/t/pystan-error-with-optimizing/11236/5 "2019-10-08T18:56:31Z")

</div>

You probably have a hard model which fails with some initial values. Hard to say anything without the model code.

---

<div class="post-metadata">

**Author:** ![Tuee](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tuee/32/5984_2.png) [@Tuee](https://discourse.mc-stan.org/u/Tuee)\
**Post date:** [October 8, 2019, 9:44pm UTC](https://discourse.mc-stan.org/t/pystan-error-with-optimizing/11236/6 "2019-10-08T21:44:01Z")

</div>

Here’s the code. Data is simulated and seed will ensure reproducibility. (The error isn’t reproducable.)

```python
import numpy as np
import pystan

ocode = """
data {
    int<lower=0> N; // number of input
    vector[N] x;
    vector[N] y;
    int<lower=0> K; // number of predicted values
    vector[K] x_pred; // values we want predictions for
}
parameters {
    real alpha;
    real beta;
    real<lower=0> sigma;
}
model {
    y ~ normal(alpha +beta * x, sigma);
}
generated quantities {
    vector[K] yhat;
    for (n in 1:K) {
        yhat[n] = normal_rng(alpha +beta * x_pred[n], sigma);
    }
}
"""

sm = pystan.StanModel(model_code=ocode,model_name='ols_with_prediction')

# actual (i.e. unobservable) model parameters
alpha = 1
beta = 10
sigma = 300

# simulate the regression model
N=1000 # number of points to simulate
x_mean=0 # point in domain around which data will be centered
x_variance=100
df=20

# first, simulate the x points 
np.random.seed(42)
x_values = x_mean+np.random.standard_t(df=df, size=N)*x_variance

# now, simulate the y points
y_values = np.random.normal(loc=alpha+beta*x_values,scale=sigma)

# calculate values for predictions
K=5
variance_scale=2
x_pred_values = x_mean+np.random.standard_t(df=df, size=K)*x_variance*variance_scale
x_pred_values = np.sort(x_pred_values)
x_pred_values

# assemble data to pass to stan
stan_data = {
    'x':x_values,
    'y':y_values,
    'N':N,
    'K':K,
    'x_pred':x_pred_values,
}

### Point estimates and point predictions

op = sm.optimizing(data=stan_data)
op

```

---

<div class="post-metadata">

**Author:** ![Tuee](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tuee/32/5984_2.png) [@Tuee](https://discourse.mc-stan.org/u/Tuee)\
**Post date:** [October 22, 2019, 6:18pm UTC](https://discourse.mc-stan.org/t/pystan-error-with-optimizing/11236/7 "2019-10-22T18:18:25Z")

</div>

Just wondering if this code is helpful for isolating the problem? I’ve noticed the error only happens when I don’t pass a seed.
