Optimization in Stan

PyStan provides an interface to Stan’s optimization methods. These methods obtain a point estimate by maximizing the posterior function defined for a model. The following example estimates the mean from samples assumed to be drawn from normal distribution with known standard deviation:

Specifying an improper prior for \(\mu\) of \(p(\mu) \propto 1\), the posterior obtains a maximum at the sample mean. The following Python code illustrates how to use Stan’s optimizer methods via a call to optimizing:

import pystan
import numpy as np

ocode = """
data {
    int<lower=1> N;
    real y[N];
parameters {
    real mu;
model {
    y ~ normal(mu, 1);
sm = pystan.StanModel(model_code=ocode)
y2 = np.random.normal(size=20)

op = sm.optimizing(data=dict(y=y2, N=len(y2)))