# 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')
bernoulli_path = os.path.join(bernoulli_dir, 'bernoulli.stan')
bernoulli_data = os.path.join(bernoulli_dir, 'bernoulli.data.json')

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

# run CmdStan's otpimize method, returns object CmdStanMLE
mle = bernoulli_model.optimize(data=bernoulli_data)
print(mle.column_names)
print(mle.optimized_params_dict)
mle.optimized_params_pd

INFO:cmdstanpy:found newer exe file, not recompiling

('lp__', 'theta')

[1]: