<jupyter_start><jupyter_text>Testing ARMA hidden semi-Markov models<jupyter_code>import numpy as np
import seaborn as sns
import time

from types import SimpleNamespace

from bioslds import sources
from bioslds.arma import Arma, make_random_arma
from bioslds.arma_hsmm import sample_switching_models, ArmaHSMM
from bioslds.plotting import FigureManager<jupyter_output><empty_output><jupyter_text>Test `sample_switching_models`  Generate a sawtooth signal<jupyter_code>sawtooth = SimpleNamespace(
    arma1=Arma([1.0], [], bias=0.05, default_source=sources.Constant(0)),
    arma2=Arma([1.0], [], bias=-0.05, default_source=sources.Constant(0)),
    usage_seq=np.tile(np.repeat([0, 1], 20), 10),
)
sawtooth.n = len(sawtooth.usage_seq)

sawtooth.sig = sample_switching_models(
    [sawtooth.arma1, sawtooth.arma2], sawtooth.usage_seq
)
with FigureManager() as (_, ax):
    ax.plot(sawtooth.sig)<jupyter_output><empty_output><jupyter_text>Generate a noisy step signal<jupyter_code>rng = np.random.default_rng(1)
noisy_step = SimpleNamespace(
    arma1=Arma([0.8], [], bias=1.0, default_source=sources.GaussianNoise(1, scale=0.1)),
    arma2=Arma(
        [0.75], [], bias=-0.5, default_source=sources.GaussianNoise(2, scale=0.1)
    ),
    arma3=Arma(
        [0.85], [], bias=0.2, default_source=sources.GaussianNoise(3, scale=0.1)
    ),
    usage_seq=np.repeat(rng.integers(low=0, high=3, size=10), 20),
)
noisy_step.n = len(sawtooth.usage_seq)

noisy_step.sig = sample_switching_models(
    [noisy_step.arma1, noisy_step.arma2, noisy_step.arma3], noisy_step.usage_seq
)
with FigureManager() as (_, ax):
    ax.plot(noisy_step.sig, "k")
    ax.set_xlabel("time step")
    ax.set_ylabel("signal")
    
    ax2 = ax.twinx()
    ax2.plot(noisy_step.usage_seq, c="C1", ls="--")
    ax2.set_ylabel("state", color="C1")
    ax2.tick_params(axis="y", labelcolor="C1")
    ax2.spines["right"].set_color("C1")
    
    ax2.set_yticks([0, 1, 2])
    sns.despine(ax=ax2, left=True, right=False, offset=10, bottom=True)<jupyter_output><empty_output><jupyter_text>Test `ArmaHSMM`  Generate a signal with switching ARs, using minimal dwell time<jupyter_code>random_switching = SimpleNamespace(
    arma1=Arma([0.8], [], default_source=sources.GaussianNoise(1)),
    arma2=Arma([-0.5], [], default_source=sources.GaussianNoise(2)),
    n=10000,
)

random_switching.arma_hsmm = ArmaHSMM(
    [random_switching.arma1, random_switching.arma2],
    min_dwell=15,
    dwell_times=[25, 35],
)

(
    random_switching.sig,
    random_switching.u,
    random_switching.usage_seq,
) = random_switching.arma_hsmm.transform(
    random_switching.n, return_input=True, return_usage_seq=True
)
with FigureManager() as (_, ax):
    ax.plot(random_switching.sig[:100], "k")
    ax.set_xlabel("time step")
    ax.set_ylabel("signal")

    ax2 = ax.twinx()
    ax2.plot(random_switching.usage_seq[:100], c="C1", ls="--")
    ax2.set_ylabel("state", color="C1")
    ax2.tick_params(axis="y", labelcolor="C1")
    ax2.spines["right"].set_color("C1")

    ax2.set_yticks([0, 1])
    sns.despine(ax=ax2, left=True, right=False, offset=10, bottom=True)
with FigureManager(1, 2) as (_, axs):
    for i, ax in enumerate(axs):
        crt_sig = random_switching.sig[random_switching.usage_seq == i]
        ax.scatter(crt_sig[1:], crt_sig[:-1], alpha=0.05, label="actual")
        xl = ax.get_xlim()
        ax.plot(
            xl,
            random_switching.arma_hsmm.models[i].a[0] * np.asarray(xl),
            "k--",
            label="expected",
        )

        leg_h = ax.legend(frameon=False)
        for crt_lh in leg_h.legendHandles:
            crt_lh.set_alpha(1)

        ax.set_xlabel("$y_t$")
        ax.set_ylabel("$y_{t-1}$")
        ax.set_title(f"State {i}")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15402969247083775, 'fraction_numerical': 0.03340402969247084, 'mean_word_length': 4.2695530726256985, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '3108', 'license': 'MIT', 'n_tokens_mistral': 1557, 'n_tokens_neox': 1469, 'n_words': 217, 'path': 'examples/tests/example_integration_arma_hsmm.ipynb', 'repo_name': 'ttesileanu/bio-time-series', 'size': '3772'}