Maximum Likelihood Estimation

Stan provides optimization algorithms which find modes of the density specified by a Stan program. Three different algorithms are available: a Newton optimizer, and two related quasi-Newton algorithms, BFGS and L-BFGS. The L-BFGS algorithm is the default optimizer. Newton’s method is the least efficient of the three, but has the advantage of setting its own stepsize.

In this example we use the CmdStan example model bernoulli.stan and data file bernoulli.data.json

The CmdStanModel class method optimize returns a CmdStanMLE object which provides properties to retrieve the estimate of the penalized maximum likelihood estimate of all model parameters:

  • column_names

  • optimized_params_dict

  • optimized_params_np

  • optimized_params_pd

In the following example, we instantiate a model and do optimization using the default CmdStan settings:

[1]:
import os
from cmdstanpy import CmdStanModel, cmdstan_path

bernoulli_dir = os.path.join(cmdstan_path(), 'examples', 'bernoulli')
stan_file = os.path.join(bernoulli_dir, 'bernoulli.stan')
data_file = os.path.join(bernoulli_dir, 'bernoulli.data.json')

# instantiate, compile bernoulli model
model = CmdStanModel(stan_file=stan_file)

# run CmdStan's otpimize method, returns object `CmdStanMLE`
mle = model.optimize(data=data_file)
print(mle.column_names)
print(mle.optimized_params_dict)
mle.optimized_params_pd
14:28:43 - cmdstanpy - INFO - Chain [1] start processing
14:28:43 - cmdstanpy - INFO - Chain [1] done processing
('lp__', 'theta')
OrderedDict([('lp__', np.float64(-5.00402)), ('theta', np.float64(0.200012))])
[1]:
lp__ theta
0 -5.00402 0.200012