"""Case 001: controlled CFA nonconvergence, NumPy + SciPy.
Download case-001-shared-data.csv beside this script. Run with Python.
Fits the same three-factor marker-variable CFA covariance model by normal ML.
This compact educational implementation is not a general SEM package.
Iteration counts/gradients differ from lavaan because optimizers differ.
"""
import csv
from pathlib import Path
import numpy as np
import scipy
from scipy.optimize import minimize

with Path(__file__).with_name('case-001-shared-data.csv').open() as f:
    rows = list(csv.DictReader(f))
data = np.array([[float(r[f'x{i}']) for i in range(1, 10)] for r in rows])
S = np.cov(data, rowvar=False, bias=True)  # normal-ML covariance, divisor N

def matrices(p):
    load = np.zeros((9, 3))
    for k in range(3):
        load[3*k, k] = 1  # marker loading identifies each factor scale
        load[3*k+1:3*k+3, k] = p[2*k:2*k+2]
    L = np.zeros((3, 3))
    L[np.tril_indices(3)] = p[6:12]
    phi = L @ L.T
    residual = np.exp(p[12:21])
    return load @ phi @ load.T + np.diag(residual), phi, residual

def objective(p):
    sigma, _, _ = matrices(p)
    sign, logdet = np.linalg.slogdet(sigma)
    if sign <= 0:
        return 1e20
    return logdet + np.trace(np.linalg.solve(sigma, S))

start = np.r_[np.repeat(.8, 6), [1, 0, 1, 0, 0, 1], np.log(np.repeat(.5, 9))]
options = dict(ftol=1e-14, gtol=1e-7, maxls=50)
failed = minimize(objective, start, method='L-BFGS-B', jac='3-point',
                  options={**options, 'maxiter': 1})
refit = minimize(objective, start, method='L-BFGS-B', jac='3-point',
                 options={**options, 'maxiter': 2000})
sigma, phi, residual = matrices(refit.x)
print('NumPy', np.__version__, 'SciPy', scipy.__version__)
print('Limited run:', failed.success, 'iterations:', failed.nit, failed.message)
print('Refit:', refit.success, 'iterations:', refit.nit, refit.message)
print('Largest numerical gradient:', np.max(np.abs(refit.jac)))
print('Smallest latent covariance eigenvalue:', np.linalg.eigvalsh(phi).min())
print('Smallest residual variance:', residual.min())
print('Do not interpret the failed run. The iteration cap is artificial.')
print('Positive variances are enforced here; this is not lavaan post.check().')
assert not failed.success and failed.nit == 1
assert refit.success and np.max(np.abs(refit.jac)) < 1e-4
assert np.linalg.eigvalsh(phi).min() > 0 and residual.min() > 0
assert refit.fun < failed.fun
print('All checks passed.')
