Skip to content

symphony_dt.models

symphony_dt.models

Model registry, CmdStan location and compilation.

pd_v2 is registered at import. Its Stan files are checked against their sha256 when symphony_dt.models.pd_v2 is imported, and again by get_model. New models are registered beside it with their own name and digest; the frozen files are never edited. pd_bb, an EXPERIMENTAL discrete observation model, is registered the same way.

ModelConfig

Bases: BaseModel

The switches and grid settings of one pd_v2 arm.

censored gives each endpoint the tobit density (1) or the truncated density (0). state_dependent_sigma is A01, ledd_in_mu_U is A02 and sigma_xbar is the A01 centring constant. Use ModelConfig.primary() for the study's primary arm.

Validation: VALIDATED. The switch values of primary() and a01_sensitivity() are the study's.

primary classmethod

primary() -> ModelConfig

The study's primary arm: A01 off, A02 on, A03 on for MoCA only.

Validation: VALIDATED.

a01_sensitivity classmethod

a01_sensitivity() -> ModelConfig

The study's pre-declared sensitivity arm: the primary arm with A01 switched on.

Validation: VALIDATED.

hyperparameters

hyperparameters() -> dict[str, Any]

Return the prior hyperparameters this configuration selects.

Validation: VALIDATED.

switches

switches() -> dict[str, Any]

Return the switch entries of the Stan data block.

Validation: VALIDATED.

ModelSpec dataclass

A registered model: its Stan programs, their digests and its model card.

programs maps a role (model, forecast_gq, conditional) to a Stan file. digests maps the same roles to sha256 values, checked by verify.

Validation: TESTED.

verify

verify() -> None

Check every program against its digest; raise ModelIntegrityError on a mismatch.

Validation: TESTED.

program

program(role: str = 'model') -> Path

Return the Stan file for role, or raise KeyError naming the roles.

Validation: TESTED.

register_model

register_model(
    spec: ModelSpec, *, replace: bool = False
) -> None

Add a model to the registry. Registering an existing name raises unless replace is set.

pd_v2 can never be replaced.

Validation: TESTED.

get_model

get_model(name: str = 'pd_v2') -> ModelSpec

Return a registered model after checking its digests.

Validation: TESTED.

list_models

list_models() -> list[str]

Return the registered model names.

Validation: TESTED.

model_card

model_card(name: str = 'pd_v2') -> str

Return the model card of a registered model as Markdown text.

Validation: TESTED.

cache_dir

cache_dir() -> Path

Return the directory where compiled models are kept.

SYMPHONY_DT_CACHE overrides the default ~/.cache/symphony-dt.

Validation: TESTED.

cmdstan_path

cmdstan_path() -> Path

Locate CmdStan: the CMDSTAN variable, then cmdstanpy's default, then ./.cmdstan.

Raises CmdStanNotFoundError with the install command when none is found.

Validation: TESTED.

cmdstan_version

cmdstan_version() -> str | None

Return the CmdStan version string, or None when CmdStan is not installed.

Validation: TESTED.

compile_model

compile_model(
    name: str = "pd_v2",
    program: str = "model",
    *,
    force: bool = False,
    threads: bool = True,
) -> Any

Compile a registered program into the cache and return a cmdstanpy.CmdStanModel.

The Stan file is copied into a cache folder named by its digest, so a compiled executable is reused across sessions and never written into the installed package.

Validation: TESTED.