Installer avec Codex ou Claude Copiez ce prompt, collez-le dans Codex, Claude ou un autre assistant, puis laissez-le vérifier la page du skill et l'installer pour vous.
Une commande directe contourne le prompt de vérification. Examinez la source avant de l'exécuter.
Math: Include citations to relevant papers for statistical models.
Examples: Use .. code-block:: python directive for code examples.
defexpected_purchases(self, future_t: int) -> xarray.DataArray:
"""
Compute expected number of future purchases.
Parameters
----------
future_t : int
Number of time periods to predict.
Returns
-------
xarray.DataArray
The expected number of purchases.
Examples
--------
.. code-block:: python
model = MyModel(data)
model.fit()
model.expected_purchases(future_t=12)
References
----------
.. [1] Fader, P. S., et al. (2005). "Counting Your Customers..."
"""
2. Preferred Implementations for Speed & Scalability
Vectorization over Loops
Avoid Python loops for mathematical operations. Use numpy, xarray, or pytensor (via pymc) broadcasting.
Why: Python loops are slow; vectorized operations in C/Fortran backends are orders of magnitude faster.
# Bad: Iterating over customers
results = []
for customer in customers:
results.append(calculate_val(customer))
# Good: Vectorized operation
results = alpha * np.exp(-beta * data)
Efficient PyMC Modeling
Use pm.Data for Mutable Inputs:
Allows you to change data (e.g., for out-of-sample predictions) without rebuilding the model graph.
# In build_modelself.model_coords = {"customer_id": unique_ids}
with pm.Model(coords=self.model_coords) asself.model:
# Mutable data container
x_data = pm.Data("x_data", data[cols], dims="customer_id")
...
Batch Dimensions (Coords):
Use named dimensions (dims) instead of raw shapes. This integrates with xarray for post-processing.
# Good
alpha = pm.Normal("alpha", mu=0, sigma=1, dims="channel")
HSGP for Gaussian Processes:
For time-varying parameters (like in MMM), prefer Hilbert Space Gaussian Processes (HSGP) over standard GPs. HSGP approximates the GP using basis functions, reducing complexity from $O(n^3)$ to $O(n \cdot m)$.
PyTensor for Optimization & Analysis
When implementing functionality like sensitivity analysis, optimization routines, or complex transformations, prefer pytensor operations over pure Python/NumPy.
Backend Capabilities: PyTensor graphs can be compiled to C, JAX, or MLX (Apple Silicon), enabling hardware acceleration and automatic differentiation.
Automatic Differentiation: Essential for gradient-based optimization and sensitivity analysis.
import pytensor.tensor as pt
# Good: PyTensor implementationdefsaturation(x, alpha):
return1 - pt.exp(-alpha * x)
# This graph can now be differentiated with respect to alpha
3. Class Structure & Design Patterns
Model Class Architecture
Models should inherit from base classes (MMM, CLVModel) and implement specific lifecycle methods:
__init__:
Validate inputs using validate_call or pydantic.
Store configuration (priors) in a model_config dictionary.
Do not build the PyMC model here.
build_model:
Constructs the PyMC model context.
Defines pm.Data containers and random variables.
_extract_predictive_variables:
Helper to prepare input data for predictions, converting pandas to xarray.
Configuration Management & Priors
Use a default_model_config property to define default priors. This allows users to easily override specific priors without rewriting the whole model.
Always use pymc_extras.prior.Prior for defining distributions. This provides a dictionary-based specification that is serializable and easy for users to modify.