Koutarou
Koutarou

Reputation: 21

PyStan Model description

I am trying to build a simple statistical model to study both stochastic model and PyStan.
Here, this is my model description in PyStan which does not work well. Please help me.


stanexec= """
data {
            int N;
            int N_age;
            vector[N] Val;
            vector[N] Age;
    }
    parameters {
            real mu;
            vector[N_age] a_age;
            real<lower=0> s_age;
            real<lower=0> s;
    }
    model {
            a_age[1] ~ normal(-sum(a_age[2:N_age]), 0.001);
            a_age[2:N_age] ~ normal(a_age[1:(N_age-1)],s_age);    
            for (i in 1:N){
                Val[i] ~ normal(mu+a_age[Age[i]],s);
            } 
    }
    """

This script goes wrong. Other models work well without problems. So, I expect miss exists in this model description.

ValueError                                Traceback (most recent call last)
<ipython-input-4-2e995ebf95f8> in <module>
     11     thin=1,
     12     warmup=100,
---> 13     seed=1
     14 )
     15 mcmc_sample  = fit.extract(permuted=True)

~\Anaconda3\lib\site-packages\pystan\api.py in stan(file, model_name, model_code, fit, data, pars, chains, iter, warmup, thin, init, seed, algorithm, control, sample_file, diagnostic_file, verbose, boost_lib, eigen_lib, include_paths, n_jobs, **kwargs)
    426                       boost_lib=boost_lib, eigen_lib=eigen_lib,
    427                       include_paths=include_paths,
--> 428                       obfuscate_model_name=obfuscate_model_name, verbose=verbose)
    429     # check that arguments in kwargs are valid
    430     valid_args = {"chain_id", "init_r", "test_grad", "append_samples", "enable_random_init",

~\Anaconda3\lib\site-packages\pystan\model.py in __init__(self, file, charset, model_name, model_code, stanc_ret, include_paths, boost_lib, eigen_lib, verbose, obfuscate_model_name, extra_compile_args)
    221                                          verbose=verbose,
    222                                          include_paths=include_paths,
--> 223                                          obfuscate_model_name=obfuscate_model_name)
    224 
    225         if not isinstance(stanc_ret, dict):

~\Anaconda3\lib\site-packages\pystan\api.py in stanc(file, charset, model_code, model_name, include_paths, verbose, obfuscate_model_name)
    165             msg = msg.encode('ascii', 'replace')
    166         error_msg = "Failed to parse Stan model '{}'. Error message:\n{}".format(model_name, msg)
--> 167         raise ValueError(error_msg)
    168     elif result['status'] == 0:  # SUCCESS_RC is 0
    169         logger.debug("Successfully parsed Stan model '{}'.".format(model_name))

ValueError: Failed to parse Stan model 'anon_model_57b2ca24f38f5d3aa2d8b8fac976c276'. Error message:
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Container index must be integer; found type=real
 error in 'unknown file name' at line 21, column 35
  -------------------------------------------------
    19:     a_age[2:N_age] ~ normal(a_age[1:(N_age-1)],s_age);    
    20:     for (i in 1:N){
    21:         Val[i] ~ normal(mu+a_age[Age[i]],s_age);
                                          ^
    22:     }
  -------------------------------------------------

PARSER EXPECTED: <one or more container indexes followed by ']'>

Upvotes: 0

Views: 355

Answers (1)

LukasNeugebauer
LukasNeugebauer

Reputation: 1337

I know it's been a while, but maybe someone else still needs the solution. The problem you're facing is that you can only index using integers. Vectors in STAN are of type real. If you want to index using an element of Age, which is a vector, you're indexing with a real number. That's not possible. The solution is to declare Age as an integer array like so:

int Age[N];

That should solve your issue.

Upvotes: 1

Related Questions