#!/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 1: Kinh tế lượng nền tảng và mô hình dữ liệu chéo thực nghiệm
Buổi học : Buổi 18 — Nhóm 5.6 Ngưỡng và phân vị dữ liệu bảng
Bài giảng: B083 — Hồi quy phân vị dữ liệu bảng theo phương pháp moment
Tên quốc tế: Method-of-Moments Panel Quantile Regression
Mã mô hình : panel_quantile_mmqr
Phần mềm : Python 3.11+ — Thư viện: statsmodels
Dữ liệu   : vietlod_panel_quantile_mmqr_ecodata.csv
Kiểm định : Hệ số Location — $\hat{\beta}$ , hệ số Scale — $\hat{\gamma}$ , phân vị chuẩn hóa $\hat{q}$, kiểm định Wald tính bất biến theo phân vị — $\gamma = 0$ .
"""

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_panel_quantile_mmqr_ecodata.csv"
if not os.path.exists(DATA_FILE):
    DATA_FILE = "https://sachktl.tnsai.vn/src/ecoaz/data/vietlod_panel_quantile_mmqr_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 B083 — Hồi quy phân vị dữ liệu bảng theo phương pháp moment
import pandas as pd
df = pd.read_csv('vietlod_panel_quantile_mmqr_ecodata.csv')
print(df.describe())
# smf.quantreg('y_demean ~ x1_demean + x2_demean', data=df).fit(q=0.5)

print("Hoan tat uoc luong mo hinh B083 (panel_quantile_mmqr) tren Python 3.11+.")
