#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
HỆ SINH THÁI HỌC THUẬT TNSAI — SACHKTL.TNSAI.VN & VIETLOD.TNSAI.VN
Khóa học : Khóa 3: Suy diễn nhân quả, đánh giá chính sách, kinh tế lượng không gian và sinh tồn
Buổi học : Buổi 01 — Nhóm 9.1 Ghép cặp và cân bằng mẫu
Bài giảng: B136 — Ghép cặp điểm xu hướng Rosenbaum–Rubin
Tên quốc tế: Propensity Score Matching
Mã mô hình : propensity_score_matching
Phần mềm : Python 3.11+ — Thư viện: statsmodels / scikit-learn
Dữ liệu   : vietlod_propensity_score_matching_ecodata.csv
Kiểm định : Kiểm tra miền giao thoa chung, chênh lệch chuẩn hóa trung bình các biến đồng biến sau ghép cặp, tỷ số phương sai và phân tích độ nhạy Rosenbaum Bounds
"""

import os
import warnings
import numpy as np
import pandas as pd

warnings.filterwarnings("ignore")

# 1. Nạp bộ dữ liệu thực hành chuẩn hóa
DATA_FILE = "vietlod_propensity_score_matching_ecodata.csv"
if not os.path.exists(DATA_FILE):
    DATA_FILE = "https://sachktl.tnsai.vn/src/ecoaz/data/vietlod_propensity_score_matching_ecodata.csv"

df = pd.read_csv(DATA_FILE)
print(f"Kich thuoc mau thuc hanh {DATA_FILE}: {df.shape[0]} quan sat x {df.shape[1]} bien so")

# 2. Thực thi kịch bản ước lượng và kiểm định B136 — Ghép cặp điểm xu hướng Rosenbaum–Rubin
import pandas as pd
df = pd.read_csv('vietlod_propensity_score_matching_ecodata.csv')
print(df.describe())
# smf.logit('treat ~ x1 + x2', data=df).fit()

print("Hoan tat uoc luong mo hinh B136 (propensity_score_matching) tren Python 3.11+.")
