"""
# Bed thickness and the number of states

An HMM makes every state last a geometric number of steps, so thin beds are always the most
likely. A hidden semi-Markov model gives each state its own duration law, here a negative
binomial with a mode away from one step. `select_states` then compares models with one to six
states by AIC, BIC, ICL and cross-validated likelihood.
"""

# %%
import matplotlib.pyplot as plt
import numpy as np
import transiograms as tg

ACCENT, GRAY, HIGHLIGHT = "#0072b2", "#8c8c8c", "#d55e00"

truth = tg.HiddenSemiMarkovModel(
    [tg.Gaussian([m], 1.0) for m in (0.0, 2.5, 5.0)],
    [tg.NegativeBinomial(6.0, 0.4), tg.NegativeBinomial(4.0, 0.5), tg.ShiftedPoisson(3.0)],
    start=[0.4, 0.3, 0.3],
    transition=[[0.0, 0.6, 0.4], [0.5, 0.0, 0.5], [0.7, 0.3, 0.0]],
    max_duration=40,
    direction="down",
)
_, obs = truth.sample([400] * 8, seed=0)
print([o.shape for o in obs[:2]], "...")

# %% [markdown]
# An HMM and an HSMM with negative binomial durations, fitted to the same eight logs. The HSMM
# recovers the bed-thickness law; the HMM can only offer geometric thicknesses.

# %%
hmm = tg.HiddenMarkovModel(3, n_init=3, seed=0, direction="down", ordering="mean").fit(obs)
hsmm = tg.HiddenSemiMarkovModel(
    3, duration="negative_binomial", max_duration=40, n_init=3, seed=0, direction="down", ordering="mean"
).fit(obs)
print(f"log-likelihood: HMM {hmm.score(obs):.1f}, HSMM {hsmm.score(obs):.1f}")
print(f"BIC: HMM {hmm.bic(obs):.1f}, HSMM {hsmm.bic(obs):.1f}")
fig, axes = plt.subplots(1, 3, figsize=(12, 3.2), layout="constrained", sharey=True)
u = np.arange(1, 41)
for j, ax in enumerate(axes):
    ax.bar(u, truth.durations[j].pmf(40), color=GRAY, label="true")
    ax.plot(u, hsmm.durations[j].pmf(40), color=ACCENT, marker="o", ms=3, label="HSMM")
    a = hmm.transition[j, j]
    ax.plot(u, (1 - a) * a ** (u - 1), color=HIGHLIGHT, label="HMM (geometric)")
    ax.set(xlabel="thickness (steps)", title=f"state {j}", xlim=(0, 30))
axes[0].set_ylabel("probability")
axes[0].legend()
fig.savefig("durations.png")

# %% [markdown]
# A geometric HSMM built from an HMM reproduces it exactly, which is the M3 equivalence check:

# %%
same = tg.HiddenSemiMarkovModel.from_hmm(hmm, max_duration=400)
print("log-likelihood gap:", abs(same.score(obs) - hmm.score(obs)))
print("same Viterbi path:", all(np.array_equal(a, b) for a, b in zip(same.predict(obs), hmm.predict(obs))))

# %% [markdown]
# Choosing K on the electrofacies logs. Samples 0.1 m apart are autocorrelated, so BIC can favour
# too many states on dense logs; leave-one-hole-out likelihood is the honest check.

# %%
logs = tg.datasets.electrofacies_logs(seed=0)
x = np.column_stack([logs["GR"], logs["RHOB"]])
table = tg.select_states(x, holes=logs["HOLE_ID"], states=range(1, 7), n_init=5, seed=0, direction="down")
for k, b, cv in zip(table["states"], table["bic"], table["cv_per_step"]):
    print(f"K = {k}: BIC {b:9.1f}, held-out log-likelihood per step {cv:.3f}")
fig, (a, b) = plt.subplots(1, 2, figsize=(10, 3.4), layout="constrained")
tg.plot.selection(table, ax=a)
b.plot(table["states"], table["cv_per_step"], marker="o", color=ACCENT)
b.set(xlabel="number of states", ylabel="held-out log-likelihood per step", title="Leave one hole out")
fig.savefig("selection.png")
