import numpy as np
import pandas as pd
import statsmodels.formula.api as sm
from tqdm import tqdm


def backwardElimination(x, y, sl, var_names):
    """ This function is used to apply backwardElimination in order to select
    only the features which have strong correlation with the dependant variable
    :param x: the independant variables, features
    :param y: the dependant variable
    :param sl: a threshold, usually 0.05, 0.01, 0.005 or 0.0001
    :param var_names: name of features
    :returns: an array of the selected features which are correlated to y
    :rtype: np.array
    """

    numVars = x.shape[1]
    for i in tqdm(range(0, numVars)):
        regressor_OLS = sm.ols('y ~ {}'.format('+'.join(var_names)),
                               pd.DataFrame(np.hstack((x, y[:, np.newaxis])), columns=var_names + ['target_pct_vunerable'])).fit()
        maxVar = max(regressor_OLS.pvalues)
        if maxVar > sl:
            for j in range(0, numVars - i):
                if (regressor_OLS.pvalues[j] == maxVar):
                    x = np.delete(x, j, 1)
                    var_names.pop(j)

    print(regressor_OLS.summary())
    return x, var_names
