Guest User

Untitled

a guest
Feb 25th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Mode of normal-inverse Chi-squared distribution\n",
  8. "\n",
  9. "The formula given in murphy seems to be wrong."
  10. ]
  11. },
  12. {
  13. "cell_type": "code",
  14. "execution_count": 4,
  15. "metadata": {
  16. "collapsed": true
  17. },
  18. "outputs": [],
  19. "source": [
  20. "using Distributions, ConjugatePriors\n",
  21. "using ConjugatePriors: NormalInverseChisq"
  22. ]
  23. },
  24. {
  25. "cell_type": "code",
  26. "execution_count": 5,
  27. "metadata": {},
  28. "outputs": [
  29. {
  30. "data": {
  31. "text/plain": [
  32. "ConjugatePriors.NormalInverseChisq{Float64}(μ=0.0, σ2=10.0, κ=2.0, ν=3.0)"
  33. ]
  34. },
  35. "execution_count": 5,
  36. "metadata": {},
  37. "output_type": "execute_result"
  38. }
  39. ],
  40. "source": [
  41. "nix = NormalInverseChisq(0., 10., 2., 3.)"
  42. ]
  43. },
  44. {
  45. "cell_type": "code",
  46. "execution_count": 20,
  47. "metadata": {},
  48. "outputs": [
  49. {
  50. "data": {
  51. "text/plain": [
  52. "(0.0, 15.0)"
  53. ]
  54. },
  55. "execution_count": 20,
  56. "metadata": {},
  57. "output_type": "execute_result"
  58. }
  59. ],
  60. "source": [
  61. "μ, σ2 = mode(nix)"
  62. ]
  63. },
  64. {
  65. "cell_type": "code",
  66. "execution_count": 21,
  67. "metadata": {},
  68. "outputs": [
  69. {
  70. "data": {
  71. "text/plain": [
  72. "0.0040313337318566775"
  73. ]
  74. },
  75. "execution_count": 21,
  76. "metadata": {},
  77. "output_type": "execute_result"
  78. }
  79. ],
  80. "source": [
  81. "pdf(nix, μ, σ2)"
  82. ]
  83. },
  84. {
  85. "cell_type": "code",
  86. "execution_count": 23,
  87. "metadata": {},
  88. "outputs": [
  89. {
  90. "data": {
  91. "text/plain": [
  92. "(false, true)"
  93. ]
  94. },
  95. "execution_count": 23,
  96. "metadata": {},
  97. "output_type": "execute_result"
  98. }
  99. ],
  100. "source": [
  101. "pdf.(nix, μ, σ2 .+ (-0.001, 0.001)) .< pdf(nix, μ, σ2)"
  102. ]
  103. },
  104. {
  105. "cell_type": "markdown",
  106. "metadata": {},
  107. "source": [
  108. "## Math\n",
  109. "\n",
  110. "The mode for the mean is clearly $\\mu_0$.\n",
  111. "\n",
  112. "Let $f(\\sigma^2)$ be the pdf as a function of the variance. Then based on Murphy (125),\n",
  113. "\n",
  114. "\\begin{align}\n",
  115. "f(x) \\propto& x^{-1/2} x^{-(\\nu/2 + 1)} \\exp\\left( \\frac{-1}{2} x^{-1} (\\nu\\sigma^2_0 + \\kappa_0(\\mu_0-\\mu)^2) \\right) \\\\\n",
  116. " \\propto& x^{-\\frac{\\nu+3}{2}} \\exp\\left(\\frac{-1}{2} x^{-1} \\nu\\sigma^2_0 \\right)\n",
  117. "\\end{align}\n",
  118. "\n",
  119. "(dropping constants since we're solving for $f^\\prime = 0$).\n",
  120. "\n",
  121. "Let\n",
  122. "\\begin{align}\n",
  123. "A &= -\\frac{\\nu+3}{2} \\\\\n",
  124. "B &= -\\frac{\\nu\\sigma^2_0}{2}\n",
  125. "\\end{align}\n",
  126. "\n",
  127. "Then $f(x) \\propto x^A \\exp(Bx^{-1})$ and $f^\\prime(x) \\propto A x^{A-1} \\exp(Bx^{-1}) - x^A Bx^{-2}\\exp(Bx^{-1})$. To find the mode,\n",
  128. "\n",
  129. "\\begin{align}\n",
  130. "f^\\prime(x) &= 0 \\\\\n",
  131. "Ax^{A-1} \\exp(Bx^{-1}) &= x^A Bx^{-2} \\exp(Bx^{-1}) \\\\\n",
  132. "A x^{-1} &= Bx^{-2} \\\\\n",
  133. "x &= \\frac{B}{A} \\\\\n",
  134. " &= \\frac{\\nu\\sigma^2_0}{\\nu+3}\n",
  135. "\\end{align}\n"
  136. ]
  137. },
  138. {
  139. "cell_type": "code",
  140. "execution_count": 25,
  141. "metadata": {},
  142. "outputs": [
  143. {
  144. "data": {
  145. "text/plain": [
  146. "(0.0, 5.0)"
  147. ]
  148. },
  149. "execution_count": 25,
  150. "metadata": {},
  151. "output_type": "execute_result"
  152. }
  153. ],
  154. "source": [
  155. "mode2(d::NormalInverseChisq) = (d.μ, d.ν * d.σ2 / (d.ν + 3))\n",
  156. "μ, σ2 = mode2(nix)"
  157. ]
  158. },
  159. {
  160. "cell_type": "code",
  161. "execution_count": 26,
  162. "metadata": {},
  163. "outputs": [
  164. {
  165. "data": {
  166. "text/plain": [
  167. "0.014730705695397627"
  168. ]
  169. },
  170. "execution_count": 26,
  171. "metadata": {},
  172. "output_type": "execute_result"
  173. }
  174. ],
  175. "source": [
  176. "pdf(nix, μ, σ2)"
  177. ]
  178. },
  179. {
  180. "cell_type": "code",
  181. "execution_count": 27,
  182. "metadata": {},
  183. "outputs": [
  184. {
  185. "data": {
  186. "text/plain": [
  187. "(true, true)"
  188. ]
  189. },
  190. "execution_count": 27,
  191. "metadata": {},
  192. "output_type": "execute_result"
  193. }
  194. ],
  195. "source": [
  196. "pdf.(nix, μ, σ2 .+ (-0.001, 0.001)) .< pdf(nix, μ, σ2)"
  197. ]
  198. },
  199. {
  200. "cell_type": "markdown",
  201. "metadata": {},
  202. "source": [
  203. "# Normal-Inverse gamma prior\n",
  204. "\n",
  205. "This is equivalent with the following (relevant) re-parametrizations:\n",
  206. "\n",
  207. "\\begin{align}\n",
  208. "a_0 &= \\nu_0/2 \\\\ \n",
  209. "b_0 &= \\nu_0\\sigma^2_0 / 2\n",
  210. "\\end{align}\n",
  211. "\n",
  212. "where $a_0$ is the shape parameter and $b_0$ is the scale. With this parametrization, the mode is $\\frac{b_0}{a_0 + 3/2}$"
  213. ]
  214. }
  215. ],
  216. "metadata": {
  217. "kernelspec": {
  218. "display_name": "Julia 0.6.2",
  219. "language": "julia",
  220. "name": "julia-0.6"
  221. },
  222. "language_info": {
  223. "file_extension": ".jl",
  224. "mimetype": "application/julia",
  225. "name": "julia",
  226. "version": "0.6.2"
  227. }
  228. },
  229. "nbformat": 4,
  230. "nbformat_minor": 2
  231. }
Add Comment
Please, Sign In to add comment