#!/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 2: Kinh tế lượng dữ liệu bảng và chuỗi thời gian tài chính
Buổi học : Buổi 10 — Nhóm 6.5 Chuỗi thời gian phi tuyến, điểm gãy và bong bóng
Bài giảng: B103 — Mô hình tự hồi quy chuyển tiếp trơn
Tên quốc tế: Smooth Transition Autoregressive và
Mã mô hình : star_smooth_transition
Phần mềm : Python 3.11+ — Thư viện: scipy / statsmodels
Dữ liệu   : vietlod_star_smooth_transition_ecodata.csv
Kiểm định : Kiểm định tuyến tính Teräsvirta LM bậc 3, đồng thời chọn tự động LSTAR vs ESTAR, kiểm định Eitrheim–Teräsvirta không còn tự tương quan/phi tuyến dư.
"""

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_star_smooth_transition_ecodata.csv"
if not os.path.exists(DATA_FILE):
    DATA_FILE = "https://sachktl.tnsai.vn/src/ecoaz/data/vietlod_star_smooth_transition_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 B103 — Mô hình tự hồi quy chuyển tiếp trơn
import pandas as pd
df = pd.read_csv('vietlod_star_smooth_transition_ecodata.csv')
print(df.describe())
# scipy.optimize.least_squares(lstar_residuals, theta0)

print("Hoan tat uoc luong mo hinh B103 (star_smooth_transition) tren Python 3.11+.")
