#!/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: B081 — Mô hình hồi quy ngưỡng dữ liệu bảng tĩnh Hansen
Tên quốc tế: Hansen Panel Threshold Regression
Mã mô hình : panel_threshold
Phần mềm : Python 3.11+ — Thư viện: linearmodels
Dữ liệu   : vietlod_panel_threshold_ecodata.csv
Kiểm định : Kiểm định Bootstrap $F_1$ tồn tại hiệu ứng ngưỡng, vì $\gamma$ không định danh dưới $H_0$, khoảng tin cậy tỷ số hợp lý $LR_1 \le c$ cho điểm ngưỡng $\hat{\gamma}$.
"""

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_threshold_ecodata.csv"
if not os.path.exists(DATA_FILE):
    DATA_FILE = "https://sachktl.tnsai.vn/src/ecoaz/data/vietlod_panel_threshold_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 B081 — Mô hình hồi quy ngưỡng dữ liệu bảng tĩnh Hansen
import pandas as pd
df = pd.read_csv('vietlod_panel_threshold_ecodata.csv')
print(df.describe())
# PanelOLS.from_formula('y ~ x1_low + x1_high + x2 + EntityEffects', data=pdata).fit()

print("Hoan tat uoc luong mo hinh B081 (panel_threshold) tren Python 3.11+.")
