{"id":41,"date":"2022-02-18T15:27:02","date_gmt":"2022-02-18T15:27:02","guid":{"rendered":"http:\/\/www.pulsars.info\/wordpress\/?p=41"},"modified":"2022-02-18T15:27:02","modified_gmt":"2022-02-18T15:27:02","slug":"drawing-samples-from-numerical-probability-density","status":"publish","type":"post","link":"http:\/\/www.pulsars.info\/wordpress\/uncategorized\/drawing-samples-from-numerical-probability-density\/","title":{"rendered":"Drawing samples from numerical probability density"},"content":{"rendered":"\n<p>Quite often it happens in science that the probability distribution is set only numerically at some grid. In this case, the fastest option is to prepare the numerical cumulative distribution by summing each element of the array. The cumulative distribution is inverted by means of a spline fitting procedure where the arguments are rearranged. The x-axis becomes the cumulative probability and y-axis is the actual random variable. After these steps, a sample with a uniform distribution between zero and one is easily converted by means of the fitted function to a distribution with the required probability density.<\/p>\n\n\n\n<p>Here is an example in Python:<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"http:\/\/pulsars.info\/ignotur\/wp-content\/uploads\/2018\/01\/pdf-1.png\" alt=\"\" class=\"wp-image-163\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"http:\/\/pulsars.info\/ignotur\/wp-content\/uploads\/2018\/01\/cdf-1.png\" alt=\"\" class=\"wp-image-162\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"http:\/\/pulsars.info\/ignotur\/wp-content\/uploads\/2018\/01\/result-1.png\" alt=\"\" class=\"wp-image-164\"\/><\/figure><\/div>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>from<\/strong> scipy <strong>import<\/strong> interpolate\n<strong>import<\/strong> numpy <strong>as<\/strong> np\n<strong>from<\/strong> math <strong>import<\/strong> *\n<strong>import<\/strong> matplotlib <strong>as<\/strong> mpl\n<strong>import<\/strong> matplotlib.pyplot <strong>as<\/strong> plt\nplt.rc('font', family='serif')\nmpl.rcParams.update({'font.size': 12})\nmpl.rcParams.update({'legend.labelspacing':0.25, 'legend.fontsize': 12})\n\nt = np.linspace(0, pi\/2.0*1.5, 200) ## domain for numerical PDF is defined (grid)\n\nrand_val = np.random.normal(0, 0.1, size=200) \n\ny_diff = 5.3*np.sin (t) + rand_val\n\ny_diff = y_diff \/ np.sum(y_diff)   ## Actual numerical PDF\n\nplt.plot(t, y_diff)\nplt.xlabel(r'$u$')\nplt.ylabel(r'$p(u)du$')\nplt.savefig('pdf.png')\nplt.show()\n\ny_cumul = np.cumsum(y_diff, axis=0)  \ny_cumul = y_cumul \/ np.max(y_cumul)  ## Preparing numerical cumulative distribution\n\n\nplt.plot (t, y_cumul)\nplt.xlabel(r'$u$')\nplt.ylabel(r'$P(&lt;u)$')\nplt.savefig('cdf.png')\nplt.show()\n\nf = interpolate.interp1d(y_cumul, t, kind='cubic')  ## fit the inverse (exchanged arguments) cumulative\n\ny=np.random.uniform(np.min(y_cumul), np.max(y_cumul), size=10000) ## generate uniform distribution\n\nsample = f(y) ## transform the uniform distribution to a distribution with required PDF using the\n              ## spline interpolation\n\nn, bins, patches = plt.hist(sample, bins=100)\nplt.plot(t, y_diff \/ np.max(y_diff) * np.max(n))\nplt.xlabel(r'$u$')\nplt.ylabel('Number')\nplt.savefig('result.png')\nplt.show()\n<\/pre>\n\n\n\n<p>This is the actual numerical PDF: The cumulative distribution function: Here I draw a sample using the technique and compare it with the numerical PDF:&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Quite often it happens in science that the probability distribution is set only numerically at some grid. In this case, the fastest option is to&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"http:\/\/www.pulsars.info\/wordpress\/uncategorized\/drawing-samples-from-numerical-probability-density\/\">Continue Reading<span class=\"screen-reader-text\">Drawing samples from numerical probability density<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.pulsars.info\/wordpress\/wp-json\/wp\/v2\/posts\/41"}],"collection":[{"href":"http:\/\/www.pulsars.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.pulsars.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.pulsars.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.pulsars.info\/wordpress\/wp-json\/wp\/v2\/comments?post=41"}],"version-history":[{"count":1,"href":"http:\/\/www.pulsars.info\/wordpress\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"predecessor-version":[{"id":42,"href":"http:\/\/www.pulsars.info\/wordpress\/wp-json\/wp\/v2\/posts\/41\/revisions\/42"}],"wp:attachment":[{"href":"http:\/\/www.pulsars.info\/wordpress\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.pulsars.info\/wordpress\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.pulsars.info\/wordpress\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}