#!/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 12 — Nhóm 4.4 Mô hình nghiệm góc và tỷ trọng
Bài giảng: B055 — Hồi quy Beta cho biến tỷ trọng liên tục
Tên quốc tế: Beta Regression for Continuous Proportions
Mã mô hình : beta_regression
Phần mềm : Python 3.11+ — Thư viện: statsmodels
Dữ liệu   : vietlod_beta_regression_ecodata.csv
Kiểm định : Pseudo-$R^2$ Ferrari & Cribari-Neto, kiểm định LR độ phân tán thay đổi — $\phi_i$ constant vs varying , phần dư chuẩn hóa trọng số2, AME.
"""

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_beta_regression_ecodata.csv"
if not os.path.exists(DATA_FILE):
    DATA_FILE = "https://sachktl.tnsai.vn/src/ecoaz/data/vietlod_beta_regression_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 B055 — Hồi quy Beta cho biến tỷ trọng liên tục
import pandas as pd
df = pd.read_csv('vietlod_beta_regression_ecodata.csv')
print(df.describe())
# BetaModel(df['y_prop'], sm.add_constant(df[['x1', 'x2']])).fit()

print("Hoan tat uoc luong mo hinh B055 (beta_regression) tren Python 3.11+.")
