%matplotlib inline
import pandas
import wbdata
import matplotlib.pyplot as plt      
import seaborn as sns

"""
Explanation: Democracy and Economic Dvelopment
Michelle Sabbagh
Data Bootcamp Final Project
I. Overview
Central Research Question: What is the relationship, if any, between a country’s level of freedom and economic development? 
In academically incorrect terms, the question could be phrased as: What is the relationship, if any, between a country’s level of democracy and economic development? I am intentionally avoiding the term democracy because it is usually measured by degrees of electoral rights. Many countries host rigged elections; presenting a flaw in the democracy variable. For example, North Korea would be considered a democracy because they host elections—with only one candidate on the ballot. To prevent pseudo-democracies from skewing my results the question is asked in terms of freedom and development. 
Since the collapse of communism, the democratization trend has been en vogue globally. One of the main arguments for democratizing governments is that democracies allow for increased economic development. However, with the rise of oil rich authoritarian regimes the hypothesis of democracy yielding high levels of economic development is subject to question.
My project will unpack the correlation between economic development and freedom by grappling with the following question: Do countries with a higher degree of freedom experience greater economic development? 
Levels of freedom will be measured by a country’s civil liberties and political rights score, provided by Freedom House. Economic development will be measured in GDP per capita by the World Bank’s World Development Indicators.
II. Accessing World Bank Data
The World Bank releases their data on 264 countries' GDP per capita from the years 1960-2016. 
Step One: Install World Bank Data
Use pip to install wbdata, a library for getting data from the Wolrd Bank, by typing "pip install wbdata" into your terminal. Ensure all necessary packages, pandas, seaborn, matplotlib are installed as well.
End of explanation
"""


indicators = {'NY.GDP.PCAP.CD':"GDP Per Capita"}
economic_df = wbdata.get_dataframe(indicators=indicators)

"""
Explanation: Step Two: Read World Bank Data
End of explanation
"""


economic_df.tail(15)

"""
Explanation: Step Three: View World Bank Data
End of explanation
"""


economic_df.xs('2015', level='date')

"""
Explanation: Access just one level of the multi-index
End of explanation
"""


economic_df.shape

"""
Explanation: Data Description
There are 15,048 rows and 1 column in the World Bank dataset
End of explanation
"""


economic_df.columns

"""
Explanation: The variables included in this dataset are:
End of explanation
"""


economic_df.describe()

"""
Explanation: Descriptive statistics on the dataset:
End of explanation
"""


freedomhouse_df = pandas.read_excel("fh1972_20161.xlsx", header=0, sheetname=1).set_index('country').rename(columns={"year": "date"})

"""
Explanation: III. Accessing Freedom House Data
Freedom House is an international watchdog organization that releases a civil liberties and political rights evaluation for 195 countries. Each country is given a score from 1 to 7 , with 1 being the most free and 7 being the least free. Amanda B. Edgell, a PhD candidate in Comparative Politics at the University of Florida released a version of Freedom House's data which also includes scale inverted, averages, minimum, and maximums,correlates of war, state abbreviations, and state codes.
The data was gathered from: https://acrowinghen.files.wordpress.com/2014/03/fh1972_20161.xlsx
Step One: Read Freedom House Data
End of explanation
"""


freedomhouse_df.head(15)

"""
Explanation: Step Two: View Freedom House Data
End of explanation
"""


freedomhouse_df.shape

"""
Explanation: Data Description
There are 7,843 rows and 18 columns in the Freedom House dataset
End of explanation
"""


freedomhouse_df.columns

"""
Explanation: The variables included in this dataset are:
End of explanation
"""


freedomhouse_dd = pandas.read_excel("fh1972_20161.xlsx", header=0, sheetname=0)

freedomhouse_dd.head(30)

"""
Explanation: The table below provides a data dictionary of the aforementioned variables
End of explanation
"""


freedomhouse_df.describe()

"""
Explanation: Descriptive statistics on the dataset
End of explanation
"""


combined_df = economic_df.xs('2015', level='date').join(freedomhouse_df[freedomhouse_df["date"] == 2015]).dropna()

combined_df

# Descriptive statistics on the merged World Bank Data and Freedom House data in 2015
combined_df.describe()

"""
Explanation: IV. Analytics Prep
In order to run the necessary analysis, I combined the World Bank Data and Freedom House datasets. To reflect the most up to date information I will only use data from 2015.
End of explanation
"""


#I sorted the data by the highest level of GDP per Capita. Luxembourg had the highest GDP per capita in 2015
combined_df.sort_values('GDP Per Capita', ascending=False)

"""
Explanation: V. Case Study: How Does the Wealthiest Country, Luxembourg, Change Over Time?
End of explanation
"""


date_indexed_df = economic_df.loc["Luxembourg"]
date_indexed_df.index = date_indexed_df.index.map(int)
lux_df = date_indexed_df.join(freedomhouse_df.loc["Luxembourg"].set_index('date')).sort_index()

lux_df.plot(y=['mean'], title='Luxembourg Freedom Over Time' ) 
lux_df.plot(y=['GDP Per Capita'], title='Luxembourg GDP per capita Over Time')

"""
Explanation: I was interested in looking at the richest country's GDP per capita overtime, compared to its level of freedom. The first graph indicates that Luxembourg became free in the late 1970s. Looking at the second graph, that is exactly when Luxembourg started to experience an increase in GDP per capita. In the vacuum of this case study,one would quickly conclude that democracy directly contributed to Luxembourg’s economic development.
End of explanation
"""


#This graph demostrates decreasing freedoms and their GDP per capita.
combined_df.sort_values("mean").plot(y="GDP Per Capita", kind="line",title='Mean of Political Rights and Civil Liberties Score', figsize=(10,5))

"""
Explanation: VI. Broad Analysis: Comparing GDP Per Capita and Levels of Freedom
I sorted the data by the average of the political rights score and the civil liberties score from countries with high freedom levels to countries with low freedom levels. The left-hand side on the x axis represents countries with high levels of freedom and as you move to the right, the levels of freedom decrease. It is interesting to note that the GDP per capita line is highest in areas with high levels of freedom, and lowest in countries with low levels of freedom. However, both sides of the graph experience extreme outliers. Demonstrating that as a general trend it seems that countries with higher levels of freedom do in fact experience higher levels of economic development. Yet, the variation and outliers in the graph cause us to understand that the correlation between the two variables is not directly linear and making the claim "democracies bring about economic development" is a strong broad generalization.
End of explanation
"""


#These graphs compare decreasing political rights and civil liberties and their gdp per capita.
combined_df.sort_values("pr").plot(y="GDP Per Capita", kind="line",title='Political Rights Score', figsize=(10,5))
combined_df.sort_values("cl").plot(y="GDP Per Capita", kind="line",title='Civil Liberties Score', figsize=(10,5))

"""
Explanation: I was curious to see if the same graph would look any different if I plotted the actual civil liberties score and the political rights score. The graphs are similar to the graph of the mean above. Concluding that even when you separate civil liberties and political rights the conclusion from the first graph is very similar.
End of explanation
"""


f, ax = plt.subplots(figsize=(15, 10))
sns.violinplot(x="status", y="GDP Per Capita", data=combined_df, palette="Set3")

"""
Explanation: The graph below depicts the densest GDP per capita’s divided by their freedom status. It seems that only "free" countries have GDP's over the one hundred thousand mark, but the range in the dataset is largest in free countries. Furthermore, with a mean GDP per capita of around $ 12,851, it makes sense that the density of all the voilins is highest in that range. Notably, partly free countries experience the lowest levels of economic development, even lower than those of not free countries. Moreover, partly free countries have the smallest range in GDP per capita, mostly concentrated in the zero to ten thousand range.
End of explanation
"""


f, ax = plt.subplots(figsize=(15, 10))
sns.violinplot(x="mean", y="GDP Per Capita", data=combined_df, palette="Set3")

sns.jointplot(x='GDP Per Capita', y="mean", data=combined_df, kind='kde', )

"""
Explanation: These two density plots show something very similar, more free countries have outliers that are extremely rich, something you don't see in the less free countries. The first graph divides the density of the dataset by the average freedom score. Further indicating that the largest range of GDP per capita is in the free countries. Evidently, extreme wealth is only found in democratic countries even though low levels of economic development can occur in democracies as well.
End of explanation
"""


# Regression showing a decrease in GDP per capita with increasing mean(decreasing freedom).
sns.regplot(x='mean', y="GDP Per Capita", data=combined_df,)

"""
Explanation: After running the regression below, I can conclude that for the most part, a decrease in GDP per capita is associated with a decrease of freedom. Yet the relatively wealthy, not free countries are glaring outliers that cannot be ignored.
End of explanation
"""


