Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 185 additions & 0 deletions Assignment_3/200179_Apurb.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "ecee66c0-86f9-44ac-aa52-79d3b58f840d",
"metadata": {},
"source": [
"# Question 1"
]
},
{
"cell_type": "markdown",
"id": "9e0814b1-fe53-40a9-8578-1b4819ff18e8",
"metadata": {},
"source": [
"Here $\\pi$ denotes standard $Cauchy$ and $g$ denotes standard $Normal$ distributions respectively $\\newline$\n",
"$\\inf_{z\\in X} \\frac{\\pi(z)}{g(z)}$ where $X$ is the support of the distributions occurs at $z=1$ which can be shown using elementary Calculus $\\newline$\n",
"$m = \\inf_{z\\in X} \\frac{\\pi(z)}{g(z)}$ $\\newline$\n",
"Expectation of $z^{2}$ wrt $\\pi$ (denoted as $E_1$) is undefined $\\newline$\n",
"Expectation of $z$ wrt $\\pi$ is equal to expectation of $\\frac{z.\\pi(z)}{g(z)}$ wrt $g$ (denoted by $E_3$) $\\newline$\n",
"Expectation of $\\frac{z^{2} .\\pi(z)^{2}}{g(z)^{2}}$ wrt $g$ (denoted as $E_2$) is equal to $\\newline$\n",
"$E_2 = \\int_X \\frac{z^{2} .\\pi(z)^{2}}{g(z)^{2}}.g(z) \\,dx \\newline$\n",
"$E_2 >= m.E_1 \\newline$\n",
"Hence $E_2$ is undefined $\\newline$\n",
"We cannot apply CLT, hence variance is undefined"
]
},
{
"cell_type": "markdown",
"id": "523f542e-638a-4f78-8cbf-9ce92003c031",
"metadata": {},
"source": [
"# Question 2"
]
},
{
"cell_type": "markdown",
"id": "5cefe374-fc45-4b17-8ebb-45c4de25f3bf",
"metadata": {},
"source": [
"a. The weighted importance estimator will also have a finite variance since there will only be a difference of factor of (b/a) in the inequality and, b and a although unknown are fixed quantitites. \\\n",
"b. One possible advantage of importance sampling over accept reject method could be that choosing the constant c in accept reject might be tricky. If not properly chosen, we may tend to reject too many samples due to which speed will be compromised."
]
},
{
"cell_type": "markdown",
"id": "b32ed4d5-9123-4f8d-a8e8-23186f8d500d",
"metadata": {},
"source": [
"# Question 3"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "1a491d87-2028-4f49-8f05-7468f5fcfaff",
"metadata": {},
"outputs": [],
"source": [
"using Distributions\n",
"using Random\n",
"\n",
"#Random.seed!(1)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "875c8371-60dc-4dfd-a781-a4e6ea0e7815",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"func (generic function with 1 method)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function func(t,y,v,n)\n",
" ans = 1\n",
" for i in 1:n\n",
" ans = ans * ((1 + ((t[i]-y)^2)/v)^(-(v+1)/2))\n",
" end\n",
" return ans\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "e72b658b-4cbd-4583-9e98-dcaab8f041b9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Sample (generic function with 1 method)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function Sample(t,v,n) #t is vector, v is another parameter\n",
" y = rand(Normal())\n",
" u = rand(Uniform())\n",
" while u>func(t,y,v,n)\n",
" y = rand(Normal())\n",
" u = rand(Uniform())\n",
" end\n",
" return y \n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3bfba61d-9fcf-403d-a7e0-cc2135dff338",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.7872618100163297e-35"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n = 50\n",
"v = 5\n",
"t = zeros(n)\n",
"for i in 1:n\n",
" t[i] = rand(TDist(v))\n",
"end\n",
"#Sample(t,v,n)\n",
"func(t,rand(Normal()),v,n)"
]
},
{
"cell_type": "markdown",
"id": "fed86942-1724-40ab-ba52-519449b21615",
"metadata": {},
"source": [
"# Question 4"
]
},
{
"cell_type": "markdown",
"id": "00d1ffa8-8002-4450-af02-5c0e1a9a7dc4",
"metadata": {},
"source": [
"$\\pi(\\lambda | Y_1,Y_2...Y_n) \\propto f(Y_1,Y_2...Y_n | \\lambda).\\pi(\\lambda) \\newline$\n",
"$\\pi(\\lambda | Y_1,Y_2...Y_n) \\propto \\frac{\\lambda^{\\alpha-1}.\\beta^{-\\alpha}.e^{-\\frac{\\lambda}{\\beta}}}{\\Gamma(\\alpha)}.\\Pi_{i=1}^{n} \\frac{e^{-\\lambda}. {\\lambda}^{y_i} }{y_i!} \\newline$\n",
"$\\pi(\\lambda | Y_1,Y_2...Y_n) \\propto e^{-(n+\\frac{1}{\\beta}).\\lambda}.\\lambda^{\\alpha-1+y_1+y_2....+y_n} \\newline$\n",
"By observation, $\\pi(\\lambda | Y_1,Y_2...Y_n) = Gamma(y_1+y_2...+y_n+\\alpha,\\frac{\\beta}{1+n.\\beta})$"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.7.2",
"language": "julia",
"name": "julia-1.7"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}