"""
Script to compare the beta values generated by scikit learn vs the ones generated by our function.
The dataset used is the Spam dataset from the book, elements of statistical learning.

"""

import numpy as np
import pandas as pd
import scipy.linalg
import sklearn.preprocessing
import sklearn.svm
import Svm


# Get the data from the Spam Dataset
spam = pd.read_table('https://web.stanford.edu/~hastie/ElemStatLearn/datasets/spam.data', sep=' ', header=None)
test_indicator = pd.read_table('https://web.stanford.edu/~hastie/ElemStatLearn/datasets/spam.traintest', sep=' ',header=None)

# Store the features and labels as an array
x = np.asarray(spam)[:, 0:-1]
y = np.asarray(spam)[:, -1]*2 - 1 

# Use the train-test split inidcator provided along with the dataset
test_indicator = np.array(test_indicator).T[0]
x_train = x[test_indicator == 0, :]
x_test = x[test_indicator == 1, :]
y_train = y[test_indicator == 0]
y_test = y[test_indicator == 1]

# Standardize the data
scaler = sklearn.preprocessing.StandardScaler()
scaler.fit(x_train)
x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)

# Compare the betas and objective values generated by scikit learn and our code
lambduh = 1
svm = Svm.Svm(max_iter = 50)
d = x_train.shape[1]
beta_init = np.zeros(d)
theta_init = np.zeros(d)
eta_init = 1/(scipy.linalg.eigh(1/len(y_train)*x_train.T.dot(x_train), eigvals=(d-1, d-1), eigvals_only=True)[0]+lambduh)
betas_fastgrad= svm.mylinearsvm(beta_init, theta_init, lambduh, x_train.T, y_train, eta_init)
n_train = len(y_train)
linear_svc = sklearn.svm.LinearSVC(penalty='l2', C=1/(2*lambduh*n_train),fit_intercept=False, tol=10e-8, max_iter=1000)
linear_svc.fit(x_train, y_train)
print('Estimated beta from sklearn:', linear_svc.coef_) 
print('Estimated beta from my code:', betas_fastgrad[-1])
print('Objective value at optimum beta from my code:', svm.objective(betas_fastgrad[-1], lambduh, x_train.T, y_train))
print('Objective value at optimum beta from sklearn:', svm.objective(linear_svc.coef_.flatten(), lambduh, x_train.T, y_train))