import pymc as pm
import numpy as np


def model(data):
    """
    Probabilistic model of dugong length as a function of age.
    
    Uses a von Bertalanffy growth model or similar asymptotic growth curve.
    The von Bertalanffy model is commonly used for animal growth:
    L(t) = L_inf * (1 - exp(-k * (t - t0)))
    
    Parameters:
    - L_inf: asymptotic maximum length
    - k: growth rate coefficient
    - t0: theoretical age at which length is zero (often negative)
    
    We place weakly informative priors on these parameters.
    """
    age = data["age"]
    length = data["length"]
    
    with pm.Model() as dugong_model:
        # Priors for von Bertalanffy parameters
        # L_inf: asymptotic length, must be positive. Based on data, max length is ~2.74
        # So L_inf should be somewhat larger than that.
        L_inf = pm.HalfNormal("L_inf", sigma=2.0)
        
        # k: growth rate, positive
        k = pm.HalfNormal("k", sigma=1.0)
        
        # t0: theoretical age at zero length. Can be negative.
        # We use a Normal prior centered at a small negative value with reasonable spread
        t0 = pm.Normal("t0", mu=-0.5, sigma=0.5)
        
        # Deterministic growth curve
        mu = L_inf * (1 - pm.math.exp(-k * (age - t0)))
        
        # Ensure mu is positive (should be by construction if L_inf > 0 and k > 0)
        # Add likelihood
        sigma = pm.HalfNormal("sigma", sigma=0.2)
        
        length_obs = pm.Normal("length_obs", mu=mu, sigma=sigma, observed=length)
    
    return dugong_model
