# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# https://discourse.pymc.io/t/nan-occured-in-optimization-in-a-vonmises-mixture-model/1296

# +
# %pylab inline

import pymc3 as pm
import pymc3.distributions.transforms as tr
# -

with pm.Model() as model:
    mu_1 = pm.VonMises('mu_1', mu=0, kappa=1)
    kappa_1 = pm.Gamma('kappa_1', 1, 1)
    vm_1 = pm.VonMises.dist(mu=mu_1, kappa=kappa_1)
    w = pm.Dirichlet('w', np.ones(2))
    vm_comps = [vm_1, vm_1]
    vm = pm.Mixture('vm', w, vm_comps)
    
    approx = pm.fit(obj_optimizer=pm.adagrad_window(learning_rate=1e-3))

model.check_test_point()

with pm.Model() as model1:
    mu_1 = pm.VonMises('mu_1', mu=0, kappa=1)
    kappa_1 = pm.Gamma('kappa_1', 1, 1)
    vm_1 = pm.VonMises.dist(mu=mu_1, kappa=kappa_1)
    w = np.ones(2)*.5 # pm.Dirichlet('w', np.ones(2))
    vm_comps = [vm_1, vm_1]
    vm = pm.Mixture('vm', w, vm_comps, transform=tr.circular)

with model1:
    approx = pm.fit(obj_optimizer=pm.adagrad_window(learning_rate=1e-3))

with pm.Model() as model2:
    mu_1 = pm.VonMises('mu_1', mu=0, kappa=1)
    kappa_1 = pm.Gamma('kappa_1', 1, 1)
    vm_1 = pm.VonMises('vm_1', mu=mu_1, kappa=kappa_1)

with model2:
    approx = pm.fit(obj_optimizer=pm.adagrad_window(learning_rate=1e-3))

with pm.Model() as model_:
    mu_1 = pm.VonMises('mu_1', mu=0, kappa=1)
    kappa_1 = pm.Gamma('kappa_1', 1, 1)
    vm_1 = pm.VonMises.dist(mu=mu_1, kappa=kappa_1)
    w = pm.Dirichlet('w', np.ones(2))
    vm_comps = [vm_1, vm_1]
    vm = pm.Mixture('vm', w, vm_comps, transform=tr.circular)
    approx = pm.fit(100000, obj_n_mc=10, 
                   obj_optimizer=pm.adagrad_window(learning_rate=1e-2)
                   )

plt.plot(approx.hist);



StarcoderdataJupyterScriptsDedupFiltered