#!/usr/bin/env python3
# Usage: python3 hw3q3.py <X_file.dat> <y_file.dat>

# No other libraries are allowed
import numpy as np
import sys

def load_data(X_file, y_file):
    '''
    Load logistic regression data from .dat files.
    You do not need to change this function.

    Parameters:
    X_file (str): Path to the .dat file containing feature matrix X, shape (n_samples, 2).
    y_file (str): Path to the .dat file containing response vector y, shape (n_samples,).

    Returns:
    X (np.array): Feature matrix, shape (n_samples, 2).
    y (np.array): Label vector, shape (n_samples,), with values in {-1, 1}.
    '''
    X = np.loadtxt(X_file)
    y = np.loadtxt(y_file)
    return X, y

def logistic_regression(X, y, lr, stop_criteria):
    '''
    Perform logistic regression using gradient ascent.

    Parameters:
    X (np.array): Feature matrix, shape (n_samples, 2).
    y (np.array): Label vector, shape (n_samples,).
    lr (float): Learning rate.
    stop_criteria (float): Stop when the ratio of change in log-likelihood to previous log-likelihood is below this threshold.

    Returns:
    beta_history: shape (n_iterations, 3) array of beta coefficients at each iteration. beta_history[-1, :] is the final beta.
    grad_history: shape (n_iterations, 3) array of gradient values at each iteration.
    ll_history: shape (n_iterations,) array of log-likelihood values at each iteration.
    '''
    # TODO: Implementation goes here
    beta_history, grad_history, ll_history = None, None, None

    return beta_history, grad_history, ll_history

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("Usage: python3 hw3q3.py <X_file.dat> <y_file.dat>")
        sys.exit(1)
    X_file = sys.argv[1]
    y_file = sys.argv[2]
    X, y = load_data(X_file, y_file)
    lr = None  # TODO: Set learning rate
    stop_criteria = None  # TODO: Set stopping criteria
    beta_history, grad_history, ll_history = logistic_regression(X, y, lr, stop_criteria)

    # Don't change the code below this line
    np.savetxt('beta_history.dat', beta_history, header='beta0 beta1 beta2')
    np.savetxt('grad_history.dat', grad_history, header='grad0 grad1 grad2')
    np.savetxt('ll_history.dat', ll_history, header='ll')

    # Print the final beta coefficients
    print(f"Done. Converged in {len(ll_history)} iterations.")
    print(f"Final beta coefficients: {beta_history[-1, :]}")
    print(f"Final Log-Likelihood: {ll_history[-1]:.4f}")

