#!/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: B082 — Mô hình ngưỡng dữ liệu bảng động có nội sinh Seo–Shin
Tên quốc tế: Seo–Shin Dynamic Panel Threshold GMM
Mã mô hình : dynamic_panel_threshold
Phần mềm : Python 3.11+ — Thư viện: linearmodels
Dữ liệu   : vietlod_dynamic_panel_threshold_ecodata.csv
Kiểm định : Kiểm định Sup-Wald bootstrap về tính tuyến tính / tồn tại bước nhảy ngưỡng, kiểm định Hansen $J$ tính hợp lệ biến công cụ trễ, khoảng tin cậy $95\%$ cho 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_dynamic_panel_threshold_ecodata.csv"
if not os.path.exists(DATA_FILE):
    DATA_FILE = "https://sachktl.tnsai.vn/src/ecoaz/data/vietlod_dynamic_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 B082 — Mô hình ngưỡng dữ liệu bảng động có nội sinh Seo–Shin
import pandas as pd
df = pd.read_csv('vietlod_dynamic_panel_threshold_ecodata.csv')
print(df.describe())
# IVGMM.from_formula('dy ~ dx1_low + dx1_high + [dl1_y ~ l2_y]', data=df).fit()

print("Hoan tat uoc luong mo hinh B082 (dynamic_panel_threshold) tren Python 3.11+.")
