Skip to content

Instantly share code, notes, and snippets.

@mlzxy
Last active January 17, 2021 12:54
Show Gist options
  • Save mlzxy/99671603903b02e60cc0a3ae014f4edd to your computer and use it in GitHub Desktop.
Save mlzxy/99671603903b02e60cc0a3ae014f4edd to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 117,
"metadata": {},
"outputs": [],
"source": [
"from numpy import mean, std\n",
"import numpy as np\n",
"from math import sqrt\n",
"import numpy.random as npr\n",
"import scipy.stats as stats\n",
"from tqdm import tqdm\n",
"import hvplot.pandas\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 160,
"metadata": {},
"outputs": [],
"source": [
"N = 1000\n",
"n = 20\n",
"a = 0.05\n",
"μ_required = 1.\n",
"Z = abs(stats.t.ppf(a, n-1))\n",
"\n",
"def make_a_feature():\n",
" μμ = 0.5\n",
" σμ = 0.5\n",
" μ = npr.normal(μμ, σμ)\n",
" σ = npr.exponential(scale=σμ)\n",
" return μ, σ"
]
},
{
"cell_type": "code",
"execution_count": 161,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 1000/1000 [00:00<00:00, 7965.15it/s]\n"
]
}
],
"source": [
"bias_features = []\n",
"bias_estimates = []\n",
"unbias_features = []\n",
"unbias_estimates = []\n",
"\n",
"for _ in tqdm(range(N)):\n",
" μ, σ = make_a_feature()\n",
" samples = [npr.normal(μ, σ) for _ in range(n)]\n",
" s = std(samples)\n",
" m = mean(samples)\n",
" reject_h0 = (sqrt(n) * (m - μ_required) / s) > Z # could use this feature\n",
" if reject_h0:\n",
" if μ < s * Z / sqrt(n) + μ_required:\n",
" bias_features.append((μ, σ))\n",
" bias_estimates.append((m, s))\n",
" else:\n",
" unbias_features.append((μ, σ))\n",
" unbias_estimates.append((m, s))"
]
},
{
"cell_type": "code",
"execution_count": 147,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 20/20 [00:18<00:00, 1.09it/s]\n"
]
}
],
"source": [
"num_unbias_plot_data = []\n",
"for n in tqdm(range(20, 601, 30)):\n",
" bias = 0\n",
" unbias = 0\n",
" for _ in range(N):\n",
" μ, σ = make_a_feature()\n",
" samples = [npr.normal(μ, σ) for _ in range(n)]\n",
" s = std(samples)\n",
" m = mean(samples)\n",
" reject_h0 = (sqrt(n) * (m - μ_required) / s) > Z # could use this feature\n",
" if reject_h0:\n",
" if μ < s * Z / sqrt(n) + μ_required:\n",
" bias += 1\n",
" else:\n",
" unbias += 1\n",
" num_unbias_plot_data.append((n, bias, unbias))"
]
},
{
"cell_type": "code",
"execution_count": 148,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame(num_unbias_plot_data, columns=['Sample Num', 'Num of Bias Feat', 'other'])"
]
},
{
"cell_type": "code",
"execution_count": 149,
"metadata": {},
"outputs": [
{
"data": {},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.holoviews_exec.v0+json": "",
"text/html": [
"<div id='4310'>\n",
"\n",
"\n",
"\n",
"\n",
"\n",
" <div class=\"bk-root\" id=\"8c88ca93-bfd1-400c-8197-391cabe7a1fd\" data-root-id=\"4310\"></div>\n",
"</div>\n",
"<script type=\"application/javascript\">(function(root) {\n",
" function embed_document(root) {\n",
" var docs_json = {\"714e86a7-6971-43da-9141-fbd48a241e96\":{\"roots\":{\"references\":[{\"attributes\":{},\"id\":\"4322\",\"type\":\"LinearScale\"},{\"attributes\":{\"below\":[{\"id\":\"4324\"}],\"center\":[{\"id\":\"4327\"},{\"id\":\"4331\"}],\"left\":[{\"id\":\"4328\"}],\"margin\":[5,5,5,5],\"min_border_bottom\":10,\"min_border_left\":10,\"min_border_right\":10,\"min_border_top\":10,\"plot_height\":300,\"plot_width\":700,\"renderers\":[{\"id\":\"4351\"}],\"sizing_mode\":\"fixed\",\"title\":{\"id\":\"4316\"},\"toolbar\":{\"id\":\"4338\"},\"x_range\":{\"id\":\"4312\"},\"x_scale\":{\"id\":\"4320\"},\"y_range\":{\"id\":\"4313\"},\"y_scale\":{\"id\":\"4322\"}},\"id\":\"4315\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{},\"id\":\"4363\",\"type\":\"UnionRenderers\"},{\"attributes\":{},\"id\":\"4320\",\"type\":\"LinearScale\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_multi\":null,\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"4314\"},{\"id\":\"4332\"},{\"id\":\"4333\"},{\"id\":\"4334\"},{\"id\":\"4335\"},{\"id\":\"4336\"}]},\"id\":\"4338\",\"type\":\"Toolbar\"},{\"attributes\":{\"children\":[{\"id\":\"4311\"},{\"id\":\"4315\"},{\"id\":\"4373\"}],\"margin\":[0,0,0,0],\"name\":\"Row09284\",\"tags\":[\"embedded\"]},\"id\":\"4310\",\"type\":\"Row\"},{\"attributes\":{},\"id\":\"4336\",\"type\":\"ResetTool\"},{\"attributes\":{\"axis_label\":\"Sample Num\",\"bounds\":\"auto\",\"formatter\":{\"id\":\"4353\"},\"major_label_orientation\":\"horizontal\",\"ticker\":{\"id\":\"4325\"}},\"id\":\"4324\",\"type\":\"LinearAxis\"},{\"attributes\":{\"margin\":[5,5,5,5],\"name\":\"HSpacer09288\",\"sizing_mode\":\"stretch_width\"},\"id\":\"4311\",\"type\":\"Spacer\"},{\"attributes\":{},\"id\":\"4334\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"margin\":[5,5,5,5],\"name\":\"HSpacer09289\",\"sizing_mode\":\"stretch_width\"},\"id\":\"4373\",\"type\":\"Spacer\"},{\"attributes\":{},\"id\":\"4333\",\"type\":\"PanTool\"},{\"attributes\":{\"end\":22.9,\"reset_end\":22.9,\"reset_start\":0.09999999999999987,\"start\":0.09999999999999987,\"tags\":[[[\"Num of Bias Feat\",\"Num of Bias Feat\",null]]]},\"id\":\"4313\",\"type\":\"Range1d\"},{\"attributes\":{},\"id\":\"4332\",\"type\":\"SaveTool\"},{\"attributes\":{\"source\":{\"id\":\"4345\"}},\"id\":\"4352\",\"type\":\"CDSView\"},{\"attributes\":{\"overlay\":{\"id\":\"4337\"}},\"id\":\"4335\",\"type\":\"BoxZoomTool\"},{\"attributes\":{\"bottom_units\":\"screen\",\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"4337\",\"type\":\"BoxAnnotation\"},{\"attributes\":{\"line_color\":\"#1f77b3\",\"line_width\":2,\"x\":{\"field\":\"Sample Num\"},\"y\":{\"field\":\"Num of Bias Feat\"}},\"id\":\"4348\",\"type\":\"Line\"},{\"attributes\":{\"line_alpha\":0.1,\"line_color\":\"#1f77b3\",\"line_width\":2,\"x\":{\"field\":\"Sample Num\"},\"y\":{\"field\":\"Num of Bias Feat\"}},\"id\":\"4349\",\"type\":\"Line\"},{\"attributes\":{\"axis_label\":\"Num of Bias Feat\",\"bounds\":\"auto\",\"formatter\":{\"id\":\"4355\"},\"major_label_orientation\":\"horizontal\",\"ticker\":{\"id\":\"4329\"}},\"id\":\"4328\",\"type\":\"LinearAxis\"},{\"attributes\":{\"callback\":null,\"renderers\":[{\"id\":\"4351\"}],\"tags\":[\"hv_created\"],\"tooltips\":[[\"Sample Num\",\"@{Sample_Num}\"],[\"Num of Bias Feat\",\"@{Num_of_Bias_Feat}\"]]},\"id\":\"4314\",\"type\":\"HoverTool\"},{\"attributes\":{\"data_source\":{\"id\":\"4345\"},\"glyph\":{\"id\":\"4348\"},\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"4350\"},\"nonselection_glyph\":{\"id\":\"4349\"},\"selection_glyph\":null,\"view\":{\"id\":\"4352\"}},\"id\":\"4351\",\"type\":\"GlyphRenderer\"},{\"attributes\":{},\"id\":\"4346\",\"type\":\"Selection\"},{\"attributes\":{\"text\":\"More Samples \\u2192 Less Bias\",\"text_color\":{\"value\":\"black\"},\"text_font_size\":{\"value\":\"12pt\"}},\"id\":\"4316\",\"type\":\"Title\"},{\"attributes\":{\"line_alpha\":0.2,\"line_color\":\"#1f77b3\",\"line_width\":2,\"x\":{\"field\":\"Sample Num\"},\"y\":{\"field\":\"Num of Bias Feat\"}},\"id\":\"4350\",\"type\":\"Line\"},{\"attributes\":{\"axis\":{\"id\":\"4324\"},\"grid_line_color\":null,\"ticker\":null},\"id\":\"4327\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"4329\",\"type\":\"BasicTicker\"},{\"attributes\":{\"end\":590.0,\"reset_end\":590.0,\"reset_start\":20.0,\"start\":20.0,\"tags\":[[[\"Sample Num\",\"Sample Num\",null]]]},\"id\":\"4312\",\"type\":\"Range1d\"},{\"attributes\":{},\"id\":\"4353\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"data\":{\"Num of Bias Feat\":[21,12,10,6,3,8,4,3,4,5,6,3,2,5,5,6,5,5,4,2],\"Num_of_Bias_Feat\":[21,12,10,6,3,8,4,3,4,5,6,3,2,5,5,6,5,5,4,2],\"Sample Num\":[20,50,80,110,140,170,200,230,260,290,320,350,380,410,440,470,500,530,560,590],\"Sample_Num\":[20,50,80,110,140,170,200,230,260,290,320,350,380,410,440,470,500,530,560,590]},\"selected\":{\"id\":\"4346\"},\"selection_policy\":{\"id\":\"4363\"}},\"id\":\"4345\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"4325\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"4355\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"axis\":{\"id\":\"4328\"},\"dimension\":1,\"grid_line_color\":null,\"ticker\":null},\"id\":\"4331\",\"type\":\"Grid\"}],\"root_ids\":[\"4310\"]},\"title\":\"Bokeh Application\",\"version\":\"2.2.3\"}};\n",
" var render_items = [{\"docid\":\"714e86a7-6971-43da-9141-fbd48a241e96\",\"root_ids\":[\"4310\"],\"roots\":{\"4310\":\"8c88ca93-bfd1-400c-8197-391cabe7a1fd\"}}];\n",
" root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
" }\n",
" if (root.Bokeh !== undefined && root.Bokeh.Panel !== undefined) {\n",
" embed_document(root);\n",
" } else {\n",
" var attempts = 0;\n",
" var timer = setInterval(function(root) {\n",
" if (root.Bokeh !== undefined && root.Bokeh.Panel !== undefined) {\n",
" clearInterval(timer);\n",
" embed_document(root);\n",
" } else if (document.readyState == \"complete\") {\n",
" attempts++;\n",
" if (attempts > 100) {\n",
" clearInterval(timer);\n",
" console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
" }\n",
" }\n",
" }, 10, root)\n",
" }\n",
"})(window);</script>"
],
"text/plain": [
":Curve [Sample Num] (Num of Bias Feat)"
]
},
"execution_count": 149,
"metadata": {
"application/vnd.holoviews_exec.v0+json": {
"id": "4310"
}
},
"output_type": "execute_result"
}
],
"source": [
"df.hvplot.line(x='Sample Num', y='Num of Bias Feat', title='More Samples → Less Bias')"
]
},
{
"cell_type": "code",
"execution_count": 150,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame(list(zip(abs(np.array(unbias_estimates)[:, 0] - np.array(unbias_features)[:, 0]),\n",
" abs(np.array(bias_estimates)[:, 0] - np.array(bias_features)[:, 0]))), columns=['Est Err of Unbias Feat', 'Est Err of Bias Feat'])"
]
},
{
"cell_type": "code",
"execution_count": 151,
"metadata": {},
"outputs": [
{
"data": {},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.holoviews_exec.v0+json": "",
"text/html": [
"<div id='4415'>\n",
"\n",
"\n",
"\n",
"\n",
"\n",
" <div class=\"bk-root\" id=\"587bcd04-8fc3-4847-b985-c731f91834c3\" data-root-id=\"4415\"></div>\n",
"</div>\n",
"<script type=\"application/javascript\">(function(root) {\n",
" function embed_document(root) {\n",
" var docs_json = {\"7d30c3be-f253-4bfe-90a3-58362c67f60c\":{\"roots\":{\"references\":[{\"attributes\":{\"click_policy\":\"mute\",\"items\":[{\"id\":\"4472\"},{\"id\":\"4491\"}],\"location\":[0,0],\"title\":\"Variable\"},\"id\":\"4471\",\"type\":\"Legend\"},{\"attributes\":{},\"id\":\"4439\",\"type\":\"PanTool\"},{\"attributes\":{\"callback\":null,\"renderers\":[{\"id\":\"4461\"},{\"id\":\"4479\"}],\"tags\":[\"hv_created\"],\"tooltips\":[[\"Variable\",\"@{Variable}\"],[\"value\",\"@{value}\"],[\"Density\",\"@{value_density}\"]]},\"id\":\"4419\",\"type\":\"HoverTool\"},{\"attributes\":{\"source\":{\"id\":\"4455\"}},\"id\":\"4462\",\"type\":\"CDSView\"},{\"attributes\":{\"fill_alpha\":0.2,\"fill_color\":\"#1f77b3\",\"line_alpha\":0.2,\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"4460\",\"type\":\"Patch\"},{\"attributes\":{\"data\":{\"x\":{\"__ndarray__\":\"MsJEoEIRsL//i4MyXwavv5uTfSQ56q2/Npt3FhPOrL/SonEI7bGrv22qa/rGlaq/CLJl7KB5qb+kuV/eel2ovz/BWdBUQae/2shTwi4lpr920E20CAmlvxHYR6bi7KO/rN9BmLzQor9I5zuKlrShv+PuNXxwmKC//uxf3JT4nr80/FPASMCcv2oLSKT8h5q/oho8iLBPmL/YKTBsZBeWvxA5JFAY35O/RkgYNMymkb/4rhgwAN2Ov2jNAPhnbIq/1Ovov8/7hb9ACtGHN4uBv2BRcp8+NXq/OI5CLw5Ucb8gliV+u+VgvwAEPkeslBs/YHaZQgafYj9gfnyRszByP4BBrAHkEXs/UALuOIr5gT/o4wVxImqGP3jFHam62oo/CKc14VJLjz9QxKaM9d2RPxi1sqhBFpQ/4KW+xI1Olj+olsrg2YaYP3SH1vwlv5o/PHjiGHL3nD8Eae40vi+fP+gsfSgFtKA/TCWDNivQoT+wHYlEUeyiPxYWj1J3CKQ/eg6VYJ0k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WQOquhwjfCRdARIM3s2HrF0DMbGnLlOUYQF3qAhmr+hlAx47zBwosG0C2E0EHJnocQO7xmmdh5B1A2v+cX+9oH0BaYjazXoIgQMciN76yWSFAdU0UEZY3IkCW8B5JhhgjQAot+KRq+CNA/PoJA6vSJEA3WeqTT6IlQNufX4goYiZAFmgbSvwMJ0AsyUsjup0nQFKwuK+uDyhAunEYG7deKEBttSkXcIcoQGjQt51dhyhAQGpf/AhdKEACihxIEwgoQNJHFB47iSdAelqhclXiJkBx//wKOhYmQEXAfQWlKCVAKa79dw8eJECRQ96fgfsiQH2nLF1hxiFArXAeuj+EIEDCyCgsUHUeQGRFPHDm3RtAaEekN0BMGUBn4wCRZ8kWQB4kvfRBXRRA4Kfdw28OEkB9rFEgd8QPQIp02gswuQtAuM2tcVUACEDFta5cw5wEQMQrxI4ijwFAeKlxcGCs/T/L8+HAGd74P0+e8locq/Q/KWq/gR8J8T8L5L8OANnrP+m3/LCZkeY/6vss7YUi4j+JSRXL2uTcP4ohTtCk0dY/oIPhkCnd0T8ce0vtQbnLP6+NwQ1pUsU/aLEDqXpAwD+ELuZ0QI24Pw+35xuGYLI/V1k+/mFCqz/DqOCtEAikPw==\",\"dtype\":\"float64\",\"order\":\"little\",\"shape\":[200]}},\"selected\":{\"id\":\"4474\"},\"selection_policy\":{\"id\":\"4489\"}},\"id\":\"4473\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"margin\":[5,5,5,5],\"name\":\"HSpacer09510\",\"sizing_mode\":\"stretch_width\"},\"id\":\"4582\",\"type\":\"Spacer\"},{\"attributes\":{\"margin\":[5,5,5,5],\"name\":\"HSpacer09509\",\"sizing_mode\":\"stretch_width\"},\"id\":\"4416\",\"type\":\"Spacer\"},{\"attributes\":{\"text\":\"Histogram of Esimation Error\",\"text_color\":{\"value\":\"black\"},\"text_font_size\":{\"value\":\"12pt\"}},\"id\":\"4422\",\"type\":\"Title\"},{\"attributes\":{\"fill_alpha\":0.5,\"fill_color\":\"#1f77b3\",\"line_alpha\":1,\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"4459\",\"type\":\"Patch\"},{\"attributes\":{\"below\":[{\"id\":\"4430\"}],\"center\":[{\"id\":\"4433\"},{\"id\":\"4437\"}],\"left\":[{\"id\":\"4434\"}],\"margin\":[5,5,5,5],\"min_border_bottom\":10,\"min_border_left\":10,\"min_border_right\":10,\"min_border_top\":10,\"plot_height\":300,\"plot_width\":700,\"renderers\":[{\"id\":\"4461\"},{\"id\":\"4479\"}],\"right\":[{\"id\":\"4471\"}],\"sizing_mode\":\"fixed\",\"title\":{\"id\":\"4422\"},\"toolbar\":{\"id\":\"4444\"},\"x_range\":{\"id\":\"4417\"},\"x_scale\":{\"id\":\"4426\"},\"y_range\":{\"id\":\"4418\"},\"y_scale\":{\"id\":\"4428\"}},\"id\":\"4421\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{},\"id\":\"4453\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"end\":0.1518569639618095,\"reset_end\":0.1518569639618095,\"reset_start\":-0.06276337063605622,\"start\":-0.06276337063605622,\"tags\":[[[\"value\",\"value\",null]]]},\"id\":\"4417\",\"type\":\"Range1d\"},{\"attributes\":{\"data_source\":{\"id\":\"4455\"},\"glyph\":{\"id\":\"4458\"},\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"4460\"},\"nonselection_glyph\":{\"id\":\"4459\"},\"selection_glyph\":null,\"view\":{\"id\":\"4462\"}},\"id\":\"4461\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"axis_label\":\"value\",\"bounds\":\"auto\",\"formatter\":{\"id\":\"4451\"},\"major_label_orientation\":\"horizontal\",\"ticker\":{\"id\":\"4431\"}},\"id\":\"4430\",\"type\":\"LinearAxis\"},{\"attributes\":{\"label\":{\"value\":\"Est Err of Unbias Feat\"},\"renderers\":[{\"id\":\"4461\"}]},\"id\":\"4472\",\"type\":\"LegendItem\"},{\"attributes\":{\"end\":27.47294481382679,\"reset_end\":27.47294481382679,\"reset_start\":0.0,\"tags\":[[[\"value_density\",\"Density\",null]]]},\"id\":\"4418\",\"type\":\"Range1d\"},{\"attributes\":{\"fill_alpha\":0.5,\"fill_color\":\"#1f77b3\",\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"4458\",\"type\":\"Patch\"},{\"attributes\":{},\"id\":\"4456\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"4451\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"fill_alpha\":0.5,\"fill_color\":\"#ff7e0e\",\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"4476\",\"type\":\"Patch\"},{\"attributes\":{\"axis\":{\"id\":\"4434\"},\"dimension\":1,\"grid_line_color\":null,\"ticker\":null},\"id\":\"4437\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"4426\",\"type\":\"LinearScale\"},{\"attributes\":{\"data_source\":{\"id\":\"4473\"},\"glyph\":{\"id\":\"4476\"},\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"4478\"},\"nonselection_glyph\":{\"id\":\"4477\"},\"selection_glyph\":null,\"view\":{\"id\":\"4480\"}},\"id\":\"4479\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"axis\":{\"id\":\"4430\"},\"grid_line_color\":null,\"ticker\":null},\"id\":\"4433\",\"type\":\"Grid\"},{\"attributes\":{\"fill_alpha\":0.5,\"fill_color\":\"#ff7e0e\",\"line_alpha\":1,\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"4477\",\"type\":\"Patch\"},{\"attributes\":{},\"id\":\"4474\",\"type\":\"Selection\"},{\"attributes\":{\"bottom_units\":\"screen\",\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"4443\",\"type\":\"BoxAnnotation\"},{\"attributes\":{\"source\":{\"id\":\"4473\"}},\"id\":\"4480\",\"type\":\"CDSView\"},{\"attributes\":{},\"id\":\"4431\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"4428\",\"type\":\"LinearScale\"},{\"attributes\":{\"axis_label\":\"Density\",\"bounds\":\"auto\",\"formatter\":{\"id\":\"4453\"},\"major_label_orientation\":\"horizontal\",\"ticker\":{\"id\":\"4435\"}},\"id\":\"4434\",\"type\":\"LinearAxis\"},{\"attributes\":{\"fill_alpha\":0.2,\"fill_color\":\"#ff7e0e\",\"line_alpha\":0.2,\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"4478\",\"type\":\"Patch\"},{\"attributes\":{},\"id\":\"4435\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"4440\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"label\":{\"value\":\"Est Err of Bias Feat\"},\"renderers\":[{\"id\":\"4479\"}]},\"id\":\"4491\",\"type\":\"LegendItem\"},{\"attributes\":{},\"id\":\"4438\",\"type\":\"SaveTool\"},{\"attributes\":{},\"id\":\"4469\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"overlay\":{\"id\":\"4443\"}},\"id\":\"4441\",\"type\":\"BoxZoomTool\"},{\"attributes\":{},\"id\":\"4442\",\"type\":\"ResetTool\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_multi\":null,\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"4419\"},{\"id\":\"4438\"},{\"id\":\"4439\"},{\"id\":\"4440\"},{\"id\":\"4441\"},{\"id\":\"4442\"}]},\"id\":\"4444\",\"type\":\"Toolbar\"},{\"attributes\":{\"data\":{\"x\":{\"__ndarray__\":\"gmU1Ks4an7+Tj7iBvBiev6S5O9mqFp2/tOO+MJkUnL/FDUKIhxKbv9Y3xd91EJq/52FIN2QOmb/4i8uOUgyYvw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QK7rj2T9j/WiidcHTP8T7TKuD4s0/UlvhtIlnxj9XKJyV8qXAPw==\",\"dtype\":\"float64\",\"order\":\"little\",\"shape\":[200]}},\"selected\":{\"id\":\"4456\"},\"selection_policy\":{\"id\":\"4469\"}},\"id\":\"4455\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"4489\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"children\":[{\"id\":\"4416\"},{\"id\":\"4421\"},{\"id\":\"4582\"}],\"margin\":[0,0,0,0],\"name\":\"Row09505\",\"tags\":[\"embedded\"]},\"id\":\"4415\",\"type\":\"Row\"}],\"root_ids\":[\"4415\"]},\"title\":\"Bokeh Application\",\"version\":\"2.2.3\"}};\n",
" var render_items = [{\"docid\":\"7d30c3be-f253-4bfe-90a3-58362c67f60c\",\"root_ids\":[\"4415\"],\"roots\":{\"4415\":\"587bcd04-8fc3-4847-b985-c731f91834c3\"}}];\n",
" root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
" }\n",
" if (root.Bokeh !== undefined && root.Bokeh.Panel !== undefined) {\n",
" embed_document(root);\n",
" } else {\n",
" var attempts = 0;\n",
" var timer = setInterval(function(root) {\n",
" if (root.Bokeh !== undefined && root.Bokeh.Panel !== undefined) {\n",
" clearInterval(timer);\n",
" embed_document(root);\n",
" } else if (document.readyState == \"complete\") {\n",
" attempts++;\n",
" if (attempts > 100) {\n",
" clearInterval(timer);\n",
" console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
" }\n",
" }\n",
" }, 10, root)\n",
" }\n",
"})(window);</script>"
],
"text/plain": [
":NdOverlay [Variable]\n",
" :Distribution [value] (Density)"
]
},
"execution_count": 151,
"metadata": {
"application/vnd.holoviews_exec.v0+json": {
"id": "4415"
}
},
"output_type": "execute_result"
}
],
"source": [
"df.hvplot.density(y=['Est Err of Unbias Feat', 'Est Err of Bias Feat'], title='Histogram of Esimation Error')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 162,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 1000/1000 [09:39<00:00, 1.73it/s]\n"
]
}
],
"source": [
"n = 200000\n",
"bias = 0\n",
"unbias = 0\n",
"for _ in tqdm(range(N)):\n",
" μ, σ = make_a_feature()\n",
" samples = [npr.normal(μ, σ) for _ in range(n)]\n",
" s = std(samples)\n",
" m = mean(samples)\n",
" reject_h0 = (sqrt(n) * (m - μ_required) / s) > Z # could use this feature\n",
" if reject_h0:\n",
" if μ < s * Z / sqrt(n) + μ_required:\n",
" bias += 1\n",
" else:\n",
" unbias += 1"
]
},
{
"cell_type": "code",
"execution_count": 163,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 163,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bias # Enough Sampling => No Bias!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
@mlzxy
Copy link
Author

mlzxy commented Jan 17, 2021

bokeh_plot (3)
bokeh_plot (4)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment