Skip to content

anypinn.catalog.lotka_volterra

ALPHA_KEY = 'alpha' module-attribute

BETA_KEY = 'beta' module-attribute

DELTA_KEY = 'delta' module-attribute

GAMMA_KEY = 'gamma' module-attribute

X_KEY = 'x' module-attribute

Y_KEY = 'y' module-attribute

LotkaVolterraDataModule

Bases: PINNDataModule

DataModule for Lotka-Volterra inverse problem. Generates synthetic data via odeint.

Source code in src/anypinn/catalog/lotka_volterra.py
class LotkaVolterraDataModule(PINNDataModule):
    """DataModule for Lotka-Volterra inverse problem. Generates synthetic data via odeint."""

    def __init__(
        self,
        hp: ODEHyperparameters,
        gen_props: ODEProperties,
        noise_frac: float = 0.0,
        validation: ValidationRegistry | None = None,
        callbacks: Sequence[DataCallback] | None = None,
    ):
        super().__init__(hp, validation, callbacks)
        self.gen_props = gen_props
        self.noise_frac = noise_frac

    @override
    def gen_data(self, config: GenerationConfig) -> tuple[Tensor, Tensor]:
        """Generate synthetic Lotka-Volterra data using odeint + Gaussian noise."""

        def lv_ode(t: Tensor, y: Tensor) -> Tensor:
            return self.gen_props.ode(t, y, self.gen_props.args)

        t = config.x

        sol = odeint(lv_ode, self.gen_props.y0, t)  # [T, 2]
        x_true = sol[:, 0].clamp_min(0.0)
        y_true = sol[:, 1].clamp_min(0.0)

        x_obs = x_true + self.noise_frac * x_true.abs().mean() * torch.randn_like(x_true)
        y_obs = y_true + self.noise_frac * y_true.abs().mean() * torch.randn_like(y_true)
        x_obs = x_obs.clamp_min(0.0)
        y_obs = y_obs.clamp_min(0.0)

        y_data = torch.stack([x_obs, y_obs], dim=1).unsqueeze(-1)

        return t.unsqueeze(-1), y_data

gen_props = gen_props instance-attribute

noise_frac = noise_frac instance-attribute

__init__(hp: ODEHyperparameters, gen_props: ODEProperties, noise_frac: float = 0.0, validation: ValidationRegistry | None = None, callbacks: Sequence[DataCallback] | None = None)

Source code in src/anypinn/catalog/lotka_volterra.py
def __init__(
    self,
    hp: ODEHyperparameters,
    gen_props: ODEProperties,
    noise_frac: float = 0.0,
    validation: ValidationRegistry | None = None,
    callbacks: Sequence[DataCallback] | None = None,
):
    super().__init__(hp, validation, callbacks)
    self.gen_props = gen_props
    self.noise_frac = noise_frac

gen_data(config: GenerationConfig) -> tuple[Tensor, Tensor]

Generate synthetic Lotka-Volterra data using odeint + Gaussian noise.

Source code in src/anypinn/catalog/lotka_volterra.py
@override
def gen_data(self, config: GenerationConfig) -> tuple[Tensor, Tensor]:
    """Generate synthetic Lotka-Volterra data using odeint + Gaussian noise."""

    def lv_ode(t: Tensor, y: Tensor) -> Tensor:
        return self.gen_props.ode(t, y, self.gen_props.args)

    t = config.x

    sol = odeint(lv_ode, self.gen_props.y0, t)  # [T, 2]
    x_true = sol[:, 0].clamp_min(0.0)
    y_true = sol[:, 1].clamp_min(0.0)

    x_obs = x_true + self.noise_frac * x_true.abs().mean() * torch.randn_like(x_true)
    y_obs = y_true + self.noise_frac * y_true.abs().mean() * torch.randn_like(y_true)
    x_obs = x_obs.clamp_min(0.0)
    y_obs = y_obs.clamp_min(0.0)

    y_data = torch.stack([x_obs, y_obs], dim=1).unsqueeze(-1)

    return t.unsqueeze(-1), y_data