Skip to content

Instantly share code, notes, and snippets.

@daxinniu
Created November 21, 2020 20:56
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 daxinniu/0813d7f5399ea90b07e009cd47035dd1 to your computer and use it in GitHub Desktop.
Save daxinniu/0813d7f5399ea90b07e009cd47035dd1 to your computer and use it in GitHub Desktop.
Simulation_example
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"using Distributions, Random"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Suppose we have a distribution $\\nu = \\mathrm{Unif}([0,1])$. Let's draw 10 observations from the distribution and compute the mean."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10-element Array{Float64,1}:\n",
" 0.6094400915124818\n",
" 0.17083209035029978\n",
" 0.0848438283876205\n",
" 0.19735306058651214\n",
" 0.7426141868026233\n",
" 0.3776925769994388\n",
" 0.007934450314826202\n",
" 0.6488288212060249\n",
" 0.18491729829383674\n",
" 0.7119689348932787"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"observations = rand(10)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.3736425339346942"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mean(observations)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"From our knowledge of the distribution, we know that the true underlying mean of the distribution is 0.5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Draw 10 observations for just one time does not give us a good enough approximation."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can repeat the above process for 1000000 times and see if the simulated mean is close to the true mean of the distribution."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.5000015089902228"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"simulation_mean = mean([mean(rand(10)) for i in 1:1000000])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that the simulated mean for 1000000 times with 10 observations each time get us very close to the true underlying mean of the distribution."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.5.1",
"language": "julia",
"name": "julia-1.5"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment