%pylab inline
import pandas as pd
import statsmodels.formula.api as smf
import statsmodels.api as sm
import matplotlib.pyplot as plt
import numpy as np

default=pd.read_csv("Data/Default.csv")
default.head()
default.describe()

"""
Explanation: Credit Card Defaults
 Data Points :
 Data set contains 10000 records associated with credit cards accounts.
- Defualt : Indicates whether credit card holder defualted or not.
- Student : If holder is a student.
- Balance : Card holders current outstanding balance.
- Income  : Income of the card holder.
End of explanation
"""


from sklearn.cross_validation import train_test_split

train, test = train_test_split(default, test_size=0.33, random_state=1)

# Converting data into the dataframe
train = pd.DataFrame(data=train, columns=default.columns)
test = pd.DataFrame(data=test, columns=default.columns)


train.hist()

plt.scatter(train.income, train.balance)
plt.xlim(0, 75000)
plt.ylim(0, 3000)
plt.xlabel('Income')
plt.ylabel('Balance')
plt.title('Income vs Balance')

train.plot(x='balance', y='income', kind='scatter', alpha=0.3)
plt.ylim([0,80000])
plt.xlim([0,3000])


train_default=train[train.default==1]
train_no_default=train[train.default==0]

plt.figure()

plt.scatter(train_no_default.balance, train_no_default.income, alpha=0.5, marker='+', c='g')
plt.scatter(train_default.balance, train_default.income, marker='o', edgecolors = 'r', facecolors = 'none')
plt.legend( ('No Default', 'Default'), loc='upper right')
plt.ylim([0,80000])
plt.xlim([0,3000])
plt.xlabel('Income')
plt.ylabel('Balance')

"""
Explanation: Split data into train and test data
End of explanation
"""


plt.scatter(train_default.balance, train_default.income, alpha=0.5, marker='o', c='g')
plt.scatter(train_no_default.balance, train_no_default.income, marker='+', edgecolors = 'r', facecolors = 'none')
plt.legend( ('Default', 'No Default'), loc='upper right')
plt.ylim([0,80000])
plt.xlim([0,3000])
plt.xlabel('Income')
plt.ylabel('Balance')

"""
Explanation: Plot the same graph differently
End of explanation
"""


# Logistic regression on the balance variable
bal = smf.logit(formula='default ~ balance', data=train).fit()
bal.summary()

# Get confidence interval.
bal.conf_int()

# odds ratio or Beta
# http://www.ats.ucla.edu/stat/mult_pkg/faq/general/odds_ratio.htm
np.exp(bal.params.balance)

"""
Explanation: Conclusion

Balace is more closely related to the default rate.

=============================================
            Logistic Regreassion 
=============================================
End of explanation
"""


prob = bal.predict({'balance': [1200, 2000]})
print (prob)

bal.predict({'balance': 1900})

x = np.linspace(test.balance.min(), test.balance.max(), 500)
betas = [bal.params.Intercept, bal.params.balance]

y = np.exp(betas[0] + betas[1]*x) / (1 + np.exp(betas[0] + betas[1]*x))
odds = np.exp(betas[0] + betas[1]*x)
log_odds = betas[0] + betas[1]*x

plt.plot(x, y, 'r', linewidth=2)
plt.ylabel('Probability')
plt.text(500, 0.7, r'$\frac{e^{\beta_o + \beta_1x}}{1+e^{\beta_o + \beta_1x}}$', fontsize=25)

plt.plot(x, odds, 'k', linewidth=2)
plt.ylabel('Odds')
plt.text(500, 30, r'$e^{\beta_o + \beta_1x}$', fontsize=20)


plt.plot(x, log_odds, 'c', linewidth=2)
plt.ylabel('Log(Odds)')
plt.xlabel('x')
plt.text(500, 2, r'$\beta_o + \beta_1x$', fontsize=15)

x_data = pd.DataFrame({'balance': np.linspace(test.balance.min(), 
                                              test.balance.max(),
                                              500)})
y_data = bal.predict(x_data)

plt.figure()
plt.scatter(test.balance, test.default, alpha=0.5)
plt.plot(x_data, y_data, 'r', linewidth=2)
plt.xlabel("Balance")
plt.ylabel("Probability of Default")
plt.ylim([-0.05,1.05])
plt.xlim([0, 2800])

# Draw default
plt.plot([1200, 1500], prob, 'y')

"""
Explanation: Various probability of defualt based on the balance.
End of explanation
"""


