{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "4115575c-9616-43a6-bd88-ba08d15e7801",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"prob_trueisinci (generic function with 1 method)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Distributions\n",
"\n",
"tstat(μ, X) = (mean(X) - μ)/√(var(X) / length(X))\n",
"pvalue(μ, X) = 2ccdf(TDist(length(X) - 1), abs(tstat(μ, X)))\n",
"\n",
"function ci(X, α)\n",
" X̄ = mean(X)\n",
" c = quantile(TDist(length(X) - 1), 1 - α/2)\n",
" d = √(var(X) / length(X))\n",
" X̄ - c*d, X̄ + c*d\n",
"end\n",
"\n",
"isininterval(x, int) = first(int) ≤ x ≤ last(int)\n",
"isinci(μ, X, α) = isininterval(μ, ci(X, α))\n",
"\n",
"prob_trueisinci(dist, n, α; L=10^6) = mean(isinci(mean(dist), rand(dist, n), α) for _ in 1:L)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "3c10ebe7-526c-42a0-bc3f-c5731b86d1b9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.949866"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prob_trueisinci(Normal(), 20, 0.05)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "61cd77ca-c4a8-4a69-9623-f8010dc9a229",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.948872"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prob_trueisinci(Uniform(), 20, 0.05)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "5a429692-3673-4742-923b-137cf119b65d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.918784"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prob_trueisinci(Exponential(), 20, 0.05)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "98b443e1-be98-451c-b07b-a992c62f91a7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.795997"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prob_trueisinci(MixtureModel([Normal(), Normal(10, 1)], [0.95, 0.05]), 20, 0.05)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "6107f4e5-041f-4264-99e5-50df8f54d2a1",
"metadata": {},
"outputs": [],
"source": [
"using Plots"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "38772a82-ab20-47dc-8f57-e8f546d50aaa",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"normal = Normal(mean(Uniform()), std(Uniform()))\n",
"P = plot(x -> pdf(Uniform(), x), -1, 2; label=\"Uniform()\")\n",
"plot!(x -> pdf(normal, x), -1, 2; label=\"normal approx.\", ls=:dash)\n",
"title!(\"pdfs\")\n",
"Q = plot(x -> cdf(Uniform(), x), -1, 2; label=\"Uniform()\")\n",
"plot!(x -> cdf(normal, x), -1, 2; label=\"normal approx.\", ls=:dash)\n",
"title!(\"cdfs\"; legend=:bottomright)\n",
"plot(P, Q; size=(720, 300))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "5c829f95-4cd1-4c22-9155-36f4219b70ec",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"normal = Normal(mean(Exponential()), std(Exponential()))\n",
"P = plot(x -> pdf(Exponential(), x), -3, 6; label=\"Exponential()\")\n",
"plot!(x -> pdf(normal, x), -3, 6; label=\"normal approx.\", ls=:dash)\n",
"title!(\"pdfs\")\n",
"Q = plot(x -> cdf(Exponential(), x), -3, 6; label=\"Exponential()\")\n",
"plot!(x -> cdf(normal, x), -3, 6; label=\"normal approx.\", ls=:dash)\n",
"title!(\"cdfs\"; legend=:bottomright)\n",
"plot(P, Q; size=(720, 300))"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "51fb3a38-6c17-461a-b180-530dd5bcc1cf",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mixnormal = MixtureModel([Normal(), Normal(10, 1)], [0.95, 0.05])\n",
"P = plot(x -> pdf(mixnormal, x), -5, 15; label=\"mixnormal\")\n",
"plot!(x -> pdf(Normal(), x), -5, 15; label=\"Normal()\", ls=:dash)\n",
"title!(\"pdfs\")\n",
"Q = plot(x -> cdf(mixnormal, x), -5, 15; label=\"mixnormal\")\n",
"plot!(x -> cdf(Normal(), x), -5, 15; label=\"Normal()\", ls=:dash)\n",
"title!(\"cdfs\"; legend=:bottomright)\n",
"plot(P, Q; size=(720, 300))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "c1f62a3a-be94-487b-aab7-d83ec638e911",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mixnormal = MixtureModel([Normal(), Normal(10, 1)], [0.95, 0.05])\n",
"normal = Normal(mean(mixnormal), std(mixnormal))\n",
"P = plot(x -> pdf(mixnormal, x), -5, 15; label=\"mixnormal\")\n",
"plot!(x -> pdf(normal, x), -5, 15; label=\"normal approx.\", ls=:dash)\n",
"title!(\"pdfs\")\n",
"Q = plot(x -> cdf(mixnormal, x), -5, 15; label=\"mixnormal\")\n",
"plot!(x -> cdf(normal, x), -5, 15; label=\"normal approx.\", ls=:dash)\n",
"title!(\"cdfs\"; legend=:bottomright)\n",
"plot(P, Q; size=(720, 300))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "eaf8209b-d968-4386-b9c2-ae6699b9328b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"model = Normal{Float64}(μ=0.0, σ=1.0)\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
"