#!/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 14 — Nhóm 5.2 Dữ liệu bảng động
Bài giảng: B065 — Ước lượng GMM sai phân dữ liệu bảng động Arellano–Bond
Tên quốc tế: Arellano-Bond
Mã mô hình : arellano_bond
Phần mềm : Python 3.11+ — Thư viện: pydynpd / linearmodels
Dữ liệu   : vietlod_arellano_bond_ecodata.csv
Kiểm định : Kiểm định tự tương quan bậc 2 của phần dư sai phân Arellano–Bond AR bậc 2 yêu cầu $p \gt 0.05$, kiểm định Hansen $J$ cho tính hợp lệ của biến công cụ và quy tắc số công cụ nhỏ hơn hoặc bằng số nhóm $N$
"""

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_arellano_bond_ecodata.csv"
if not os.path.exists(DATA_FILE):
    DATA_FILE = "https://sachktl.tnsai.vn/src/ecoaz/data/vietlod_arellano_bond_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 B065 — Ước lượng GMM sai phân dữ liệu bảng động Arellano–Bond
import pandas as pd
df = pd.read_csv('vietlod_arellano_bond_ecodata.csv')
print(df.describe())
# abond('y L1.y x1 x2 | gmm(y, 2:4) | iv(x1 x2)', df, ['id', 't'])

print("Hoan tat uoc luong mo hinh B065 (arellano_bond) tren Python 3.11+.")
