Skip to content

Instantly share code, notes, and snippets.

@Allgoerithm
Last active July 25, 2019 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Allgoerithm/55187359caf0b2d795e530b4268e35af to your computer and use it in GitHub Desktop.
Save Allgoerithm/55187359caf0b2d795e530b4268e35af to your computer and use it in GitHub Desktop.
Is_it_the_noise.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Is_it_the_noise.ipynb",
"version": "0.3.2",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/Allgoerithm/55187359caf0b2d795e530b4268e35af/is_it_the_noise.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "4D0uDvRGMTHW",
"colab_type": "code",
"outputId": "0fee076d-8303-4f93-bd2a-e4a543fac6dd",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 729
}
},
"source": [
"import altair as alt\n",
"import pandas as pd\n",
"import numpy as np\n",
"np.random.seed(9876) # you might want to try other values here\n",
"\n",
"def error_simulation(measurement_error: float, growth: float, start_value_abs: int = 100000, \n",
" first_year: int = 2004, last_year: int = 2018) -> pd.DataFrame:\n",
" '''Simulate growth at a constant rate, with and without measurement error. \n",
" :param measurement_error: Measurement error (between 0 and 1, e.g., 0.02 for 2%)\n",
" :param growth: Growth rate (usually values like 0.01 for 1%, 0.05 for 5%, etc)\n",
" :param start_value_abs: absolut value in the first year\n",
" :param first_year: starting year of simulation\n",
" :param last_year: last year of simulation\n",
" :return: Pandas dataframe with observed and error-free data for absolute and relative simulated values\n",
" '''\n",
" current_wo_error_abs_value = start_value_abs\n",
" growth_factor = 1 + growth\n",
" wo_error_abs_value = [] # error-free absolute value\n",
" wo_error_rel_value = [np.nan] # error-free relative value (percentage growth against previous year)\n",
" observed_abs_value = [] # observed absolute value, including measurement error\n",
" observed_rel_value = [np.nan] # observed relative value, including measurement error\n",
" for i in range(last_year - first_year + 1):\n",
" wo_error_abs_value.append(current_wo_error_abs_value)\n",
" rand_error = np.random.uniform(low=-measurement_error, high=measurement_error)\n",
" observed_abs_value.append(round(current_wo_error_abs_value + current_wo_error_abs_value * rand_error))\n",
" if i > 0:\n",
" observed_rel_value.append((observed_abs_value[i] - observed_abs_value[i-1])/observed_abs_value[i-1])\n",
" wo_error_rel_value.append((wo_error_abs_value[i] - wo_error_abs_value[i-1])/wo_error_abs_value[i-1])\n",
" current_wo_error_abs_value = round(current_wo_error_abs_value * growth_factor)\n",
"\n",
" return pd.DataFrame({'year': range(first_year, last_year + 1), 'observed_abs_value': observed_abs_value, \n",
" 'observed_rel_value': observed_rel_value, 'wo_error_abs_value': wo_error_abs_value, \n",
" 'wo_error_rel_value': wo_error_rel_value})\n",
"\n",
"simulation = error_simulation(measurement_error=0.02, growth=0.05)\n",
"\n",
"# define charts without error\n",
"wo_error_abs_chart = alt.Chart(simulation).mark_line().encode(\n",
" x='year:Q',\n",
" y='wo_error_abs_value:Q'\n",
")\n",
"\n",
"wo_error_rel_chart = alt.Chart(simulation).mark_line().encode(\n",
" x='year:Q',\n",
" y='wo_error_rel_value:Q'\n",
")\n",
"\n",
"\n",
"# define charts with error\n",
"observed_abs_chart = alt.Chart(simulation).mark_line().encode(\n",
" x='year:Q',\n",
" y='observed_abs_value:Q'\n",
")\n",
"\n",
"observed_rel_chart = alt.Chart(simulation).mark_line().encode(\n",
" x='year:Q',\n",
" y='observed_rel_value:Q'\n",
")\n",
"\n",
"\n",
"# put all charts next to and below each other into a single figure\n",
"(wo_error_abs_chart|wo_error_rel_chart) & (observed_abs_chart|observed_rel_chart)"
],
"execution_count": 1,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"VConcatChart({\n",
" data: year observed_abs_value ... wo_error_abs_value wo_error_rel_value\n",
" 0 2004 98654 ... 100000 NaN\n",
" 1 2005 105507 ... 105000 0.050000\n",
" 2 2006 112019 ... 110250 0.050000\n",
" 3 2007 116996 ... 115762 0.049995\n",
" 4 2008 121614 ... 121550 0.049999\n",
" 5 2009 125569 ... 127628 0.050004\n",
" 6 2010 133794 ... 134009 0.049997\n",
" 7 2011 138665 ... 140709 0.049997\n",
" 8 2012 150396 ... 147744 0.049997\n",
" 9 2013 155076 ... 155131 0.049999\n",
" 10 2014 160791 ... 162888 0.050003\n",
" 11 2015 168632 ... 171032 0.049998\n",
" 12 2016 177885 ... 179584 0.050002\n",
" 13 2017 187015 ... 188563 0.049999\n",
" 14 2018 195104 ... 197991 0.049999\n",
" \n",
" [15 rows x 5 columns],\n",
" vconcat: [HConcatChart({\n",
" hconcat: [Chart({\n",
" encoding: FacetedEncoding({\n",
" x: X({\n",
" field: 'year',\n",
" type: 'quantitative'\n",
" }),\n",
" y: Y({\n",
" field: 'wo_error_abs_value',\n",
" type: 'quantitative'\n",
" })\n",
" }),\n",
" mark: 'line'\n",
" }), Chart({\n",
" encoding: FacetedEncoding({\n",
" x: X({\n",
" field: 'year',\n",
" type: 'quantitative'\n",
" }),\n",
" y: Y({\n",
" field: 'wo_error_rel_value',\n",
" type: 'quantitative'\n",
" })\n",
" }),\n",
" mark: 'line'\n",
" })]\n",
" }), HConcatChart({\n",
" hconcat: [Chart({\n",
" encoding: FacetedEncoding({\n",
" x: X({\n",
" field: 'year',\n",
" type: 'quantitative'\n",
" }),\n",
" y: Y({\n",
" field: 'observed_abs_value',\n",
" type: 'quantitative'\n",
" })\n",
" }),\n",
" mark: 'line'\n",
" }), Chart({\n",
" encoding: FacetedEncoding({\n",
" x: X({\n",
" field: 'year',\n",
" type: 'quantitative'\n",
" }),\n",
" y: Y({\n",
" field: 'observed_rel_value',\n",
" type: 'quantitative'\n",
" })\n",
" }),\n",
" mark: 'line'\n",
" })]\n",
" })]\n",
"})"
],
"text/html": [
"<!DOCTYPE html>\n",
"<html>\n",
"<head>\n",
" <style>\n",
" .vega-actions a {\n",
" margin-right: 12px;\n",
" color: #757575;\n",
" font-weight: normal;\n",
" font-size: 13px;\n",
" }\n",
" .error {\n",
" color: red;\n",
" }\n",
" </style>\n",
" <script type=\"text/javascript\" src=\"https://cdn.jsdelivr.net/npm//vega@5\"></script>\n",
" <script type=\"text/javascript\" src=\"https://cdn.jsdelivr.net/npm//vega-lite@3.3.0\"></script>\n",
" <script type=\"text/javascript\" src=\"https://cdn.jsdelivr.net/npm//vega-embed@4\"></script>\n",
"</head>\n",
"<body>\n",
" <div id=\"altair-viz\"></div>\n",
" <script>\n",
" var spec = {\"config\": {\"view\": {\"width\": 400, \"height\": 300}, \"mark\": {\"tooltip\": null}}, \"vconcat\": [{\"hconcat\": [{\"mark\": \"line\", \"encoding\": {\"x\": {\"type\": \"quantitative\", \"field\": \"year\"}, \"y\": {\"type\": \"quantitative\", \"field\": \"wo_error_abs_value\"}}}, {\"mark\": \"line\", \"encoding\": {\"x\": {\"type\": \"quantitative\", \"field\": \"year\"}, \"y\": {\"type\": \"quantitative\", \"field\": \"wo_error_rel_value\"}}}]}, {\"hconcat\": [{\"mark\": \"line\", \"encoding\": {\"x\": {\"type\": \"quantitative\", \"field\": \"year\"}, \"y\": {\"type\": \"quantitative\", \"field\": \"observed_abs_value\"}}}, {\"mark\": \"line\", \"encoding\": {\"x\": {\"type\": \"quantitative\", \"field\": \"year\"}, \"y\": {\"type\": \"quantitative\", \"field\": \"observed_rel_value\"}}}]}], \"data\": {\"name\": \"data-fe5aefd2a7a03eaee2847061bf229325\"}, \"$schema\": \"https://vega.github.io/schema/vega-lite/v3.3.0.json\", \"datasets\": {\"data-fe5aefd2a7a03eaee2847061bf229325\": [{\"year\": 2004, \"observed_abs_value\": 98654, \"observed_rel_value\": null, \"wo_error_abs_value\": 100000, \"wo_error_rel_value\": null}, {\"year\": 2005, \"observed_abs_value\": 105507, \"observed_rel_value\": 0.069464998884992, \"wo_error_abs_value\": 105000, \"wo_error_rel_value\": 0.05}, {\"year\": 2006, \"observed_abs_value\": 112019, \"observed_rel_value\": 0.0617210232496422, \"wo_error_abs_value\": 110250, \"wo_error_rel_value\": 0.05}, {\"year\": 2007, \"observed_abs_value\": 116996, \"observed_rel_value\": 0.04442996277417224, \"wo_error_abs_value\": 115762, \"wo_error_rel_value\": 0.04999546485260771}, {\"year\": 2008, \"observed_abs_value\": 121614, \"observed_rel_value\": 0.03947143492085199, \"wo_error_abs_value\": 121550, \"wo_error_rel_value\": 0.049999136158670375}, {\"year\": 2009, \"observed_abs_value\": 125569, \"observed_rel_value\": 0.032520926866972555, \"wo_error_abs_value\": 127628, \"wo_error_rel_value\": 0.050004113533525296}, {\"year\": 2010, \"observed_abs_value\": 133794, \"observed_rel_value\": 0.06550183564414784, \"wo_error_abs_value\": 134009, \"wo_error_rel_value\": 0.04999686589149716}, {\"year\": 2011, \"observed_abs_value\": 138665, \"observed_rel_value\": 0.03640671480036474, \"wo_error_abs_value\": 140709, \"wo_error_rel_value\": 0.049996642016580974}, {\"year\": 2012, \"observed_abs_value\": 150396, \"observed_rel_value\": 0.08459957451411676, \"wo_error_abs_value\": 147744, \"wo_error_rel_value\": 0.049996801910325565}, {\"year\": 2013, \"observed_abs_value\": 155076, \"observed_rel_value\": 0.031117848878959548, \"wo_error_abs_value\": 155131, \"wo_error_rel_value\": 0.04999864630712584}, {\"year\": 2014, \"observed_abs_value\": 160791, \"observed_rel_value\": 0.03685289793391627, \"wo_error_abs_value\": 162888, \"wo_error_rel_value\": 0.0500029007741844}, {\"year\": 2015, \"observed_abs_value\": 168632, \"observed_rel_value\": 0.0487651672046321, \"wo_error_abs_value\": 171032, \"wo_error_rel_value\": 0.049997544324934924}, {\"year\": 2016, \"observed_abs_value\": 177885, \"observed_rel_value\": 0.05487096162057024, \"wo_error_abs_value\": 179584, \"wo_error_rel_value\": 0.050002338743626924}, {\"year\": 2017, \"observed_abs_value\": 187015, \"observed_rel_value\": 0.05132529443179582, \"wo_error_abs_value\": 188563, \"wo_error_rel_value\": 0.0499988863150392}, {\"year\": 2018, \"observed_abs_value\": 195104, \"observed_rel_value\": 0.043253214982755396, \"wo_error_abs_value\": 197991, \"wo_error_rel_value\": 0.049999204509898545}]}};\n",
" var embedOpt = {\"mode\": \"vega-lite\"};\n",
"\n",
" function showError(el, error){\n",
" el.innerHTML = ('<div class=\"error\" style=\"color:red;\">'\n",
" + '<p>JavaScript Error: ' + error.message + '</p>'\n",
" + \"<p>This usually means there's a typo in your chart specification. \"\n",
" + \"See the javascript console for the full traceback.</p>\"\n",
" + '</div>');\n",
" throw error;\n",
" }\n",
" const el = document.getElementById('altair-viz');\n",
" vegaEmbed(\"#altair-viz\", spec, embedOpt)\n",
" .catch(error => showError(el, error));\n",
"\n",
" </script>\n",
"</body>\n",
"</html>"
]
},
"metadata": {
"tags": []
},
"execution_count": 1
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "kb2tZKYCexra",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment