К основному содержанию
правило

statsmodels — правило для Cursor: что делает и как установить

Задаёт стандарты работы со statsmodels в Python: оформление формул, добавление константы регрессии и воспроизводимые тесты моделей.

Установить в Cursor · Вручную: положить файлы Все способы и платформы ↓
.cursor/rules/statsmodels.mdc
Открыть на GitHub

Что делает statsmodels

Правило настраивает Cursor на генерацию чистого кода со statsmodels в файлах **/*.py. Оно требует импортировать библиотеку под псевдонимами sm и smf, а данные передавать через pandas.DataFrame. Это сохраняет имена колонок в отчётах summary() и упрощает чтение спецификаций.

В инструкциях закреплены технические требования: обязательное добавление константы через sm.add_constant при работе с массивами, аннотация типов через RegressionResultsWrapper и docstring в стиле NumPy. При работе с сырыми массивами без sm.add_constant модель обучится без свободного коэффициента, что исказит оценки.

Для проверки расчётов правило требует писать тесты с фиксированным np.random.seed и сверять параметры через np.isclose. Зависимости предлагается изолировать в виртуальном окружении .venv и фиксировать в pyproject.toml.

Какие задачи решает

  • настроить линейную регрессию через формулы smf.ols и pandas
  • добавить свободный коэффициент в регрессию на массивах через sm.add_constant
  • типизировать функции обучения с помощью RegressionResultsWrapper
  • написать воспроизводимый тест коэффициентов регрессии с фиксированным сидом

Примеры запросов агенту

  • Построй OLS-регрессию на pandas DataFrame с формулой через smf.ols и выведи сводку
  • Обучи модель sm.OLS на массиве признаков, не забыв добавить свободный коэффициент
  • Напиши тест для проверки коэффициентов регрессии с фиксированным сидом через pytest

Как установить statsmodels

Вручную: положить файлы

.cursor/rules/statsmodels.mdc

Во frontmatter: description, globs, alwaysApply

По шагам, с командами для каждой платформы: Cursor rules: файлы .mdc, четыре режима и замена .cursorrules

Вопросы о statsmodels

Как установить правило statsmodels в Cursor?

Сохраните файл правила по пути .cursor/rules/statsmodels.mdc в корне вашего проекта.

Что контролирует правило statsmodels?

Оно проверяет алиасы импортов sm и smf, требует использовать pandas.DataFrame вместо голых массивов и следит за добавлением константы регрессии.

Зачем явно вызывать sm.add_constant?

Формульный интерфейс smf.ols добавляет интерсепт сам, но при передаче обычных массивов в sm.OLS константу нужно добавлять вручную через sm.add_constant(X).

Исходный текст

statsmodels.mdc · CC0-1.0
---
description: This guide provides opinionated, actionable best practices for using statsmodels in Python, focusing on code organization, common patterns, performance, and modern development workflows to ensure robust and reproducible statistical analysis.
globs: **/*.py
---
# `statsmodels` Best Practices

`statsmodels` is the definitive library for rigorous statistical modeling and inference in our AI/ML pipelines. This guide ensures consistent, high-quality, and reproducible statistical analysis across our projects.

## 1. Code Organization and Imports

Always use the recommended import aliases for clarity and consistency. Keep model specifications in dedicated, well-named scripts or notebooks.

### ✅ GOOD: Standardized Imports

```python
# For general models and datasets
import statsmodels.api as sm
import pandas as pd
import numpy as np

# For R-style formula interface
import statsmodels.formula.api as smf
```

### ❌ BAD: Inconsistent or Wildcard Imports

```python
from statsmodels.api import * # Pollutes namespace
import statsmodels as sm_lib # Non-standard alias
```

## 2. Data Handling and Model Specification

`statsmodels` excels with `pandas` DataFrames and R-style formulas. Leverage these for readability and robustness.

### 2.1. Use Pandas DataFrames

Always prepare your data as `pandas.DataFrame` objects. This ensures clear column names and simplifies formula specification.

### ✅ GOOD: Pandas DataFrame for Data

```python
import pandas as pd
import statsmodels.api as sm
import statsmodels.formula.api as smf

data = pd.DataFrame({
    'y_response': np.random.rand(100),
    'x_feature1': np.random.rand(100) * 10,
    'x_feature2': np.random.randint(0, 2, 100),
    'group': np.random.choice(['A', 'B'], 100)
})

# Use formula API for convenience
model = smf.ols('y_response ~ x_feature1 + C(x_feature2) + group', data=data).fit()
print(model.summary())
```

### ❌ BAD: Raw NumPy Arrays (unless absolutely necessary)

```python
# Avoid this unless you need fine-grained control or non-standard designs
X = np.random.rand(100, 2)
y = np.random.rand(100)
model = sm.OLS(y, X).fit() # Column names are lost in summary
```

### 2.2. Always Add a Constant (Intercept)

For most regression models, an intercept term is crucial. The formula API handles this automatically; for array-based models, explicitly add it.

### ✅ GOOD: Explicitly Adding a Constant

```python
import statsmodels.api as sm
import numpy as np

X_raw = np.random.rand(100, 2)
X = sm.add_constant(X_raw) # Adds a column of ones for the intercept
y = np.random.rand(100)

model = sm.OLS(y, X).fit()
```

### ❌ BAD: Forgetting the Constant

```python
# This model will be fitted without an intercept, which is often incorrect
X = np.random.rand(100, 2)
y = np.random.rand(100)
model = sm.OLS(y, X).fit()
```

## 3. Reproducibility and Debugging

Ensure your analyses are reproducible and easy to debug by including version information.

### ✅ GOOD: Reporting Version Information

```python
import statsmodels.api as sm

print(f"Statsmodels Version: {sm.version.full_version}")
sm.show_versions() # Crucial for dependency debugging
```

### ❌ BAD: Omitting Version Details

```python
# When reporting issues or sharing code, this makes debugging harder
# print("Model results...")
```

## 4. Type Hints for Clarity

Use type hints to improve code readability, maintainability, and enable static analysis.

### ✅ GOOD: Using Type Hints

```python
import pandas as pd
import statsmodels.api as sm
from statsmodels.regression.linear_model import RegressionResultsWrapper

def fit_linear_model(data: pd.DataFrame, formula: str) -> RegressionResultsWrapper:
    """Fits an OLS model and returns the results."""
    model = sm.formula.ols(formula, data=data)
    results = model.fit()
    return results

# Usage
df = pd.DataFrame({'y': np.random.rand(10), 'x': np.random.rand(10)})
ols_results = fit_linear_model(df, 'y ~ x')
print(ols_results.summary())
```

### ❌ BAD: Untyped Code

```python
def fit_linear_model(data, formula): # Lacks clarity on expected types
    model = sm.formula.ols(formula, data=data)
    results = model.fit()
    return results
```

## 5. Virtual Environments and Packaging

Always isolate project dependencies using virtual environments and manage them explicitly.

### ✅ GOOD: Dedicated Virtual Environments and `pyproject.toml`

```bash
# Create and activate a virtual environment
python -m venv .venv
source .venv/bin/activate # Linux/macOS
.venv\Scripts\activate # Windows

# Install dependencies
pip install pandas numpy statsmodels

# Manage dependencies with pyproject.toml (preferred) or requirements.txt
# pyproject.toml example:
# [project]
# name = "my-stats-project"
# dependencies = [
#     "pandas>=2.0",
#     "numpy>=1.24",
#     "statsmodels>=0.15"
# ]
```

### ❌ BAD: Global Package Installation

```bash
pip install pandas numpy statsmodels # Pollutes global Python environment
```

## 6. Testing Approaches

Thorough testing is non-negotiable. Ensure statistical models are robust and produce expected outcomes.

### ✅ GOOD: Unit and Integration Tests with Fixed Seeds

```python
import pandas as pd
import numpy as np
import statsmodels.api as sm
import statsmodels.formula.api as smf

def test_ols_model_coefficients():
    np.random.seed(42) # Fix seed for reproducibility
    data = pd.DataFrame({
        'y': 2 + 3 * np.random.rand(100) + np.random.normal(0, 0.5, 100),
        'x': np.random.rand(100)
    })
    model = smf.ols('y ~ x', data=data).fit()

    # Assert expected coefficients within a tolerance
    assert np.isclose(model.params['Intercept'], 2.0, atol=0.5)
    assert np.isclose(model.params['x'], 3.0, atol=0.5)

# Run tests as part of CI (e.g., pytest)
# Ensure CI pipeline includes ruff, isort, and other linters.
```

### ❌ BAD: Untested Models or Unreproducible Tests

```python
# No tests for model logic or coefficient validation
# Tests that rely on random data without a fixed seed will be flaky
```

## 7. Docstrings and Comments

Follow the NumPy/SciPy docstring standard and use concise, clear comments for complex logic.

### ✅ GOOD: NumPy-style Docstrings

```python
def analyze_sales_data(df: pd.DataFrame) -> sm.regression.linear_model.RegressionResultsWrapper:
    """
    Analyzes sales data using Ordinary Least Squares (OLS).

    Parameters
    ----------
    df : pd.DataFrame
        Input DataFrame containing 'sales', 'ad_spend', and 'promotions' columns.

    Returns
    -------
    sm.regression.linear_model.RegressionResultsWrapper
        The fitted OLS model results.

    Raises
    ------
    ValueError
        If required columns are missing from the DataFrame.
    """
    required_cols = ['sales', 'ad_spend', 'promotions']
    if not all(col in df.columns for col in required_cols):
        raise ValueError(f"DataFrame must contain columns: {required_cols}")

    model = smf.ols('sales ~ ad_spend + promotions', data=df)
    results = model.fit()
    return results
```

### ❌ BAD: Missing or Poor Docstrings

```python
def analyze_sales_data(df): # What does it do? What are inputs/outputs?
    model = smf.ols('sales ~ ad_spend + promotions', data=df)
    results = model.fit()
    return results
```

## 8. Git Workflow

Adhere to our standard Git practices for maintainability and clear history.

### ✅ GOOD: Focused Branches and Clear Commits

```bash
# Work on a single feature per branch
git checkout -b feature/add-robust-regression

# Write concise, descriptive commit messages
git commit -m "feat: Implement Huber-M-estimator for RLM" -m "Adds robust linear model with Huber loss function to handle outliers in financial data analysis."
```

### ❌ BAD: "Kitchen Sink" Branches and Vague Commits

```bash
git checkout -b dev # Generic branch
git commit -m "Update code" # Uninformative
```

© sanjeed5 · CC0-1.0 · sanjeed5/awesome-cursor-rules-mdc

Похожие правила IDE

Все правила IDE по теме «Данные и базы» →
jupyter-notebook официальный openai Создаёт чистые Jupyter-ноутбуки для экспериментов и пошаговых руководств через готовые шаблоны и вспомогательный скрипт. Помогает избежать ручной сборки сырого JSON. CX Codex +1 5 600установок shap K-Dense-AI Скилл помогает агенту вычислять и проверять объяснения предсказаний ML-моделей через библиотеку SHAP. Пригодится для аудита алгоритмов и построения графиков влияния признаков. CX Codex +1 2 000установок data-scientist davila7 Скилл помогает проводить статистический анализ, строить ML-модели и объяснять результаты. Подходит для задач от EDA до подготовки моделей к развёртыванию. 385установок statistical-power K-Dense-AI Вычисляет, сколько объектов наблюдения нужно для исследования, чтобы обнаружить значимый эффект. Помогает при подготовке протокола, гранта или pre-registration — от простых t-тестов до сложных регрессий и смешанных моделей. CX Codex +1 1 300установок Time Series Analysis aj-geddes Скилл для разбора временных рядов: тренд, сезонность, автокорреляция и прогноз на Python. Пригодится, когда надо понять данные по продажам или трафику. CX Codex +1 605установок cointegration-analysis agiprolabs Проверяет пары активов на коинтеграцию методами Engle-Granger, Johansen и Филлипса-Ульяриса, чтобы найти пары для статистического арбитража. CX Codex +1 456установок

Из того же набора240

Весь набор →
svelte Правило для Cursor задаёт, как писать компоненты Svelte 5: руны вместо старой реактивности, типизация и ключи в списках. Пригодится, если проект уже на Svelte 5. ★ 3 580звёзд репо supabase Задаёт стандарты написания SQL, моделирования схем и политик безопасности для проектов на Supabase в Cursor. ★ 3 580звёзд репо stripe Правило задаёт практики для платёжных сценариев Stripe: от Checkout Sessions и Payment Intents до обработки ошибок и вебхуков. ★ 3 580звёзд репо streamlit Правило для Cursor задаёт, как писать приложения на Streamlit: кэшировать тяжёлые операции, держать UI отдельно от логики и разбивать проект на модули. ★ 3 580звёзд репо sqlite Правило задаёт Cursor практики для схем SQLite, запросов, транзакций и защиты от SQL-инъекций. Пригодится при работе с локальной базой. ★ 3 580звёзд репо sqlalchemy Правило для Cursor задаёт стиль SQLAlchemy 2.x: типизированные модели, сессии через контекстный менеджер, select() вместо устаревшего Query. ★ 3 580звёзд репо springboot Настраивает Cursor на стандарты Spring Boot 3.x и Java 17+. Помогает генерировать чистую доменную архитектуру, DTO и обработку ошибок. ★ 3 580звёзд репо spring Правило подсказывает, как организовать Spring Boot-код, внедрять зависимости и обрабатывать типичные проблемы. Пригодится, когда агент пишет или пересматривает Java-код проекта. ★ 3 580звёзд репо

Спонсоры проекта

Компании, которые помогают держать аналитику открытой