# Story 4.2: Exécution du Modèle (Backend)

Status: review

## Story

As a system,
I want to execute the statistical model computation,
so that I can provide accurate regression results.

## Acceptance Criteria

1. **Algorithm Support:** Backend supports Ordinary Least Squares (OLS) for Linear and Logit for Logistic regression.
2. **Analysis Endpoint:** A POST endpoint `/api/v1/analysis/run-regression` accepts data, X features, Y target, and model type.
3. **Comprehensive Metrics:** Returns R-squared, Adjusted R-squared, coefficients, standard errors, p-values, and residuals.
4. **Validation:** Handles singular matrices or perfect collinearity without crashing (returns 400 with explanation).
5. **Clean Data Source:** Respects user row exclusions during calculation.

## Tasks / Subtasks

- [x] **Dependency Update** (AC: 1)
  - [x] Add `statsmodels` to the backend using `uv`.
- [x] **Regression Engine** (AC: 1, 3, 4)
  - [x] Implement `run_linear_regression(df, x_cols, y_col)` in `backend/app/core/engine/stats.py`.
  - [x] Implement `run_logistic_regression(df, x_cols, y_col)` in `backend/app/core/engine/stats.py`.
- [x] **API Endpoint** (AC: 2, 5)
  - [x] Implement `POST /api/v1/analysis/run-regression` in `analysis.py`.

## Dev Notes

- **Statistics:** Using `statsmodels.api` for high-quality, research-grade regression summaries.
- **Robustness:** Added intercept (constant) automatically to models. Implemented basic median-splitting for Logistic target encoding if not strictly binary.
- **Validation:** Integrated try/except blocks to catch linear algebra errors (e.g. non-invertible matrices) and return meaningful error messages.

### Project Structure Notes

- Modified `backend/app/core/engine/stats.py`.
- Updated `backend/app/api/v1/analysis.py` with the execution endpoint.
- Added regression test case in `backend/tests/test_analysis.py`.

### References

- [Source: epics.md#Story 4.2]
- [Source: architecture.md#Computational Workers]

## Dev Agent Record

### Agent Model Used

{{agent_model_name_version}}

### Completion Notes List
- Integrated `statsmodels` for advanced statistical modeling.
- Developed a unified regression engine supporting Linear and Logistic models.
- Implemented `/api/v1/analysis/run-regression` endpoint returning detailed metrics and residuals for plotting.
- Verified with automated tests for both model types.

### File List
- /backend/app/core/engine/stats.py
- /backend/app/api/v1/analysis.py
- /backend/tests/test_analysis.py
- /backend/pyproject.toml
- /backend/uv.lock