"""Reproducible analysis for: Time to Recidivism After Release

Generated by thericerca on 2026-07-30 21:01 UTC.
Regenerate the same statistical output on the same data by running this script (or the
matching .ipynb) with the third-party libraries at the top installed.

Statement of the problem:
  This study examines how quickly released prisoners are re-arrested and which baseline factors — including a randomized financial-aid intervention — influence the timing of re-arrest.

Objectives:
  - To describe the weeks_to_arrest and arrested indicator among the released prisoners.
  - To estimate survival curves for the financial_aid vs no-aid groups (Kaplan-Meier).
  - To compare recidivism-free time between the financial_aid groups (log-rank).
  - To model how age and prior offenses predict recidivism time (Cox regression).

"""

# Optional libraries used below (install with: pip install lifelines).
import pandas as pd
import numpy as np
from scipy import stats
import statsmodels.api as sm
import statsmodels.formula.api as smf
from lifelines import KaplanMeierFitter
from lifelines.statistics import logrank_test, multivariate_logrank_test
from lifelines import CoxPHFitter


# Load the dataset. Point this at your local copy of 'recidivism.csv'.
df = pd.read_csv('recidivism.csv')
print(f'Loaded {len(df)} rows, {len(df.columns)} columns.')
df.head()


# ------------------------------------------------------------------------------
# [1/7] Descriptive Statistics
#   Question: To describe the weeks_to_arrest and arrested indicator among the released prisoners.
# ------------------------------------------------------------------------------
cols = ['weeks_to_arrest']
print(df[cols].describe(include='all').T)


# ------------------------------------------------------------------------------
# [2/7] Frequency & Percentage Distribution
#   Question: To describe the weeks_to_arrest and arrested indicator among the released prisoners.
# ------------------------------------------------------------------------------
# Frequency & percentage distribution for each categorical variable.
vc = df['arrested'].value_counts(dropna=False)
print(pd.DataFrame({'n': vc, 'pct': (vc / vc.sum() * 100).round(2)}))


# ------------------------------------------------------------------------------
# [3/7] Kaplan–Meier Survival Estimate
#   Question: To estimate survival curves for the financial_aid vs no-aid groups (Kaplan-Meier).
# ------------------------------------------------------------------------------
kmf = KaplanMeierFitter()
for name, sub in df.groupby('financial_aid'):
    kmf.fit(durations=sub['weeks_to_arrest'], event_observed=sub['arrested'], label=str(name))
    print(name, 'median survival:', kmf.median_survival_time_)


# ------------------------------------------------------------------------------
# [4/7] Log-Rank Test
#   Question: To estimate survival curves for the financial_aid vs no-aid groups (Kaplan-Meier).
# ------------------------------------------------------------------------------
result = multivariate_logrank_test(df['weeks_to_arrest'], df['financial_aid'], df['arrested'])
print(f'Log-rank chi2 = {result.test_statistic:.3f}, p = {result.p_value:.4g}')


# ------------------------------------------------------------------------------
# [5/7] Cox Proportional Hazards Regression
#   Question: To estimate survival curves for the financial_aid vs no-aid groups (Kaplan-Meier).
# ------------------------------------------------------------------------------
sub = df[['weeks_to_arrest', 'arrested', 'financial_aid']].dropna()
cph = CoxPHFitter()
cph.fit(sub, duration_col='weeks_to_arrest', event_col='arrested')
cph.print_summary()


# ------------------------------------------------------------------------------
# [6/7] Kaplan–Meier Survival Estimate
#   Question: To model how age and prior offenses predict recidivism time (Cox regression).
# ------------------------------------------------------------------------------
kmf = KaplanMeierFitter()
kmf.fit(durations=df['weeks_to_arrest'], event_observed=df['arrested'])
print('Median survival:', kmf.median_survival_time_)
print(kmf.survival_function_.head())


# ------------------------------------------------------------------------------
# [7/7] Cox Proportional Hazards Regression
#   Question: To model how age and prior offenses predict recidivism time (Cox regression).
# ------------------------------------------------------------------------------
sub = df[['weeks_to_arrest', 'arrested', 'age', 'prio']].dropna()
cph = CoxPHFitter()
cph.fit(sub, duration_col='weeks_to_arrest', event_col='arrested')
cph.print_summary()


# ------------------------------------------------------------------------------
# End of thericerca reproducible analysis.
# ------------------------------------------------------------------------------
