import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib inline

import warnings
warnings.filterwarnings("ignore", category=np.VisibleDeprecationWarning)

pd.options.display.max_rows = 125

import seaborn as sns
sns.set(color_codes=True)
sns.set(rc={"figure.figsize": (16, 4)})

loandata = pd.read_csv("data/loandata.csv", low_memory=False)
loandata['year'] = pd.to_datetime(loandata['ListedOnUTC']).dt.year
loandata['yearmonth'] = pd.to_datetime(loandata['ListedOnUTC']).dt.to_period('M')
recentld = loandata[loandata['year'] > 2012]
repaid = recentld[recentld['Status'] == 'Repaid']

(loandata.shape, recentld.shape)

"""
Explanation: Data Story
The data (Bondora's loan book) can be download from: https://www.bondora.com/marketing/media/LoanData.zip
End of explanation
"""


countByYear = loandata.groupby('year').size()

plot = sns.barplot(x=countByYear.index,y=countByYear)

"""
Explanation: Number of loans per year
End of explanation
"""


t = loandata[['year', 'IncomeTotal', 'Country']]
t = t[(t['year'] > 2010) & (t['year'] < 2017)]

plot = t.groupby(['year', 'Country']).median().unstack(1).plot(kind='bar', figsize=(16, 4))

"""
Explanation: From the initial analysis we can see that the number of loans is definitely growing over time. This can be caused by a higher demand for loans or rise in popularity of Bondora.
Median salary per year and country
End of explanation
"""


plot = sns.distplot(loandata['Amount'].astype(int), bins=50)

"""
Explanation: We can see that, generally, the income of the borrowers increases over time. This is an expected behaviour as the countries, where Bondora operates, have seen an increase of average salary over the last years. 
Loan amount analysis
End of explanation
"""


plot = (loandata['Amount'] // 100 * 100).value_counts().head(30).plot(kind='bar')

"""
Explanation: List of the top 30 loan amount (round to nearest 100) with counts:
End of explanation
"""


plot = sns.violinplot(cut=0, scale="width", x="year", y="Amount", data=loandata[['Amount', 'year']])

"""
Explanation: The most common loan amount is 500 EUR with the first 13 being lower or equal to 3100 EUR.
Distributions of loan amounts over years
End of explanation
"""


plot = sns.violinplot(cut=0, scale="width", x="Country", y="Amount", data=loandata[['Amount', 'Country']])

"""
Explanation: In the first couple of years the loans were relatively much lower then in the last years. Average, minimum and maximum loan amounts increase over time.
Distribution of loan amounts per country
End of explanation
"""


pd.options.mode.chained_assignment = None  # default='warn'
t = loandata[['Amount', 'LoanDuration']]
t['LoanDuration2'] = t['LoanDuration'] // 12 * 12

plot = sns.distplot(loandata['LoanDuration'], bins=50) # remove density

"""
Explanation: Finland has the highest most frequent loan amount (about 2100 EUR) and Estonia the lowest (about 500 EUR). The shapes of the distrubtions are similar across all the countries.
Loan duration analysis
End of explanation
"""


plot = sns.violinplot(cut=0, scale="width", x="LoanDuration2", y="Amount", data=t)

"""
Explanation: Loan duration with relation to the amount
End of explanation
"""


plot = sns.violinplot(cut=0, scale="width", x="year", y="LoanDuration", data=loandata[['year', 'LoanDuration']])

"""
Explanation: There is a visible linear dependency between the amount borrowed and loan duration -- the longer the loan the higher amount borrowed.
Loan duration with relation to year of issue
End of explanation
"""


plot = sns.violinplot(cut=0, scale="width", x="year", y="LoanDuration", data=repaid[['year', 'LoanDuration']])

"""
Explanation: Over the first three years Bondora issued loans of maximum 24 months duration, but since 2013 the maximum duration is 60 months. We can see that the most popular durations in the recent years are 36 and 60 months with very few borrowers choosing durations lower than 12 months.
End of explanation
"""


p = loandata[['Age', 'NrOfDependants']]
p['DepNum'] = pd.to_numeric(loandata.NrOfDependants, errors='coerce')
plot = p.groupby('NrOfDependants').size().sort_values().plot(kind='bar')

"""
Explanation: Number of dependants vs age
End of explanation
"""


p = p.dropna().astype(int)
grid = sns.lmplot(x="Age", y="NrOfDependants", data=p, fit_reg=False, size=6, aspect=3)

"""
Explanation: More than half of the borrowers have no dependants at all with very few borrowers have more than 5 dependants.
End of explanation
"""


loandata['yearmonth'] = pd.to_datetime(loandata['ListedOnUTC']).dt.to_period('M')

plot = loandata.groupby(['yearmonth', 'Country']).size().unstack(1).sort_index(ascending=True).fillna(0).plot(figsize=(16, 5))

"""
Explanation: We can see a non linear dependency between the age of the borrower and number of the dependants, gradually increasing from the age of 18, reaching peak between 40-45, and then gradually decreasing.
Number of loans listed per year month
End of explanation
"""


plot = sns.violinplot(cut=0, scale="width", x="Amount", y="Gender", orient='h', data=recentld)

df = recentld
m = df[df['Gender'] == 0.0]
f = df[df['Gender'] == 1.0]
(m.shape, f.shape)

v = 'Amount'

m_mean = m[v].dropna().mean()
f_mean = f[v].dropna().mean()
std = df[v].dropna().std()
z = (m_mean - f_mean) / std
(m_mean, f_mean, std, m_mean - f_mean, z)

"""
Explanation: From the analysis of the loans listed per yearmonth, it is clearly visible, that Slovakian loans were listed only for a short period of time (mostly 2014) and since then borrowing from that country has been phased out.
Distribution of loan amounts for genders
End of explanation
"""


repaid['Defaulted'] = repaid['PrincipalPaymentsMade'] < repaid['Amount'] 
repaid[['Defaulted', 'PrincipalPaymentsMade', 'InterestAndPenaltyPaymentsMade', 'Amount', 'Interest']]

print(repaid.shape)
print(repaid['Defaulted'].mean())

print(repaid['PrincipalPaymentsMade'].sum() / repaid['Amount'].sum())
print(repaid['InterestAndPenaltyPaymentsMade'].sum() / repaid['Amount'].sum())
print((repaid['PrincipalPaymentsMade'].sum() + repaid['InterestAndPenaltyPaymentsMade'].sum()) / repaid['Amount'].sum())


"""
Explanation: H0: No difference between mean loan amount for female borrowers and male borrowers. H1 - there is a difference.
Historical repayment rate of principal and amount of interest with penalties
End of explanation
"""


