"""Design-based estimates for complex surveys (weights, strata, PSUs).

Weighted least squares point estimates with Taylor-linearized variance:
V = A^-1 [ sum_h n_h/(n_h-1) sum_j (z_hj - zbar_h)(z_hj - zbar_h)' ] A^-1
where A = X'WX and z_hj sums w_i * x_i * e_i over PSU j in stratum h.
This matches what R's survey::svyglm reports for a linear model.
Strata with a single PSU are dropped from the variance (a common 'certainty' convention).
"""
import numpy as np
import pandas as pd


def svy_lm(df, y, X_cols, weight="weight", strata="stratum", psu="psu"):
    d = df.dropna(subset=[y, weight, strata, psu] + X_cols)
    X = np.column_stack([np.ones(len(d))] + [d[c].to_numpy(float) for c in X_cols])
    w = d[weight].to_numpy(float)
    yv = d[y].to_numpy(float)
    A = X.T @ (X * w[:, None])
    Ainv = np.linalg.pinv(A)
    beta = Ainv @ (X.T @ (w * yv))
    e = yv - X @ beta
    u = X * (w * e)[:, None]
    U = pd.DataFrame(u)
    U["h"] = d[strata].to_numpy()
    U["j"] = d[psu].to_numpy()
    z = U.groupby(["h", "j"]).sum()
    meat = np.zeros((X.shape[1], X.shape[1]))
    for h, zh in z.groupby(level=0):
        n_h = len(zh)
        if n_h < 2:
            continue
        dev = zh.to_numpy() - zh.to_numpy().mean(axis=0)
        meat += n_h / (n_h - 1) * dev.T @ dev
    V = Ainv @ meat @ Ainv
    se = np.sqrt(np.diag(V))
    n_psu = len(z)
    n_strata = z.index.get_level_values(0).nunique()
    return {"beta": beta, "se": se, "n": len(d), "df": n_psu - n_strata, "names": ["const"] + X_cols}


def svy_mean(df, y, **kw):
    r = svy_lm(df, y, [], **kw)
    return r["beta"][0], r["se"][0], r["n"]
