Include dependency graph for integral_approximation2.cpp:</div>
<divclass="dyncontent">
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d5/d5c/integral__approximation2_8cpp__incl.svg"width="607"height="112"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<trclass="memdesc:d0/da4/namespacemonte__carlo"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Functions for the <ahref="https://en.wikipedia.org/wiki/Monte_Carlo_integration"target="_blank">Monte Carlo Integration</a> implementation. <br/></td></tr>
<trclass="memdesc:a71249ee535f16f8ed2e9cc8f0199a2cf"><tdclass="mdescLeft"> </td><tdclass="mdescRight">short-hand for std::functions used in this implementation <ahref="../../db/d40/integral__approximation2_8cpp.html#a71249ee535f16f8ed2e9cc8f0199a2cf">More...</a><br/></td></tr>
<trclass="memitem:af7da9ba8932f1f48b9bbc2d80471af51"><tdclass="memItemLeft"align="right"valign="top">double </td><tdclass="memItemRight"valign="bottom"><aclass="el"href="../../db/d40/integral__approximation2_8cpp.html#af7da9ba8932f1f48b9bbc2d80471af51">math::monte_carlo::integral_monte_carlo</a> (const double &start_point, const Function &function, const Function &pdf, const uint32_t &num_samples=1000000)</td></tr>
<trclass="memdesc:af7da9ba8932f1f48b9bbc2d80471af51"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Compute an approximation of an integral using Monte Carlo integration. <ahref="../../db/d40/integral__approximation2_8cpp.html#af7da9ba8932f1f48b9bbc2d80471af51">More...</a><br/></td></tr>
<divclass="textblock"><p><ahref="https://en.wikipedia.org/wiki/Monte_Carlo_integration"target="_blank">Monte Carlo Integration</a></p>
<p>In mathematics, Monte Carlo integration is a technique for numerical integration using random numbers. It is a particular Monte Carlo method that numerically computes a definite integral. While other algorithms usually evaluate the integrand at a regular grid, Monte Carlo randomly chooses points at which the integrand is evaluated. This method is particularly useful for higher-dimensional integrals.</p>
<p>This implementation supports arbitrary pdfs. These pdfs are sampled using the <ahref="https://en.wikipedia.org/wiki/Metropolis–Hastings_algorithm"target="_blank">Metropolis-Hastings algorithm</a>. This can be swapped out by every other sampling techniques for example the inverse method. Metropolis-Hastings was chosen because it is the most general and can also be extended for a higher dimensional sampling space.</p>
<p>short-hand for std::functions used in this implementation </p>
<p>Generate samples according to some pdf</p>
<p>This function uses Metropolis-Hastings to generate random numbers. It generates a sequence of random numbers by using a markov chain. Therefore, we need to define a start_point and the number of samples we want to generate. Because the first samples generated by the markov chain may not be distributed according to the given pdf, one can specify how many samples should be discarded before storing samples. </p><dlclass="params"><dt>Parameters</dt><dd>
<tableclass="params">
<tr><tdclass="paramname">start_point</td><td>The starting point of the markov chain </td></tr>
<tr><tdclass="paramname">pdf</td><td>The pdf to sample </td></tr>
<tr><tdclass="paramname">num_samples</td><td>The number of samples to generate </td></tr>
<tr><tdclass="paramname">discard</td><td>How many samples should be discarded at the start </td></tr>
</table>
</dd>
</dl>
<dlclass="section return"><dt>Returns</dt><dd>A vector of size num_samples with samples distributed according to the pdf </dd></dl>
<divclass="line"><aid="l00064"name="l00064"></a><spanclass="lineno"> 64</span><spanclass="keywordflow">for</span>(uint32_t t = 0; t < num_samples + discard; ++t) {</div>
<divclass="line"><aid="l00065"name="l00065"></a><spanclass="lineno"> 65</span><spanclass="comment">// Generate a new proposal according to some mutation strategy.</span></div>
<divclass="line"><aid="l00066"name="l00066"></a><spanclass="lineno"> 66</span><spanclass="comment">// This is arbitrary and can be swapped.</span></div>
<divclass="line"><aid="l00071"name="l00071"></a><spanclass="lineno"> 71</span><spanclass="comment">// Accept "new state" according to the acceptance_probability</span></div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../db/d40/integral__approximation2_8cpp_a71249ee535f16f8ed2e9cc8f0199a2cf_cgraph.svg"width="355"height="139"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<p>Compute an approximation of an integral using Monte Carlo integration. </p>
<p>The integration domain [a,b] is given by the pdf. The pdf has to fulfill the following conditions: 1) for all x \in [a,b] : p(x) > 0 2) for all x \not\in [a,b] : p(x) = 0 3) \int_a^b p(x) dx = 1 </p><dlclass="params"><dt>Parameters</dt><dd>
<tableclass="params">
<tr><tdclass="paramname">start_point</td><td>The start point of the Markov Chain (see generate_samples) </td></tr>
<tr><tdclass="paramname">function</td><td>The function to integrate </td></tr>
<tr><tdclass="paramname">pdf</td><td>The pdf to sample </td></tr>
<tr><tdclass="paramname">num_samples</td><td>The number of samples used to approximate the integral </td></tr>
</table>
</dd>
</dl>
<dlclass="section return"><dt>Returns</dt><dd>The approximation of the integral according to 1/N \sum_{i}^N f(x_i) / p(x_i) </dd></dl>
<divclass="line"><aid="l00105"name="l00105"></a><spanclass="lineno"> 105</span><spanclass="keywordflow">return</span> integral / <spanclass="keyword">static_cast<</span><spanclass="keywordtype">double</span><spanclass="keyword">></span>(samples.size());</div>
<divclass="ttc"id="aintegral__approximation2_8cpp_html_a71249ee535f16f8ed2e9cc8f0199a2cf"><divclass="ttname"><ahref="../../db/d40/integral__approximation2_8cpp.html#a71249ee535f16f8ed2e9cc8f0199a2cf">math::monte_carlo::generate_samples</a></div><divclass="ttdeci">std::vector< double > generate_samples(const double &start_point, const Function &pdf, const uint32_t &num_samples, const uint32_t &discard=100000)</div><divclass="ttdoc">short-hand for std::functions used in this implementation</div><divclass="ttdef"><b>Definition:</b> integral_approximation2.cpp:53</div></div>
</div><!-- fragment --><divclass="dynheader">
Here is the call graph for this function:</div>
<divclass="dyncontent">
<divclass="center"><iframescrolling="no"frameborder="0"src="../../db/d40/integral__approximation2_8cpp_af7da9ba8932f1f48b9bbc2d80471af51_cgraph.svg"width="550"height="147"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../db/d40/integral__approximation2_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg"width="274"height="190"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<divclass="line"><aid="l00116"name="l00116"></a><spanclass="lineno"> 116</span><aclass="code hl_classRef"target="_blank"href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a><<<spanclass="stringliteral">"Disclaimer: Because this is a randomized algorithm,"</span><<<aclass="code hl_functionRef"target="_blank"href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<divclass="line"><aid="l00117"name="l00117"></a><spanclass="lineno"> 117</span><aclass="code hl_classRef"target="_blank"href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a><<<spanclass="stringliteral">"it may happen that singular samples deviate from the true result."</span><<<aclass="code hl_functionRef"target="_blank"href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a><<<aclass="code hl_functionRef"target="_blank"href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;;</div>
<divclass="line"><aid="l00146"name="l00146"></a><spanclass="lineno"> 146</span><aclass="code hl_classRef"target="_blank"href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a><<<spanclass="stringliteral">"This number should be close to 10.666666: "</span><< integral <<<aclass="code hl_functionRef"target="_blank"href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<divclass="line"><aid="l00170"name="l00170"></a><spanclass="lineno"> 170</span><aclass="code hl_classRef"target="_blank"href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a><<<spanclass="stringliteral">"This number should be close to 1.7182818: "</span><< integral <<<aclass="code hl_functionRef"target="_blank"href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<divclass="line"><aid="l00173"name="l00173"></a><spanclass="lineno"> 173</span><spanclass="comment"> This is a difficult integral because of its infinite domain.</span></div>
<divclass="line"><aid="l00174"name="l00174"></a><spanclass="lineno"> 174</span><spanclass="comment"> Therefore, it may deviate largely from the expected result.</span></div>
<divclass="line"><aid="l00186"name="l00186"></a><spanclass="lineno"> 186</span><aclass="code hl_classRef"target="_blank"href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a><<<spanclass="stringliteral">"This number should be close to 1.0: "</span><< integral <<<aclass="code hl_functionRef"target="_blank"href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<divclass="ttc"id="aintegral__approximation2_8cpp_html_af7da9ba8932f1f48b9bbc2d80471af51"><divclass="ttname"><ahref="../../db/d40/integral__approximation2_8cpp.html#af7da9ba8932f1f48b9bbc2d80471af51">math::monte_carlo::integral_monte_carlo</a></div><divclass="ttdeci">double integral_monte_carlo(const double &start_point, const Function &function, const Function &pdf, const uint32_t &num_samples=1000000)</div><divclass="ttdoc">Compute an approximation of an integral using Monte Carlo integration.</div><divclass="ttdef"><b>Definition:</b> integral_approximation2.cpp:97</div></div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../db/d40/integral__approximation2_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg"width="175"height="190"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<divid="nav-path"class="navpath"><!-- id is needed for treeview function! -->
<liclass="footer">Generated by <ahref="https://www.doxygen.org/index.html"><imgclass="footer"src="../../doxygen.svg"width="104"height="31"alt="doxygen"/></a> 1.9.2 </li>
<area shape="rect" id="node1" title="Compute an approximation of an integral using Monte Carlo integration." alt="" coords="5,78,152,119"/>
<area shape="rect" id="node2" href="$db/d40/integral__approximation2_8cpp.html#a71249ee535f16f8ed2e9cc8f0199a2cf" title="short-hand for std::functions used in this implementation" alt="" coords="200,49,340,90"/>
<gid="a_node2"><axlink:href="../../db/d40/integral__approximation2_8cpp.html#a71249ee535f16f8ed2e9cc8f0199a2cf"target="_top"xlink:title="short-hand for std::functions used in this implementation">
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.