# Chapter 42: Nonlinear Models, Regimes, Market Microstructure, and Ordered Outcomes
# Fundamentals of Python for Financial Econometrics - Nonlinear Models, Regimes, Market Microstructure, and Ordered Outcomes
# Dataset: Ceteris Lab teaching sample

# Nonlinear Models, Regimes, Market Microstructure, and Ordered Outcomes
#
# **Opening question:** What if the same lag has a different effect in calm and stressed regimes, or the observed price change is an ordered category?

# %% Cell 2
import numpy as np

rng = np.random.default_rng(23)
x = np.zeros(300)
for t in range(1, len(x)):
    phi = -1.2 if x[t-1] < 0 else 0.55
    x[t] = phi*x[t-1] + rng.normal()
print(round(x.mean(), 3), round((x < 0).mean(), 3))

# **Interpretation check:** Interpretation. The unconditional mean is positive even though neither regime includes an intercept, illustrating nonlinear asymmetry.

# %% Cell 4
import numpy as np
import pandas as pd
from statsmodels.miscmodels.ordinal_model import OrderedModel

rng = np.random.default_rng(2301)
x = rng.normal(size=700)
latent = 0.8*x + rng.normal(size=700)
y = pd.cut(latent, [-np.inf, -0.7, 0.7, np.inf], labels=[0,1,2], ordered=True)
model = OrderedModel(y, pd.DataFrame({"x": x}), distr="probit").fit(method="bfgs", disp=False)
print(round(model.params["x"], 3))
print(np.round(model.model.predict(model.params, exog=pd.DataFrame({"x": [0.0]}))[0], 3))

# **Interpretation check:** Interpretation. At x=0, the central category is most probable. The probabilities sum to one and depend on both slope and thresholds.

# %% Cell 6
import numpy as np
rng = np.random.default_rng(23)
x = np.zeros(300)
x[t] = phi*x[t-1] + rng.normal()
print(round(x.mean(), 3), round((x < 0).mean(), 3))
import numpy as np

# Verified source output
#
# ```text
# 0.726 0.263
# ```
#
# ```text
# 0.805 [0.282 0.518 0.2 ]
# ```
#
# ```text
# 0.805
# [0.282 0.518 0.2  ]
# ```
