{"cells":[{"metadata":{"trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"import os\nfor root, dirs, fns in os.walk(\"/kaggle/input\"):\n    for fn in fns:\n        print(os.path.join(root, fn))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.graph_objects as go\nfrom ipywidgets import widgets\nimport plotly.express as px\nimport plotly.offline as pyo\nfrom plotly.subplots import make_subplots\nimport warnings\nwarnings.simplefilter(action='ignore', category=FutureWarning)\npd.options.mode.chained_assignment = None \n# Set notebook mode to work in offline\npyo.init_notebook_mode()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"green_indicators = pd.read_csv(\"/kaggle/input/environmentequity-starterpack/Environment-Equity Datasets/Social Equity/Government policies and support/Green Growth Indicators by country.csv\")\ncoal_us = pd.read_csv(\"/kaggle/input/extra-datasets-for-cdp/Coal Jobs US (1985-2020).csv\")\ncoal_us['Annual Mean'] = coal_us.iloc[:,1:].mean(axis=1)\nperc_energy_country = pd.read_csv(\"/kaggle/input/extra-datasets-for-cdp/renewable-share-energy.csv\")\ngini_country = pd.read_csv(\"/kaggle/input/extra-datasets-for-cdp/gini_by_country_world_bank.csv\")\npollution_region = pd.read_csv(\"/kaggle/input/environmentequity-starterpack/Environment-Equity Datasets/Social Equity/Pollution/Pollution emissions by region.csv\")\nstats_cost_country = pd.read_csv(\"/kaggle/input/environmentequity-starterpack/Environment-Equity Datasets/Social Equity/Pollution/Health and Deaths/Statistical costs of premature deaths due to environmental risks by country.csv\")\npm25_vs_pmdeaths_country = pd.read_csv(\"/kaggle/input/environmentequity-starterpack/Environment-Equity Datasets/Social Equity/Pollution/Health and Deaths/Deaths by Particulate Matter Air Pollution vs PM25 by country.csv\")\ngdp_country = pd.read_csv(\"/kaggle/input/extra-datasets-for-cdp/GDP by country2.csv\",  encoding='cp1252') \n#death_pollution_100000s_country = pd.read_csv(\"/kaggle/input/extra-datasets-for-cdp/death-rates-total-air-pollution.csv\")\nco2_emissions_country = pd.read_csv(\"/kaggle/input/environmentequity-starterpack/Environment-Equity Datasets/Social Equity/Pollution/CO2 emissions by country.csv\")\ngdp_region = pd.read_csv(\"/kaggle/input/environmentequity-starterpack/Environment-Equity Datasets/Social Equity/Equity Indicators/GDP by region.csv\")\nstate_population = pd.read_csv(\"/kaggle/input/extra-datasets-for-cdp/nst-est2019-alldata.csv\")\ngeneration_state = pd.read_csv(\"/kaggle/input/extra-datasets-for-cdp/annual_generation_state.csv\")\ngdp_country = pd.read_csv(\"/kaggle/input/extra-datasets-for-cdp/GDP by country2.csv\",encoding='cp1252')\nre_jobs_country = pd.read_csv(\"/kaggle/input/environmentequity-starterpack/Environment-Equity Datasets/Social Equity/Energy/Renewable Energy Jobs by Country.csv\")\nregion_population = pd.read_csv(\"/kaggle/input/extra-datasets-for-cdp/pop_region2.csv\",encoding='cp1252')\nrenewable_energy_stats = pd.read_csv(\"/kaggle/input/environmentequity-starterpack/Environment-Equity Datasets/Corporations/Renewable Energy Statistics (2010-2019).csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"energy_consumption_country = pd.read_csv(\"/kaggle/input/environmentequity-starterpack/Environment-Equity Datasets/Social Equity/Energy/Percentage of Energy Consumption by Country.csv\")\nglobal_investment_renew = pd.read_csv(\"/kaggle/input/extra-datasets-for-cdp/global investment cap.csv\")","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"corp_18 = pd.read_csv(\"/kaggle/input/cdp-unlocking-climate-solutions/Corporations/Corporations Responses/Climate Change/2018_Full_Climate_Change_Dataset.csv\")\ncorp_19 = pd.read_csv(\"/kaggle/input/cdp-unlocking-climate-solutions/Corporations/Corporations Responses/Climate Change/2019_Full_Climate_Change_Dataset.csv\")\ncorp_20 = pd.read_csv(\"/kaggle/input/cdp-unlocking-climate-solutions/Corporations/Corporations Responses/Climate Change/2020_Full_Climate_Change_Dataset.csv\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Introduction\n\nIn this notebook, we propose several **environmental Ginis** to measure environment-equity alignment, and discuss some of the **intersections between corporate and environmental interests** especially with regards to renewable energies.\n\nApologies for the length, you may skip to the KPIs and intersections section to view the crux of the notebook. The rest is mainly contextualization and explanation."},{"metadata":{},"cell_type":"markdown","source":"\n# Context\n\nIn the early 1800s, the first Industrial Revolution would hyper-accelerate the rate of technological  advancement and change the trajectory of human history forever. It was undoubtedly a huge boon for our development, but it did not come without its own share of growing pains. For instance, the benefits of the revolution were not equally available to everyone. The proletariat famously had to endure terrible working conditions, proffer child workers, and suffer all manner of pollution-related sicknesses and deaths. Even worse, the subjugated natives of the European colonies were the ones who had to pay the heavy price of these ravenous processes, and only got to enjoy the fruits of their labour via the breadcrumbs of their colonial masters.\n\nNow,  we are in the midst of a “Green Revolution”. And have no doubt, it is certainly underway. For instance:\n\n1. Governments are increasing funding for research on environemntal protection and subsidies for clean energies, spurred in part by the rise of climate-induced natural distasters.\n\n2. Advancements in  renewable energy have made them more attractive to business owners nad invesetors than ever before..\n\n3. Increased pressure from civil society groups on governments and business enact climate change mitigation policies. \n\nThe only question is at what speed it will occur, and whether the thousands of preventable deaths due to food shortages, extreme weather conditions, and displacement of people at coastal areas can be avoided.\n\nBut beyond the rate of uptake, we cannot neglect the other elephant in the room, which is how to perform this transition equitably. We can find a lot of parallels with the first Industrial Revolution in this sense, especially with regards to how not all groups will benefit equally from a green transition in the short term. In this notebook, we aim to explore the aspects of this inequity, and aim to propose some KPIs to healp quanitfy the progress made in achieving environmental goals, while not compromising on equity. "},{"metadata":{},"cell_type":"markdown","source":"# Objective \n\nThis notebook aims to explore all the issues above with the relevant data, and produce two actionable outcomes:\n\n1.\t**Create a set of KPIs** to allow governments to measure **effectiveness in providing environmental protection while also ensuring social equity**.  \n\n2.\tExplore if there is an i**ntersection between environment protection and corporate interests**, and proffer some **suggestions to further encourage this relationship**. "},{"metadata":{},"cell_type":"markdown","source":"# How does environmental protection create inequality? An introduction\n\n\nHere, we will explore the main arguments for environmental regulation exacerbating inequity, which are largely economical in nature. \n\n"},{"metadata":{},"cell_type":"markdown","source":"First, is the opportunity cost associated with implementing these green policies. In other words, public funds spent on green projects such as renewable energy investments  or reforestation etc. could  have been used for schemes that more directly benefit the poor, such as food subsidies and public education. This may result in a welfare loss for society if the legislation does not bring aboout a commensurate positive impact that covers the costs. \n\nFor instance, a common way of measuring the economic efficacy of environmental policies is via a marginal carbon abatement curve (MACC), which is an estimation of the volumes and costs of opportunities to reduce emissions in a given year. This allows policymakers to easily compare the relative costs and GHG emissions reductions of each course of action. Here, we see that the most effective environmental policies are also the most expensive, resulting in these funds being unable to be used for poverty-alleviating measures instead. "},{"metadata":{},"cell_type":"markdown","source":"![Carbon Abatement Curve](https://i.imgur.com/CKD8XJk.png)"},{"metadata":{},"cell_type":"markdown","source":"Next, is that negative impacts of environmental regulations fall disproportionately on the poor, in terms of employment and direct cost.\n\nFor employment, increased carbon taxation and a shift towards cleaner energies will cause a long-term decline in certain industries which largely support blue-collar workers. This is especially egregious if the workers do not have the requisite skills or opportunities to transition to new jobs in the renewable energy sector.\n\nFor instance, a hot-button topic in US politics involves the decline in manufacturing and energy jobs in the Rust Belt, supposedly spurred by \"job-killing\" environmental regulations.  \n\nWhile the decline in manufacturing jobs over the decades has more to do with cheaper labor costs overseas and US trade agreements, it is true that the energy sectors have been greatly pressured by the regulations. For instance, it has been generally agreed that the decline of the coal industry was induced by a shift towards renewable energy. "},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"\n\n\n\nus_energy = perc_energy_country.loc[perc_energy_country.Entity == 'United States']\nus_energy = us_energy[us_energy.Year.between(1985, 2019, inclusive=True)]\ncoal_us = coal_us[coal_us.Year.between(1985, 2019, inclusive=True)]\n\n\n\n\n\nfig = make_subplots(specs=[[{\"secondary_y\": True}]])\n\n# Add traces\nfig.add_trace(\n    go.Bar(x=us_energy['Year'], y=us_energy[\"Renewables (% sub energy)\"], name=\"Renewables (% energy usage)\", opacity=0.6),\n    secondary_y=False,\n)\n\nfig.add_trace(\n    go.Scatter(x=us_energy['Year'], y=coal_us['Annual Mean'], name=\"US Coal Mining Jobs (thousands)\"),\n    secondary_y=True,\n)\n\n# Add figure title\n#fig.update_layout(\n#    title_text=\"Coal Mining Jobs vs Renewables\"\n#)\n\n# Set x-axis title\nfig.update_xaxes(title_text=\"Year\")\n\n# Set y-axes titles\nfig.update_yaxes(title_text=\"Renewables (% energy usage)\", secondary_y=False)\nfig.update_yaxes(title_text=\"US Coal Mining Jobs (thousands)\", secondary_y=True)\n\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# How do environmental regulations alleviate inequality? \n\nDespite the doomsaying claims of the opponents of regulation, environmental policies can actuallly help ameliorate poverty and inequity, especially in the long term. In fact, for every academic study or journal that denounce such laws for their economic harm,  are a dozen more that conclude that the savings from improved health outcomes are more than sufficient to cover any economic pain the poor might face in the short-term.\n\nIs this true though? Climate change denialists would argue that it is all a charlatan's ruse. We will certify this with some data. \n\nFirst, we explore the idea that environmental degradation may incur huge costs for populations. Logically, one would think of the social and economic costs borne by society due to sickness and premature deaths from phenomenon such as increased pollution, unsafe water sources, lead poisoning etc. Indeed, we confirm this hypothesis by finding a strong correlation between PM2.5 readings and deaths by particulate matter air pollution. \n\n"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"pm_25_vs_pmdeaths_country = pm25_vs_pmdeaths_country.dropna()\npm_25_vs_pmdeaths_country = pm_25_vs_pmdeaths_country[pm_25_vs_pmdeaths_country.Year.between(2010,2020,inclusive=True)]\npm_25_vs_pmdeaths_country = pm_25_vs_pmdeaths_country.rename(columns={\"Deaths - Ambient particulate matter pollution - Sex: Both - Age: Age-standardized (Rate)\":\"Deaths - Ambient particulate matter pollution\"})\npm_25_vs_pmdeaths_country = pm_25_vs_pmdeaths_country.rename(columns={\"Total population (Gapminder, HYDE & UN)\":\"Total population\"})\npm_25_vs_pmdeaths_country = pm_25_vs_pmdeaths_country.rename(columns={\"PM2.5 air pollution, mean annual exposure (micrograms per cubic meter)\": \"PM2.5 air pollution (μg per cm3)\"})\nfig = px.scatter(pm_25_vs_pmdeaths_country, y=\"PM2.5 air pollution (μg per cm3)\", x=\"Deaths - Ambient particulate matter pollution\",\n                 animation_frame=\"Year\", animation_group=\"Entity\",\n          size=\"Total population\", hover_data=['Entity'], color='Entity', title=\"Pollution Level vs Death by Ambient Matter Pollution\")\nfig[\"layout\"].pop(\"updatemenus\")\nfig.show()\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"This much is obvious. However, after digging deeper, we find a more troubling trend. Countries who are the greatest contriubtors of carbon emissions are not necessarily the ones who suffer the most emissions-related deaths. In fact, we find that in general, richer economies contribute marginally more carbon emissions while suffering lower mortality,while the countries with the highest mortality tend to have lower GDP. The obvious exceptions are China and India, but these are rapidly industrializing countries with huge inequality issues of their own. So it seems logical to conclude that this is a form of environmental inequity, and any measures to alleviate it will also help in attenuating this death inequality."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"co2_emissions_country_2015 = co2_emissions_country[co2_emissions_country.Year == 2015]\nco2_emissions_country_2015 = co2_emissions_country_2015[['Code', 'Annual CO2 emissions']]\ngdp_country_small = gdp_country[['Country Name', 'Country Code', '2015']]\ngdp_country_small = gdp_country_small.rename(columns={'2015':'GDP'})\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"co2_emissions_country_2015 = co2_emissions_country[co2_emissions_country.Year == 2015]\nco2_emissions_country_2015 = co2_emissions_country_2015[['Code', 'Annual CO2 emissions']]\ngdp_country_small = gdp_country[['Country Name', 'Country Code', '2015']]\ngdp_country_small = gdp_country_small.rename(columns={'2015':'GDP'})\n\npm25_gdp_country = pm_25_vs_pmdeaths_country.join(gdp_country_small.set_index('Country Code'), on='Code', how='inner').dropna()\npm25_gdp_country = pm25_gdp_country.join(co2_emissions_country_2015.set_index('Code'), on='Code', how='inner').dropna()\nfig = px.scatter(pm25_gdp_country, y=\"Annual CO2 emissions\", x=\"Deaths - Ambient particulate matter pollution\",\n                 #animation_frame=\"year\", animation_group=\"country\",\n          size=\"GDP\", hover_data=['Entity'], color='Continent', title=\"Pollution Level vs Death by Ambient Matter Pollution (2015)\")\n\nfor i in range(len(fig['data'])):\n    fig['data'][i]['marker']['size'] = list(np.array(fig['data'][i]['marker']['size'])*3)\n\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Ironically, another way that environmental protection may help level the playing field is in terms of employment. In the same study by OECD that showed the deterioration of energy sectors due to GHG mitigation policies, they also found that the jobs created in renewable energy were not only more abudnant than those lost in energy, but that they were largely proportionate in terms of skill level. \n"},{"metadata":{},"cell_type":"markdown","source":"![Green growth job destruction and creation](https://i.imgur.com/8IBv6pM.png)"},{"metadata":{},"cell_type":"markdown","source":"\nThis means that, theoretically, given enough time, all workers displaced by the environemntal policies will find a replacement in the renwable energy sector, with more to spare. If this is commensurate with reality, than it would disable a key argument levied against these policies. "},{"metadata":{},"cell_type":"markdown","source":"# Finally, some KPIs for Environment-Equity Alignment\n\nAnd now, we have finally reached the part where we discuss some possible KPIs for measuring the relationship between reducing enviromental degradation and inequity. However, the issues we discussed above were not for naught; they will help play a key role in the formulation of these KPIs. Essentially, we want to find an alignment between environmental and equity goals; policies that help alleviate inequality more than exacerbate it. To do so, we will need to factor in all the concerns that we have listed above. "},{"metadata":{},"cell_type":"markdown","source":"We start by considering the most intuitive choice for measuring equity, which is the commmonly used Gini index. The Gini is a single-value measure of the inequality (usually income) in a population. It is defined mathematically as the ratio of the area between the Lorenz curve and the line of equality (perfectly equal scenario) over the total area under the line of equality. Thus, its value ranges from 0 (perfect equality) to 1 (perfect inequality). It is currently the de facto choice for measuring inequality in a population, largely due to its ability to capture complex income distributions in a simple number.  "},{"metadata":{},"cell_type":"markdown","source":"![GINI](https://i.imgur.com/OPJyGDI.jpg)"},{"metadata":{},"cell_type":"markdown","source":"\nA naive approach would be to plot the Gini index of a population with a proxy of enviromental performance (such as pollution readings) over time. However, this would be grossly unrepresentative, because in reality income inequality in a nation is dependent on a host of macroeconomic factors, and is in fact usually hardly impacted by environemntal policy, due to its relatively small contribution to overall GDP."},{"metadata":{},"cell_type":"markdown","source":"Therefore, we need a more nuanced approach that attempts to isolate the impact of environmental policy on equity as much as possible. So we propose an enviroment-based Ginis instead, based mainly on the factors that we had discussed above. So, the objective is for these KPIS to be able to measure inequality in **environmental health** as well as **employment created/destroyed by environmental policy**. \n"},{"metadata":{},"cell_type":"markdown","source":"## Pollution Gini\n\nLet's begin by trying to measure inequality in environmental outcomes across a nation. There are many ways to measure environmental health of a community: through level of pollution, water security,climate vulnerability etc. For this part, however, we will be mainly exploring pollution as quantified by annual PM 2.5 levels, since this is the only information that is widely available and easily understood.  \n\nPM 2.5 Levels are usually measured across a large area (eg. city/state level. Therefore, we will utilize area-based Gini coefficient (AR-Gini), where each area is treated as an a uniform group in the Gini tabulation. \n\nSince we are using a discretized version of Gini, the formula of Gini coefficient is defined as follows:\n\n$$ G = 1 - \\sum\\limits_{k=1}^{k=n-1} {(x_k - x_{k+1})(y_k + y_{k+1}) } $$\n\nwhere y is the cumulative sum of the dependent variable (eg. PM 2.5 level) in the kth state, x is the cumulative sum of the population in the kth state, k is the number of states.\n\n(Note: this is equivalent to the geometric definition we saw before, where G = A/(A+B). Since A+B = 0.5, we have G = 2A = 1 - 2B. B is the Lorenz curve and can be estimated by trapezoids defined by the x/y values, which is represented in the second term in our equation above.)\n\nLet's illustrate the concept of Environmental Gini with the USA.  We would like for our Gini to summarize the change in inequality of pollution across its population. We could achieve this in several ways. First, we could treat each state as a separate entity and calculate the Gini as if there were only that number of people. Here, we only have PM 2.5 data for 47 states, so we will be calculating it as if the US is a 47 person population. Doing so for the year 2003 yields this graph:\n"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"US_STATES = ['Alabama', 'Alaska',\n       'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut',\n       'Delaware', 'District of Columbia', 'Florida', 'Georgia', 'Hawaii',\n       'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky',\n       'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan',\n       'Minnesota', 'Mississippi', 'Missouri', 'Montana (US)', 'Nebraska',\n       'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York',\n       'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon',\n       'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota',\n       'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington']\n\ngdp_region_rev = gdp_region[gdp_region.MEAS == 'USD_PPP']\npollutions_region_rev = pollution_region[pollution_region.VAR == 'AIR_POL']\npollutions_region_rev = pollutions_region_rev.rename(columns={\"Value\":\"PM2.5 Level (µg/m³)\"})\npollutions_region_rev = pollutions_region_rev[['REG_ID','PM2.5 Level (µg/m³)', 'Year']]\nppp_pollutions_region = gdp_region_rev.join(pollutions_region_rev.set_index('REG_ID'), on='REG_ID', how='inner', lsuffix='_left', rsuffix='_right')\nppp_pollutions_region = ppp_pollutions_region[ppp_pollutions_region.Year_left == ppp_pollutions_region.Year_right].sort_values(['Region', 'Year_left'])\nregion_population = region_population.rename(columns={'Value':'Population'})[['REG_ID', 'Year', 'Population']]\nppp_pollutions_region = ppp_pollutions_region.join(region_population.set_index('REG_ID'), on='REG_ID', how='inner', lsuffix='_ka', rsuffix='_pa')\nppp_pollutions_region = ppp_pollutions_region[ppp_pollutions_region.Year == ppp_pollutions_region.Year_right].sort_values(['Region', 'Year_left'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"\ngeneration_state_small = generation_state[generation_state[\"TYPE OF PRODUCER\"] == \n                                         \"Total Electric Power Industry\"]\n\nFOSSIL_FUELS = ['Coal', 'Natural Gas', 'Petroleum', 'Wood and Wood Derived Fuels']\nRENEWABLES = [ 'Wind',  'Nuclear','Other Biomass',\n             'Geothermal', 'Solar Thermal and Photovoltaic','Pumped Storage']\nOTHERS = ['Other Gases', 'Other']\ngeneration_state_small = generation_state_small.dropna(axis=1)\ngeneration_state_small['ENERGY TYPE'] = \"Other\"\ngeneration_state_small.loc[generation_state_small['ENERGY SOURCE'].isin(FOSSIL_FUELS),'ENERGY TYPE'] = \"Fossil Fuels\"\n\ngeneration_state_small.loc[generation_state_small['ENERGY SOURCE'].isin(RENEWABLES),'ENERGY TYPE'] = \"Renewables\"\n#\ngeneration_state_small = generation_state_small[generation_state_small[\"ENERGY SOURCE\"] != \"Total\"]\ngeneration_state_small = generation_state_small.drop(columns=[\"TYPE OF PRODUCER\", \"ENERGY SOURCE\"])\ngeneration_state_small[\"GENERATION (Megawatthours)\"] = generation_state_small[\"GENERATION (Megawatthours)\"].apply(lambda val: int(val.replace(\",\", \"\")))\ngeneration_state_grouped = generation_state_small.groupby(['YEAR', 'ENERGY TYPE','STATE' ]).sum().reset_index()\ngeneration_state_grouped = generation_state_grouped[generation_state_grouped[\"ENERGY TYPE\"] != \"Other\"]\n\n\nra = ppp_pollutions_region[ppp_pollutions_region.Region.isin(US_STATES)][[\"REG_ID\", \"Region\"]].drop_duplicates()\ndict(zip(ra.REG_ID.values, ra.Region.values))\nkra = {'US01': 'AL',\n 'US02': 'AK',\n 'US04': 'AZ',\n 'US05': 'AR',\n 'US06': 'CA',\n 'US08': 'CO',\n 'US09': 'CT',\n 'US10': 'DE',\n 'US11': 'DC',\n 'US12': 'FL',\n 'US13': 'GA',\n 'US15': 'HI',\n 'US16': 'ID',\n 'US17': 'IL',\n 'US18': 'IN',\n 'US19': 'IA',\n 'US20': 'KS',\n 'US21': 'KY',\n 'US22': 'LA',\n 'US23': 'ME',\n 'US24': 'MD',\n 'US25': 'MA',\n 'US26': 'MI',\n 'US27': 'MN',\n 'US28': 'MS',\n 'US29': 'MO',\n 'US31': 'NE',\n 'US32': 'NV',\n 'US33': 'NH',\n 'US34': 'NJ',\n 'US35': 'NM',\n 'US36': 'NY',\n 'US37': 'NC',\n 'US38': 'ND',\n 'US39': 'OH',\n 'US40': 'OK',\n 'US41': 'OR',\n 'US42': 'PA',\n 'US44': 'RI',\n 'US45': 'SC',\n 'US46': 'SD',\n 'US47': 'TN',\n 'US48': 'TX',\n 'US49': 'UT',\n 'US50': 'VT',\n 'US51': 'VA',\n 'US53': 'WA'}\nma = dict(zip(kra.values(), kra.keys()))\n\ndef checkout(row):\n    na = row['STATE']\n    if na in ma.keys():\n        return ma[na]\n    else:\n        return 'PLACEHOLDER'\ngeneration_state_grouped['OECD_code'] = generation_state_grouped.apply(checkout , axis=1)\nus_ppp_pollutions_region =  ppp_pollutions_region[ppp_pollutions_region.Region.isin(US_STATES)]\nus_ppp_energy_region = us_ppp_pollutions_region.join(generation_state_grouped.set_index('OECD_code'), on='REG_ID', how='inner')\nus_ppp_energy_region = us_ppp_energy_region[us_ppp_energy_region.YEAR == us_ppp_energy_region.Year_right]\nkr = us_ppp_energy_region[['YEAR','ENERGY TYPE','GENERATION (Megawatthours)', 'STATE', 'Value', 'Population']]\nprint(kr.shape)\nenergy_state_table = kr.pivot_table('GENERATION (Megawatthours)', ['YEAR','STATE', 'Value', 'Population'], 'ENERGY TYPE').reset_index()\nenergy_state_table['renew_fraction'] = energy_state_table['Renewables']/(energy_state_table['Renewables'] + energy_state_table['Fossil Fuels'])\nenergy_state_table= energy_state_table.fillna(0)\nenergy_state_table\ndiffs = energy_state_table.groupby('STATE').diff(1)\npct_ff_diffs = energy_state_table.groupby('STATE')['Fossil Fuels'].apply(pd.Series.pct_change)\npct_renew_diffs = energy_state_table.groupby('STATE')['Renewables'].apply(pd.Series.pct_change)\nenergy_state_table['FF_diff'] = diffs['Fossil Fuels']\nenergy_state_table['Renew_diff'] = diffs['Renewables']\nenergy_state_table\n\n\n\nenergy_state_table['pct_ff_diff'] = pct_ff_diffs\nenergy_state_table['pct_renew_diff'] = pct_renew_diffs \nenergy_state_table['Net Energy Change'] =  energy_state_table['FF_diff'] + energy_state_table['Renew_diff']\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"\n\n\ndef get_cum_values_states_year(states, year, criteria=\"state\", power=1):\n    usa_ppp_pollution = ppp_pollutions_region[ppp_pollutions_region['Region'].isin(states)]\n    usa_ppp_pollution_year = usa_ppp_pollution[usa_ppp_pollution.Year_left == year]\n    if power != 1:\n        usa_ppp_pollution_year['nPM2.5 Level (µg/m³)'] =  usa_ppp_pollution_year['PM2.5 Level (µg/m³)'].apply(lambda x: max(0, x-2.4))\n    else:\n        usa_ppp_pollution_year['nPM2.5 Level (µg/m³)'] = usa_ppp_pollution_year['PM2.5 Level (µg/m³)']\n    usa_ppp_pollution_year['pow_PM2.5 Level (µg/m³)'] = np.power(usa_ppp_pollution_year['nPM2.5 Level (µg/m³)'], power)\n    \n   \n    total_pop = usa_ppp_pollution_year.Population.sum()\n    total_pm25 =  usa_ppp_pollution_year[\"pow_PM2.5 Level (µg/m³)\"].sum()\n    usa_ppp_pollution_year.sort_values('PM2.5 Level (µg/m³)',inplace=True)\n    \n    \n    \n    if criteria == \"state\":\n        usa_ppp_pollution_year['cumsum_pop'] = np.linspace(0, 1, usa_ppp_pollution_year.shape[0])\n        usa_ppp_pollution_year['cumsum_pm25'] = usa_ppp_pollution_year[\"pow_PM2.5 Level (µg/m³)\"].cumsum()/total_pm25\n    else:\n        usa_ppp_pollution_year['total_2.5'] =   usa_ppp_pollution_year[\"pow_PM2.5 Level (µg/m³)\"] * usa_ppp_pollution_year[criteria] \n\n      \n        total_crit = usa_ppp_pollution_year[criteria].sum()\n        total_pm25 =  usa_ppp_pollution_year[\"total_2.5\"].sum()\n        usa_ppp_pollution_year['cumsum_pop'] = usa_ppp_pollution_year[criteria].cumsum()/total_crit\n        usa_ppp_pollution_year['cumsum_pm25'] = usa_ppp_pollution_year[\"total_2.5\"].cumsum()/total_pm25        \n   \n   \n    \n    return usa_ppp_pollution_year\n\ndef calculate_env_gini(y_vals, x_vals):\n    trap_abs = []\n    nick = y_vals.values\n    nick = np.concatenate([np.array([0]), nick])\n    for i in range(len(nick) - 1):\n        trap_abs.append(nick[i] + nick[i+1])\n\n    nick = x_vals.values\n    nick = np.concatenate([np.array([0]), nick])\n    trap_hs = []\n    for i in range(len(nick) - 1):\n        trap_hs.append(nick[i+1] - nick[i])\n\n    curve_area = 0\n    for i in range(len(trap_abs)):\n\n        \n        curve_area += (trap_abs[i] * trap_hs[i])\n\n    return 1 - curve_area\n\n\ndef ginis_pollution_yearly(states, code, criteria=\"state\", power=1):\n    cou_years = sorted(ppp_pollutions_region[ppp_pollutions_region['Region'].isin(states)].Year_left.unique())\n    pollution_wealth_ginis = []\n    for year in cou_years:\n        cou_ppp_pollution_year = get_cum_values_states_year(states, year, criteria, power=power) \n        k = calculate_env_gini(cou_ppp_pollution_year['cumsum_pm25'], cou_ppp_pollution_year['cumsum_pop'])\n        pollution_wealth_ginis.append(k)\n\n    total_cou_pollution = pollution_region[pollution_region.REG_ID == code]\n    total_cou_pollution = total_cou_pollution[total_cou_pollution.VAR == 'AIR_POL']\n    total_cou_pollution = total_cou_pollution[total_cou_pollution.Year.isin(cou_years)]\n    total_cou_pollution =  total_cou_pollution.sort_values('Year').Value\n    return cou_years, pollution_wealth_ginis, total_cou_pollution\n\n\ndef plot_pollution_wealth_gini(states, country_code, country_name, criteria=\"state\", power=1):\n    \n    years_to_use, pollution_wealth_ginis, total_pollution = ginis_pollution_yearly(states,  country_code, criteria, power=power)\n   \n    fig = make_subplots(specs=[[{\"secondary_y\": True}]])\n\n    # Add traces\n    fig.add_trace(\n        go.Scatter(x=years_to_use, y=pollution_wealth_ginis, name=\"Pollution Gini\"),\n        secondary_y=True,\n    )\n\n    fig.add_trace(\n        go.Scatter(x=years_to_use, y=total_pollution, name=\"Air Pollution in {} (µg/m³)\".format(country_name)),\n        secondary_y=False,\n    )\n\n    # Add figure title\n    fig.update_layout(\n        title_text= country_name +\" Pollution Gini and PM2.5 levels\"\n    )\n\n    # Set x-axis title\n    fig.update_xaxes(title_text=\"Year\")\n\n    # Set y-axes titles\n    fig.update_yaxes(title_text=\"Pollution Gini\", secondary_y=True)\n    fig.update_yaxes(title_text=\"Air Pollution in PM2.5 (µg/m³)\", secondary_y=False)\n\n\n    fig.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"usa_ppp_pollution_2013 = get_cum_values_states_year(US_STATES, 2003)\nenv_gini = calculate_env_gini(usa_ppp_pollution_2013['cumsum_pm25'], usa_ppp_pollution_2013['cumsum_pop'])\nprint(\"Pollution Gini (state): {}\".format(env_gini))\nfig = go.Figure()\nfig.add_trace(\n        go.Scatter(x=usa_ppp_pollution_2013['cumsum_pop'], y=usa_ppp_pollution_2013['cumsum_pm25'], name=\"Actual Distribution\"),\n\n    )\nfig.add_trace(\n        go.Scatter(x=[0,1], y=[0,1], name=\"Line of Equality\"),\n\n    )\nfig.add_trace(\n    go.Bar(x=usa_ppp_pollution_2013['cumsum_pop'], y=usa_ppp_pollution_2013['cumsum_pm25'], opacity=0.2, name='State Weightings')\n)\n\n# Add figure title\nfig.update_layout(\n    #title_text= \" Pollution Gini - State Level (2003)\"\n)\n\n# Set x-axis title\nfig.update_xaxes(title_text=\"Cumulative share of states\")\n\n\nfig.update_yaxes(title_text=\"Cumulative share of air pollution\")\n\n\nfig.show()\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"It is interesting to note that the pollution inequality at the state level is not very high. We proceed to repeat this process for every year that we have data for."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"\nplot_pollution_wealth_gini(US_STATES, \"USA\", \"USA\")\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We find that the Pollution Gini falls in tandem with air pollution of the US as a whole. So does our metric line up with reality? Thus the distribution of pollution in US as a whole become more equitable? Let's explore with a choropleth graph. (Note: 4 states are unrepresented because we lacked data for those states) "},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"usa_ppp_pollution = ppp_pollutions_region[ppp_pollutions_region['Region'].isin(US_STATES)]\n\n#usa_ppp_pollution_year = usa_ppp_pollution\n#total_gdp = usa_ppp_pollution_year.Population.sum()\n#total_pm25 =  usa_ppp_pollution_year[\"PM2.5 Level (µg/m³)\"]\nener_2018 = us_ppp_energy_region[us_ppp_energy_region.YEAR == 2018]\nfig = px.choropleth(us_ppp_energy_region, locations=us_ppp_energy_region.STATE, locationmode=\"USA-states\",\n                    animation_frame=\"YEAR\", animation_group=\"STATE\", hover_data=[\"PM2.5 Level (µg/m³)\", \"Population\"],\n                    color=us_ppp_energy_region[\"PM2.5 Level (µg/m³)\"], scope=\"usa\", range_color=[0,18])\n\nfig.show(config={'scrollZoom':False})\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We can see that as whole, the level of pollution in the US falls AND becomes more equitable across states. Thus demonstrating the legitimacy of the metric.\n\nHowever, some may take issue with this interpretation of pollution levels, and for good reason. Here, we are treating PM 2.5 levels as a uniform measure with every unit having the same (negative) value. So, with our current version, we would view 2 states with 5µg/m³ annually as being as bad as 1 state with 105µg/m³ annually. However, this is clearly at odds with reality. A measurement of 5µg/m³ is usually considered 'a clear and non-hazy day', [while EPA researchers have associated PM 2.5 levels of 10µg/m³ with higher incidence of pollution-related death in black communities](https://www.pri.org/stories/2020-01-07/air-pollution-silent-killer-even-when-it-meets-epa-standards).  To align our metric with our expections, we propose scaling these levels exponentially, as a way of better approximating the actual risk levels associated with higher PM 2.5 levels. "},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"def findMin(a):\n    k = a - 2.4\n    return max(k, 0)\n\n\nfig = go.Figure()\nfig.add_trace(\n        go.Scatter(x=[a for a in range(0, 18)], y=[findMin(a)**1.5 for a in range(0,18)] , name=\"Actual Distribution\"),\n\n    )\nfig.update_xaxes(title_text=\"Annual PM2.5 level (µg/m³)\")\n\nfig.update_layout(\n    title_text= \"Conversion of PM2.5 to risk levels\"\n)\n\nfig.update_yaxes(title_text='\"Risk Level\"')\nfig.update_layout(width=800, height=800)\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We use the baseline of 2.4µg/m as a baseline \"safe\" value based on [this study](https://www.sciencedirect.com/science/article/abs/pii/S004896972032355X), while having higher values rise by a power of 1.5. Of course, the choice of power function is arbitrary here, and I don't really have any justification of one value over another. Still, I think this presents at least a more accurate picture of reality compared to our linear function previous. "},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"POWER = 1.5\n\nusa_ppp_pollution_2013 = get_cum_values_states_year(US_STATES, 2003, power=POWER)\nenv_gini = calculate_env_gini(usa_ppp_pollution_2013['cumsum_pm25'], usa_ppp_pollution_2013['cumsum_pop'])\nprint('Env Gini: {}'.format(env_gini))\nfig = go.Figure()\nfig.add_trace(\n        go.Scatter(x=usa_ppp_pollution_2013['cumsum_pop'], y=usa_ppp_pollution_2013['cumsum_pm25'], name=\"Actual Distribution\"),\n\n    )\nfig.add_trace(\n        go.Scatter(x=[0,1], y=[0,1], name=\"Line of Equality\"),\n\n    )\nfig.add_trace(\n    go.Bar(x=usa_ppp_pollution_2013['cumsum_pop'], y=usa_ppp_pollution_2013['cumsum_pm25'], opacity=0.2, name='State Weightings')\n)\n\n# Add figure title\nfig.update_layout(\n    title_text= \"Modified Pollution Gini- State Level (2003)\"\n)\n\n# Set x-axis title\nfig.update_xaxes(title_text=\"Cumulative share of states\")\n\n\nfig.update_yaxes(title_text=\"Cumulative share of air pollution\")\n\n\nfig.show()\n\nplot_pollution_wealth_gini(US_STATES, \"USA\", \"USA\", power=POWER)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We observe a marked rise in pollution inequality across all years, which is more in line with our intuition. \n\nOur current KPI may be fine if we just want to compare between states, but what if want a more representative sample of the US? We can modify our Gini to take the population instead. To do so, we assume each person in each state experiences that state's annual PM 2.5 level equally. So we multiply population by PM 2.5, to get a total pollution level for each state, and spread that across the entire population. \n\nIn this case, we have y = Population * PM 2.5 Level, x = Population for each state. \n"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"POWER = 1.6\n\nusa_ppp_pollution_2013 = get_cum_values_states_year(US_STATES, 2003, power=POWER, criteria='Population')\nenv_gini = calculate_env_gini(usa_ppp_pollution_2013['cumsum_pm25'], usa_ppp_pollution_2013['cumsum_pop'])\nprint('Env Gini: {}'.format(env_gini))\nfig = go.Figure()\nfig.add_trace(\n        go.Scatter(x=usa_ppp_pollution_2013['cumsum_pop'], y=usa_ppp_pollution_2013['cumsum_pm25'], name=\"Actual Distribution\", \n                  ),\n\n    )\nfig.add_trace(\n        go.Scatter(x=[0,1], y=[0,1], name=\"Line of Equality\"),\n\n    )\nfig.add_trace(\n    go.Bar(x=usa_ppp_pollution_2013['cumsum_pop'], y=usa_ppp_pollution_2013['cumsum_pm25'], opacity=0.2, name='State Weightings' )\n)\n\n# Add figure title\nfig.update_layout(\n    title_text= \"Modified Pollution Gini- Population (2003)\"\n)\n\n\n    \n\n# Set x-axis title\nfig.update_xaxes(title_text=\"Cumulative share of population (least to most polluted)\")\n\n\nfig.update_yaxes(title_text=\"Cumulative share of air pollution\")\n\n\nfig.show()\n\nplot_pollution_wealth_gini(US_STATES, \"USA\", \"USA\", power=POWER, criteria='Population')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The graph is somewhat different, but confirms the general downward trend of inequality. "},{"metadata":{},"cell_type":"markdown","source":"## Net Energy Change Gini and Fossil Fuel Reliance as a proxy for employment\n\nFrom available litreature, we also know that fossil fuel jobs would be lost and renewable energy jobs created from environmental policy, especially in a push towards clean energies. So, it would be prudent for us to calculate an employment-wealth Gini from this as well. However, this is where we run into a roadblock, because unfortunately there is no publicly available dataset containing information on renewable jobs AND energy jobs created on the state/country level. (believe me, I've looked) \n\nSo we have to make do with a proxy. Fortunately, we can use an important finding confirmed independently by many pieces of peer-reviewed research, which is that renewable energy tends to create more jobs than it removes per unit energy produced compared to fossil fuels - up to 12 times, some say. So, in our analyis, we can safely make a worst-case assumption that a unit of energy produced for renewables is equivalent in job production/loss to a unit produced for fossil fuels. \n\nFortunately for us, data for renewable and fossil fuel energy generation is available for US states. So we can subtract the difference in fossil fuel generation over years from renewable generation to get the net energy change for each state. So we take the change in these energy generation levels as a very rough proxy for employment in these areas. (An approach with many limitations, but we makek do with what we have). Here, we calculate Ginis for renewable energy generation, fossil fuel generation, and net energy change = renewable energy change - fossil fuel energy change. Since the Net Energy Change column has negative values which is incompatible with Gini, we normalize by adding the minimum value in the year to all values. \n"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"\ndef get_cum_values_states_year(year, crit):\n    \n    usa_ppp_energy_year = energy_state_table[energy_state_table.YEAR == year]\n    \n    if crit == 'Net Energy Change' or 'diff' in crit: \n    \n        min_crit = usa_ppp_energy_year[crit].min()\n        usa_ppp_energy_year[crit] = usa_ppp_energy_year[crit].apply(lambda x: x - min_crit)\n    \n    \n    \n    \n    \n    total_pop =  usa_ppp_energy_year[\"Population\"].sum()\n    total_crit = usa_ppp_energy_year[crit].sum()\n    usa_ppp_energy_year['crit_per_person'] = usa_ppp_energy_year[crit]/usa_ppp_energy_year[\"Population\"]\n    usa_ppp_energy_year.sort_values('crit_per_person',inplace=True)\n   \n    \n    usa_ppp_energy_year['cumsum_pop'] = usa_ppp_energy_year['Population'].cumsum()/total_pop \n    \n    usa_ppp_energy_year['cumsum_crit'] = usa_ppp_energy_year[crit].cumsum()/total_crit\n    \n    return usa_ppp_energy_year\n\ndef ginis_energy_yearly(crit):\n    cou_years = sorted(us_ppp_energy_region.Year_left.unique())\n    energy_wealth_ginis = []\n    for year in cou_years:\n        if 'diff' in crit and year == 2003:\n            continue\n        \n        cou_ppp_pollution_year = get_cum_values_states_year(year, crit) \n        \n        k = calculate_env_gini(cou_ppp_pollution_year['cumsum_crit'], cou_ppp_pollution_year['cumsum_pop'])\n        \n        energy_wealth_ginis.append(k)\n    \n    total_cou_energy = []\n    for year in cou_years:\n        \n        kr = energy_state_table[energy_state_table.YEAR == year]\n        renew_sum = kr['Renewables'].sum()\n        ff_sum = kr['Fossil Fuels'].sum()\n    \n        total_cou_energy.append(ff_sum/(ff_sum + renew_sum))\n    \n  \n    return cou_years, energy_wealth_ginis, total_cou_energy\n\n\ndef plot_energy_wealth_gini(crit):\n    \n    years_to_use, energy_wealth_ginis, total_energy = ginis_energy_yearly(crit)\n   \n    fig = make_subplots(specs=[[{\"secondary_y\": True}]])\n\n    # Add traces\n    fig.add_trace(\n        go.Scatter(x=years_to_use, y=energy_wealth_ginis, name= crit + \" Gini\"),\n        secondary_y=True,\n    )\n\n    fig.add_trace(\n        go.Scatter(x=years_to_use, y=total_energy, name=\"Renewable Energy Generation (Megawatthours)\"),\n        secondary_y=False,\n    )\n    \n    \n\n    # Add figure title\n    fig.update_layout(\n        title_text= crit + \" Gini and Renewable Energy Generation\"\n    )\n\n    # Set x-axis title\n    fig.update_xaxes(title_text=\"Year\")\n\n    # Set y-axes titles\n    fig.update_yaxes(title_text= crit+ \" Gini\", secondary_y=True)\n    fig.update_yaxes(title_text=\"Renewable Energy (% of Total US Generation)\", secondary_y=False)\n\n\n    fig.show()\n    \n\n","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"for crit in ['Renewables', 'Fossil Fuels', 'Net Energy Change']:\n    ener = get_cum_values_states_year(2018, crit)\n    env_gini = calculate_env_gini(ener['cumsum_crit'], ener['cumsum_pop'])\n    #print(crit + ' Gini: {}'.format(env_gini))\n    fig = go.Figure()\n    fig.add_trace(\n            go.Scatter(x=ener['cumsum_pop'], y=ener['cumsum_crit'], name=\"Actual Distribution\"),\n\n        )\n    fig.add_trace(\n            go.Scatter(x=[0,1], y=[0,1], name=\"Line of Equality\"),\n\n        )\n    fig.add_trace(\n        go.Bar(x=ener['cumsum_pop'], y=ener['cumsum_crit'], opacity=0.2, name='State Weightings')\n    )\n\n    # Add figure title\n    fig.update_layout(\n        title_text= crit + \" Gini- Population (2018)\"\n    )\n\n    # Set x-axis title\n    fig.update_xaxes(title_text=\"Cumulative share of population\")\n\n\n    fig.update_yaxes(title_text=\"Cumulative share of \" + crit)\n\n\nfig.show()\n#plot_energy_wealth_gini('Fossil Fuels')\nfig = go.Figure()\nfor crit in ['Renewables', 'Fossil Fuels', 'Net Energy Change']:\n    years_to_use, energy_wealth_ginis, total_energy = ginis_energy_yearly(crit)\n    fig.add_trace(\n        go.Scatter(x=years_to_use, y=energy_wealth_ginis, name= crit + \" Gini\"),\n \n    )\n    fig.update_xaxes(title_text=\"Year\")\n    \nfig.update_layout(\n        title_text= \"Several Ginis - Population (2018)\"\n    )        \nfig.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"View the net change in energy generation by state in this graph! **(Note: you may have to run the slider up and down once to get it to work properly) **\n"},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"\nfig = px.bar(energy_state_table, x=\"STATE\", y=\"Net Energy Change\",\n             title=\"Net Energy Change by State\",  animation_frame=\"YEAR\", animation_group=\"STATE\")\nfig.show(config={'scrollZoom':False})","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We see that on the  years where most states saw a rise in net energy generation, the inequality index is lower, while during years that had mixed changes in energy generation, inequality index is high. (eg. 2010 and 2011) \n\nViewing the annual change in Renewable/Fossil Fuel energy generation is interesting, but ultimately it may be too susceptible to macroeconomic fluctuations to be used as a reliable indicator alone. Another persepective we could take is by viewing a state's fossil fuel dependence, which is simply their fossil fuel energy generation/total energy generation. \n\nThis is useful because it shows us 1. which states have successfully transitioned to renewables, and which ones are still stuck on fossil fuels, which we can roughly approximate to employment in these sectors, and 2. the future employment prospects of that state. Many economic studies and papers have confirmed the inexorable decline of fossil fuels, so we would like them all states to reduce fossil fuel reliance anyway. "},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"\nfig = px.bar(energy_state_table, x=\"STATE\", y=[\"Renewables\", \"Fossil Fuels\"],\n             title=\"Renewable/Fossil Fuel Distribution by State\",  animation_frame=\"YEAR\", animation_group=\"STATE\")\n\nfig.show(config={'scrollZoom':False})","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"energy_state_table[\"Fossil Fuel Reliance\"] = 1 - energy_state_table[\"renew_fraction\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"\n\n\n\n\n#usa_ppp_pollution_year = usa_ppp_pollution\n#total_gdp = usa_ppp_pollution_year.Population.sum()\n#total_pm25 =  usa_ppp_pollution_year[\"PM2.5 Level (µg/m³)\"]\nener_2018 = us_ppp_energy_region[us_ppp_energy_region.YEAR == 2018]\nfig = px.choropleth(energy_state_table, locations=energy_state_table.STATE, locationmode=\"USA-states\",\n                    animation_frame=\"YEAR\", animation_group=\"STATE\", hover_data=[\"Renewables\", 'Fossil Fuels'],\n                    color=energy_state_table[\"Fossil Fuel Reliance\"], scope=\"usa\", range_color=[0,1])\nfig.update_layout(\n        title_text= \"Fossil Fuel Reliance by state - Choropleth\"\n    )        \nfig.show(config={'scrollZoom':False})","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"# Add dropdown\nimport random\n\nimport matplotlib\ncolors = []\nfor name, hex in matplotlib.colors.cnames.items():\n    colors.append(hex)\ncolors\n\n\nbuttons = []\nfig = go.Figure()\n\nvis_arg = {\"visible\":[True for a in range(len(energy_state_table.STATE.unique()))]}\nbuttons.append(dict(args=[vis_arg], label='All', method='update'))\n\nfor i, state in enumerate(energy_state_table.STATE.unique()):\n    est = energy_state_table[energy_state_table.STATE == state]\n    \n \n        \n    fig.add_trace(\n        go.Scatter(x=est.YEAR,\n               y=est[\"Fossil Fuel Reliance\"],\n               name=state,\n               line=dict(color=colors[i+10]),\n              visible=True)\n    )\n\n    vis_arg = {\"visible\":[False for a in range(len(energy_state_table.STATE.unique()))]}\n    vis_arg[\"visible\"][i] = True\n    buttons.append(dict(args=[vis_arg], label=state, method='update'))\n    \nfig.update_layout(\n    title_text='Fossil Fuel Reliance by State',\n    updatemenus=[dict(\n        active=0,\n        buttons=buttons,\n    )]\n)\nfig.show()\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We can even plot a Gini for Fossil Fuel Reliance, if we so choose. "},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"for crit in ['Fossil Fuel Reliance']:\n    ener = get_cum_values_states_year(2018, crit)\n    env_gini = calculate_env_gini(ener['cumsum_crit'], ener['cumsum_pop'])\n    #print(crit + ' Gini: {}'.format(env_gini))\n    fig = go.Figure()\n    fig.add_trace(\n            go.Scatter(x=ener['cumsum_pop'], y=ener['cumsum_crit'], name=\"Actual Distribution\"),\n\n        )\n    fig.add_trace(\n            go.Scatter(x=[0,1], y=[0,1], name=\"Line of Equality\"),\n\n        )\n    fig.add_trace(\n        go.Bar(x=ener['cumsum_pop'], y=ener['cumsum_crit'], opacity=0.2, name='State Weightings')\n    )\n\n    # Add figure title\n    fig.update_layout(\n        title_text= crit + \" Gini- Population (2018)\"\n    )\n\n    # Set x-axis title\n    fig.update_xaxes(title_text=\"Cumulative share of population\")\n\n\n    fig.update_yaxes(title_text=\"Cumulative share of \" + crit)\n\n\nfig.show()\n#plot_energy_wealth_gini('Fossil Fuels')\nfig = make_subplots(specs=[[{\"secondary_y\": True}]])\nfor crit in ['Fossil Fuel Reliance']:\n    years_to_use, energy_wealth_ginis, total_energy = ginis_energy_yearly(crit)\n    fig.add_trace(\n        go.Scatter(x=years_to_use, y=energy_wealth_ginis, name= crit + \" Gini\"),\n         secondary_y = False\n    )\n    fig.add_trace(\n        go.Scatter(x=years_to_use, y=total_energy, name= \"Fossil Fuel Reliance (USA)\"),\n         secondary_y = True\n    )\n    fig.update_xaxes(title_text=\"Year\")\n    fig.update_yaxes(title_text=crit + \" Gini\", secondary_y=False)\n    fig.update_yaxes(title_text=\"Fossil Fuel Reliance (USA)\", secondary_y=True)\n    \nfig.update_layout(\n        title_text= \"Fossil Fuel Reliance Gini - Population (2018)\"\n    )        \nfig.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Overall, we find both a declining trend in fossil fuel reliance, as well as Gini for fossil fuel reliance in US over the years, which is a positive development."},{"metadata":{},"cell_type":"markdown","source":"# Intersection between corporate and environmental concerns \n\nPrivately-owned businesses and enterprises are estimated to release 70% of all carbon emissions, and governments often bend to the interests of these massively powerful corporations. (to varying degrees) Therefore, they are undoubtedly an important stakeholder to consider, some might say even more than policymakers. After all, environmental regulations may help curtail some of the actions of these polluting industries, but at the expense of society's welfare since energy is a necessity\\. Instead, if there was an incentive for private businesses to naturally adopt more sustainable practices instead, then we could let the hand of the free market guide coroporations to a cleaner, more equitable world. "},{"metadata":{},"cell_type":"markdown","source":"## Renewables\n\nIn the past, renewable energy had been earmarked by investors as being too expensive and inefficient to be financially feasible in  any manner. However, with the advancement of green technologies and a shift in global perceptions towards climate change over the past decade, they have been growing in prominence and attractiveness, and are looking poised to overtake traditional fossil fuels as the main energy source over the next decade or two. \n\nHowever, how viable is renewable energy really compared to fossil fuels? As an business owner/investor, would it really be in my best interest to invest into clean energy? We invesetigate several key issues surrounding the adoption of renewable energies, and therefore find the intersection between corporate and environmental concerns. "},{"metadata":{},"cell_type":"markdown","source":"### 1. Price of Renewables\n\nCosts in producing energy can be broadly broken into two categories. First, is the capital costs involved to set up the infrastructure and machinery needed to generate the energy, whether that comes in the form a power plant, or wind farms. The second are the costs for the production of the energy itself, the \"running costs of the operation\". \n\nWe start with running costs. Running costs can be quite handily summarized with the levelized cost of energy (LROE)  metric, which takes the sum of costs over a plant's lifetime and divides it by the sum of electricity produced over its lifetime. Taken over many plants, this provides a good estimate for us to compare the running costs of different energy types. The data below was collected by IRENA, and shows declining LCOEs for renewables over time. We use Lazard's estimation of LCOE for gas combined cycle power plant and coal for 2020."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"fig = go.Figure()\nfor et in renewable_energy_stats['Energy Type'].unique():\n    et_data = renewable_energy_stats[renewable_energy_stats['Energy Type'] == et]\n    et_lcoe = et_data[\"LCOE (2019 USD/kWh) (Weighted Average)\"]\n    fig.add_trace(\n        go.Scatter(x=et_data.Year, y=et_lcoe, name= et ),\n\n    )\n\nfig.add_trace(go.Scatter(x=[2010, 2019], y=[0.112, 0.112],  name='Coal (Lazard)', line=dict(color='Red', dash='dot')))\nfig.add_trace(go.Scatter(x=[2010, 2019], y=[0.059, 0.059],  name='Gas/oil combined cycle power plant (Lazard)', line=dict(color='Purple', dash='dot')))\n\nfig.update_layout(\n    title_text= \"Levellised cost of energy for different energy sources\"\n)\n\nfig.update_xaxes(title_text=\"Year\")\nfig.update_yaxes(title_text=\"Levelized cost of energy (2019 USD/kWh)\") \nfig.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Of course, the estimates for these costs vary, and Lazard's own estimation for the average cost of solar PV puts it below the combined cycle power plant at 0.036. Whichever the case though, this shows that the most common renewables, solar photovoltaic and onshore wind, are price-competitive with fossil fuels in terms of running costs.  \n\nThen, we move on to the initial capital costs, the barrier to entry for all energy aspirants. This is typically measured using overnight cost per watt. As of 2019, the estimated average capital costs per kilowatt for coal is 3500, while for gas/oil combined cycle it is 1000. Most renewables have fallen below the installed price of coal as of 2019, but only solar PV is able to compete with the combined cycle power plant."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"fig = go.Figure()\nfor et in renewable_energy_stats['Energy Type'].unique():\n    et_data = renewable_energy_stats[renewable_energy_stats['Energy Type'] == et]\n    et_lcoe = et_data[\"Total Installed Costs (2019 USD/kW) (Weighted Average)\"]\n    fig.add_trace(\n        go.Scatter(x=et_data.Year, y=et_lcoe, name= et ),\n\n    )\n    \nfig.add_trace(go.Scatter(x=[2010, 2019], y=[3500, 3500],  name='Coal (2019)', line=dict(color='Red', dash='dot')))\nfig.add_trace(go.Scatter(x=[2010, 2019], y=[1000, 1000],  name='Gas/oil combined cycle power plant (2019)', line=dict(color='Purple', dash='dot')))\n\nfig.update_layout(\n    title_text= \"Total Installed Costs for different energy sources\"\n)\n\nfig.update_xaxes(title_text=\"Year\")\nfig.update_yaxes(title_text=\"Total Installed Costs (2019 USD/kW) (Average)\") \nfig.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"In conclusion, it seems that costs should not be a deterrent for businesses to switch from fossil fuels to the most cost efficient renewables. In fact, one could make substantial cost savings by moving from coal to solar PV/onshore wind power in the long run. Further government subsidization and schemes should only serve to reinforce this. "},{"metadata":{},"cell_type":"markdown","source":"### 2. Reliability of Renewables\n\nIntuitively, renewable energies seem like they have the potential to be unreliable. After all, many are dependent on transient energy sources (such as the sun and wind), so what happens when these are unavailable? And do nascent renewable energy industries (compared to fossil fuels) have the infrastructure to accomodate variability, failures and power ramps? \n\nA lot of research have been conducted by academics and industry experts on these matters, and I am far from** being well-informed enough to explain them in detail. However, the trend is that every year the litreature suggests that renewable energy is able to reliably power larger and larger areas. This is corroborated by the data we do hae, which sees an exponential increase in the consumption of new renewables, as compared to a linear increase only for fossil fuels. (which is to be expected due to the increase in global demand for energy anyway)"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"wec = energy_consumption_country[energy_consumption_country.Entity == 'World']\nwec = wec[wec.Year > 1970]\nwec[\"Coal Consumption - TWh\"] = wec[\"Coal Consumption - EJ\"] * 277.778\nwec[\"Gas Consumption - TWh\"] = wec[\"Gas Consumption - EJ\"] * 277.778\nwec[\"Oil Consumption - TWh\"] = wec[\"Oil Consumption - EJ\"] * 277.778","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"fig = make_subplots(rows=1, cols=2, subplot_titles=[\"World Consumption of Renewables\", \"World Consumption of Fossil Fuels\"])\nfig.add_trace(go.Scatter(x=wec.Year, y=wec[\"Solar Generation - TWh\"], name= 'Solar Generation - TWh' ), row=1,col=1)\nfig.add_trace(go.Scatter(x=wec.Year, y=wec[\"Wind Generation -TWh\"], name= 'Wind Generation - TWh' ),row=1,col=1)\n#fig.add_trace(go.Scatter(x=wec.Year, y=wec[\"Hydro Generation - TWh\"], name= 'Hydro Generation - TWh' ))\nfig.add_trace(go.Scatter(x=wec.Year, y=wec[\"Geo Biomass Other - TWh\"], name= 'Geo Biomass Other - TWh' ),row=1,col=1)\n\nfig.update_xaxes(title_text=\"Year\", row=1, col=1)\nfig.update_yaxes(title_text=\"Total Consumption\", row=1,col=1) \n\nfig.add_trace(go.Scatter(x=wec.Year, y=wec[\"Coal Consumption - EJ\"], name= 'Coal Consumption - EJ' ), row=1,col=2)\nfig.add_trace(go.Scatter(x=wec.Year, y=wec[\"Gas Consumption - EJ\"], name= 'Gas Consumption - EJ' ),row=1,col=2)\n#fig.add_trace(go.Scatter(x=wec.Year, y=wec[\"Hydro Generation - TWh\"], name= 'Hydro Generation - TWh' ))\nfig.add_trace(go.Scatter(x=wec.Year, y=wec[\"Oil Consumption - EJ\"], name= 'Oil Consumption - EJ' ),row=1,col=2)\n\nfig.update_xaxes(title_text=\"Year\", row=1, col=2)\nfig.update_yaxes(title_text=\"Total Consumption\", row=1,col=2) \n\nfig.show()\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### 3. Investment potential of renewables \n\nInvestor sentiment is an indispensable factor when it comes to business feasibility. It can be said that even the CEO of a company is just another employee in the eyes of its shareholders. So how do we convince investors to make the switch from traditional to renewable, assuming players that are driven not only by alturism and expect some kind of commensurate return? \n\nA [study](https://www.imperial.ac.uk/business-school/faculty-research/research-centres/centre-climate-finance-investment/research/energy-investing-exploring-risk-and-return-the-capital-markets/) by the Imperial College London and the International Energy Agency this year sought to answer this question. They first identified some \"pure play\" companies in renewable energy and fossil fuels in  Germany/France,the UK and the US, using them to build two separate portfolios respectively. They then analyzed the activity of these two portfolios over 5 and 10 year period. They found that renewables yielded a 178.2% return over -20.7% for fossil fuel investments over a 5 year period for France+Germany, 75.4% compared to 8.8% returns for the UK, and 200.3% against 97.2% for the US.\n\n![Returns](https://i.imgur.com/UUjY4oY.png)\n\nIf renewables are such a lucrative market, why aren't more instituational investors jumping on? Well, the authors themselves proffered an explanation,  “It is easier to allocate a meaningful percentage of their assets under management to renewables if the market is deep and liquid...Currently, that is not the case.” Currently, the renewable market is still too nascent and its waters too untested for big-name firms to shake off their hesitancy. Still, even on this front, this may not be the case for long. Investment in renewable energy has been steadily rising for the past decade and governments have already pledged a collective US$1 trillion on clean energy in the next decade, so it seems only a matter of time until the market becomes deep and tested enough to inspire the confidence of shareholders. Maybe then, it will reach the crucial inflexion point it needs to really take off.\n\n"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"fig = go.Figure()\nfig.add_trace(go.Bar(x=global_investment_renew.Year, y=global_investment_renew[\"GLOBAL RENEWABLE ENERGY CAPACITY INVESTMENT, $BN\"], \n                        name=\"GLOBAL RENEWABLE ENERGY CAPACITY INVESTMENT, $BN\"))\nfig.update_xaxes(title_text=\"Year\")\n\nfig.update_layout(\n    title_text= \"Global Renewable Energy Capacity Investment, $BN\"\n)\nfig.show()\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### So what's stopping people?\n\nI have not shown all sides of this argument, naturally. It would be impossible to capture all political, economic, (and administrative?) contexts unique to each region that might possibly act as a roadblock for providers of renewable energy. However, the fact is that renewables have seen widespread success in many regions across the world. In Europe, especially, there have been many success stories. I have already mentioned Germany, but the other EU states are perhaps even more impressive. The UK and Denmark have achieved 50% renewable usage, and Scotland, incredibly, is on track to power 100% of its domestic electricity consumption with clean energy by the end of this year. \n\nAll this was achieved wfrom a collaboration of public and private sectors, with governments generously supplying subsidies and infrastructures and the business owners managing the production and distribution. The Aberdeen Renewable Energy Group (AREG), for instance,  is a public-private partnership created in Scotland to identify and promote renewable energy opportunities for businesses in the country. \n\nThe existence of such exemplars, together the aforementioned factors of costs, reliability, and investment potential, suggest that the inability of other developed nations to transition to renewables stems from factors besides economic. For rapidly industrializing economies, the situation is more fraught, but that doesn't mean that they cannot contribute in small ways. I[ndia has committed to reducing their greenhouse gas emissions intensity by 33% from 2005 to 2030](https://www.globalenergyinstitute.org/indias-conditional-unconditional-climate-pledge), and [China has pledged to reach \"peak carbon\" by 2030 and zero emissions by 2060](https://www.theguardian.com/environment/2020/sep/27/china-carbon-pledge-put-energy-system-reverse-wind-solar). Although it still remains to be seen whether they can deliver on these promises, the conditions seem favorable for a smooth transition, and perhaps a day may come when it becomes more economically vialbe to adopt renewables as the primary energy source. For the whole world, it only seems a matter of time. \n\n"},{"metadata":{},"cell_type":"markdown","source":"## What about energy-efficient companies? \n\nNot all companies need to be in the energy sector to make a difference to climate change, though. We examine companies from all different sectors provided in the CDP dataset. Since the question posed was whether that could be any intersection between corporate and environmental interests, we will focus on the possible profitability derived from selling of environmentally friendly products. "},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"fig = go.Figure()\nall_corps = [corp_18, corp_19, corp_20]\nprint(\"Percentage of companies who sell low-carbon products/help lower GHG emissions for third-parties:\")\nyears = []\nyes = []\nno = []\nfor i, corp in enumerate(all_corps):\n   \n    r = corp[corp[\"question_number\"]== \"C4.5\"].response_value.value_counts().values\n    yes_r = r[0]/(r[0] + r[1])\n    years.append(2018+i) \n    yes.append(r[0])\n    no.append(r[1])\n    print(\"{}: {:.1f}%\".format(2018+i, yes_r*100))\n    \nfig.add_trace(go.Bar(x=years, y=yes, name='Yes'))\nfig.add_trace(go.Bar(x=years, y=no, name='No'))\nfig.update_layout(\n    title_text= \"Do you classify any of your existing goods and/or services as low-carbon products or do they enable a third party to avoid GHG emissions?\"\n)\nfig.show()\n\n","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"y1 = []\ny2 = []\ny3 = []\nfor corp in all_corps:\n    qin = corp[corp[\"column_name\"].str.contains(\"C4.5a_C3\")]\n    r = qin[qin.response_value.notna()].response_value.value_counts().values\n   \n    y1.append(r[0])\n    y2.append(r[1])\n    y3.append(r[2])\n    \n\n\nyears = ['2018', '2019', '2020']\n\nfig = go.Figure()\nfig.add_trace(go.Bar(\n    x=years,\n    y=y1,\n    name='Low-carbon product',\n    marker_color='indianred'\n))\nfig.add_trace(go.Bar(\n    x=years,\n    y=y2,\n    name='Avoided Emissions',\n    marker_color='lightsalmon'\n))\n\nfig.add_trace(go.Bar(\n    x=years,\n    y=y3,\n    name='Both',\n    marker_color='blue'\n))\n\n\n# Here we modify the tickangle of the xaxis, resulting in rotated labels.\nfig.update_yaxes(title_text=\"No. of Companies\")\nfig.update_layout(barmode='group', xaxis_tickangle=-45)\nfig.update_layout(\n    title_text= \"Are these low-carbon product(s) or do they enable avoided emissions?\"\n)\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"fig = go.Figure()\nfor i, corp in enumerate(all_corps):\n    qin = corp[corp[\"column_name\"].str.contains(\"C4.5a_C5\")]\n    r = qin[qin.response_value.notna()]\n    fig.add_trace(\n        go.Box(y=r.response_value.values,  boxpoints='all', jitter=0.3,  name=str(i + 2018))\n    )\nfig.update_layout(\n    title_text= \"% revenue from low-carbon product(s) in the reporting year\"\n)    \nfig.show()\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"While the median company still makes only about 2% of their revenue from low-carbon products in 2020, this is a rise from the  previous years of 1%, and if we extend it to the 75th percentile, we find a marked increase from 25% to 29.5%, which is promising. There are also a group of companies that make a 100% of their revenue from low-carbon products, but this is likely from the nature of the product they are selling. "},{"metadata":{},"cell_type":"markdown","source":"# Thanks for reading!"},{"metadata":{},"cell_type":"markdown","source":"# Acknowledgements\n\nCredit to all the sites I've linked in this notebook, and the sources for the data are listed in the compiled dataset page.\n\nExternal datasets used come from the following sources:\n\nOECD: https://stats.oecd.org/\n\nOur World in Data: https://ourworldindata.org/co2-and-other-greenhouse-gas-emissions, https://ourworldindata.org/energy https://ourworldindata.org/co2-and-other-greenhouse-gas-emissions\n\nClimateWatch: https://www.climatewatchdata.org/\n\nCorporate Knights: https://www.corporateknights.com/reports/2020-global-100/2020-global-100-ranking-15795648/\n\nIRENA: https://www.irena.org/Statistics/View-Data-by-Topic/Costs/Global-Trends\n\nInstitute for Health Metrics and Evaluation: http://www.healthdata.org/\n\nBP Statistical Review of World Energy: https://www.bp.com/en/global/corporate/energy-economics/statistical-review-of-world-energy.html\n\nWorld Resource Institute: https://www.wri.org/\n\nUS Bureau of Labour Statistics: https://www.bls.gov/\n\nUS Census Bureau: https://www.census.gov/data/tables/time-series/demo/popest/2010s-state-total.html\n\nWorld Bank: https://www.worldbank.org/\n\nUS Energy Information Administration: https://www.eia.gov/electricity/data/state/\n"}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}