<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>蒙特卡罗方法 &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/%E8%92%99%E7%89%B9%E5%8D%A1%E7%BD%97%E6%96%B9%E6%B3%95/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Mon, 27 Apr 2020 17:49:18 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创] 重要性采样／Importance Sampling</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e9%87%8d%e8%a6%81%e6%80%a7%e9%87%87%e6%a0%b7%ef%bc%8fimportance-sampling/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e9%87%8d%e8%a6%81%e6%80%a7%e9%87%87%e6%a0%b7%ef%bc%8fimportance-sampling/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Thu, 11 Jul 2019 12:17:07 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[Monte Carlo method]]></category>
		<category><![CDATA[蒙特卡洛方法]]></category>
		<category><![CDATA[蒙特卡罗方法]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=10022</guid>

					<description><![CDATA[<p>
查看本系列文章合集，请点击<a href="https://www.codelast.com/?p=9951" rel="noopener noreferrer" target="_blank"><span style="background-color: rgb(255, 160, 122);">这里</span></a>。</p>
<p>在前面的文章中，我们看到，随机采样是一个蒙特卡罗方法中很关键的步骤。而采样是需要技巧的，单纯地增加采样次数太没有效率了，比如说，如果随机采样一亿次，你可以把结果计算得特别精确，但是采样一亿次需要的时间非常长，长得远远超过了我们能接受的范围，这又有什么意义呢？<br />
人们发现，有一些方法可以让随机采的样本&#8220;特别好&#8221;。那么什么算&#8220;特别好&#8221;呢？比如说，本来使用没有任何原则的采样方法，需要采样1万个点，才能让计算出来的结果很接近真实值；现在使用一个&#8220;特别好&#8221;的采样方法，可以让我们只需要采样100个点，就可以让计算出来的结果很接近真实值了，这样就极大地减少了计算量。<br />
<span id="more-10022"></span><br />
而<span style="color:#0000ff;">重要性采样</span>（Importance Sampling），就是人们发现的、可以实现这个目的手段之一。</p>
<ul>
<li>
		<span style="background-color:#00ff00;">定义</span></li>
</ul>
<blockquote>
<p>
		重要性采样（Importance Sampling）是统计学中估计某一分布性质时使用的一种方法。该方法从与原分布不同的另一个分布中采样，而对原先分布的性质进行估计。</p>
</blockquote>
<p>乍一看，这句话可能有点抽象，别急，往后看你就理解了。</p>
<ul>
<li>
		<span style="background-color:#00ff00;">实例&#8212;&#8212;蒙特卡罗平均值法计算定积分</span></li>
</ul>
<p>在之前的文章中，我们已经见识过了用蒙特卡罗<span style="color:#0000ff;">投点法</span>计算定积分的过程，这里有另一个叫作&#8220;平均值法&#8221;的方法，由于它也是随机化的算法，因此，它也属于一种蒙特卡罗方法。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
我们来看看来自<a href="https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/monte-carlo-methods-in-practice/monte-carlo-integration" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">scratchapixel.com</span></a>的一幅图：<br />
<span style="text-align: center; height: 212px;"><img decoding="async" alt="monte carlo calculate definite integral" src="https://www.codelast.com/wp-content/uploads/2017/12/monte_carlo_importance_sampling_integration_1.png" style="width: 670px; height: 212px;" /></span></p>
<p>这幅图表明了什么意思呢？我们知道，计算[a,b]内的定积分就是求曲线 f(x)、直线 x=a,x=b以及x轴围成的形状的面积，因此，如果我们在曲线上随机地选取N个点，计算如图所示的粉红色长方形面积之和，再求个平均，其实就得到了定积分的近似值。点的数量取得越多，这个平均值就越逼近定积分的真实值。<br />
用公式写出来就是：<br />
 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_6729ad9b16ef416b8e9a493de61ad802.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\frac{1}{N}\left[ {(b - a) \times f({X_1}) + (b - a) \times f({X_2}) + \cdots + (b - a) \times f({X_N})} \right] = \frac{{b - a}}{N}\sum\limits_{i = 1}^N {f({X_i})} " /></span><script type='math/tex'>\frac{1}{N}\left[ {(b - a) \times f({X_1}) + (b - a) \times f({X_2}) + \cdots + (b - a) \times f({X_N})} \right] = \frac{{b - a}}{N}\sum\limits_{i = 1}^N {f({X_i})} </script> <br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e9%87%8d%e8%a6%81%e6%80%a7%e9%87%87%e6%a0%b7%ef%bc%8fimportance-sampling/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
查看本系列文章合集，请点击<a href="https://www.codelast.com/?p=9951" rel="noopener noreferrer" target="_blank"><span style="background-color: rgb(255, 160, 122);">这里</span></a>。</p>
<p>在前面的文章中，我们看到，随机采样是一个蒙特卡罗方法中很关键的步骤。而采样是需要技巧的，单纯地增加采样次数太没有效率了，比如说，如果随机采样一亿次，你可以把结果计算得特别精确，但是采样一亿次需要的时间非常长，长得远远超过了我们能接受的范围，这又有什么意义呢？<br />
人们发现，有一些方法可以让随机采的样本&ldquo;特别好&rdquo;。那么什么算&ldquo;特别好&rdquo;呢？比如说，本来使用没有任何原则的采样方法，需要采样1万个点，才能让计算出来的结果很接近真实值；现在使用一个&ldquo;特别好&rdquo;的采样方法，可以让我们只需要采样100个点，就可以让计算出来的结果很接近真实值了，这样就极大地减少了计算量。<br />
<span id="more-10022"></span><br />
而<span style="color:#0000ff;">重要性采样</span>（Importance Sampling），就是人们发现的、可以实现这个目的手段之一。</p>
<ul>
<li>
		<span style="background-color:#00ff00;">定义</span></li>
</ul>
<blockquote>
<p>
		重要性采样（Importance Sampling）是统计学中估计某一分布性质时使用的一种方法。该方法从与原分布不同的另一个分布中采样，而对原先分布的性质进行估计。</p>
</blockquote>
<p>乍一看，这句话可能有点抽象，别急，往后看你就理解了。</p>
<ul>
<li>
		<span style="background-color:#00ff00;">实例&mdash;&mdash;蒙特卡罗平均值法计算定积分</span></li>
</ul>
<p>在之前的文章中，我们已经见识过了用蒙特卡罗<span style="color:#0000ff;">投点法</span>计算定积分的过程，这里有另一个叫作&ldquo;平均值法&rdquo;的方法，由于它也是随机化的算法，因此，它也属于一种蒙特卡罗方法。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
我们来看看来自<a href="https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/monte-carlo-methods-in-practice/monte-carlo-integration" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">scratchapixel.com</span></a>的一幅图：<br />
<span style="text-align: center; height: 212px;"><img decoding="async" alt="monte carlo calculate definite integral" src="https://www.codelast.com/wp-content/uploads/2017/12/monte_carlo_importance_sampling_integration_1.png" style="width: 670px; height: 212px;" /></span></p>
<p>这幅图表明了什么意思呢？我们知道，计算[a,b]内的定积分就是求曲线 f(x)、直线 x=a,x=b以及x轴围成的形状的面积，因此，如果我们在曲线上随机地选取N个点，计算如图所示的粉红色长方形面积之和，再求个平均，其实就得到了定积分的近似值。点的数量取得越多，这个平均值就越逼近定积分的真实值。<br />
用公式写出来就是：<br />
 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_6729ad9b16ef416b8e9a493de61ad802.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\frac{1}{N}\left[ {(b - a) \times f({X_1}) + (b - a) \times f({X_2}) + \cdots + (b - a) \times f({X_N})} \right] = \frac{{b - a}}{N}\sum\limits_{i = 1}^N {f({X_i})} " /></span><script type='math/tex'>\frac{1}{N}\left[ {(b - a) \times f({X_1}) + (b - a) \times f({X_2}) + \cdots + (b - a) \times f({X_N})} \right] = \frac{{b - a}}{N}\sum\limits_{i = 1}^N {f({X_i})} </script> <br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
现在来看一下蒙特卡罗积分的表达式：<br />
 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_ca4826a1e89d7dcf370577224b5a94c8.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="{F^N} = \frac{1}{N}\sum\limits_{i = 1}^N {\frac{{f({X_i})}}{{p({X_i})}}} " /></span><script type='math/tex'>{F^N} = \frac{1}{N}\sum\limits_{i = 1}^N {\frac{{f({X_i})}}{{p({X_i})}}} </script> <br />
这个式子没有积分符号 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_d4dfe9e9553b338ea11840e57f6345cd.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\int {} " /></span><script type='math/tex'>\int {} </script> ，但是它却叫做&ldquo;积分&rdquo;公式，这是因为这个式子求的是积分的近似值&mdash;&mdash;当N越大的时候，计算出的值就越接近定积分的真实值。<br />
在公式中，有一个奇怪的东西，就是 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_eddcdfcc218117c455e65c9d0d947b17.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="{p({X_i})}" /></span><script type='math/tex'>{p({X_i})}</script> ，它表示 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_2af46b114c169c94ea9a86ca7b36758b.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="{{X_i}}" /></span><script type='math/tex'>{{X_i}}</script> 这个点，在某个分布下取 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_2af46b114c169c94ea9a86ca7b36758b.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="{{X_i}}" /></span><script type='math/tex'>{{X_i}}</script> 这个值的概率。那么这个分布是什么呢？比如说，它能不能是简单的均匀分布？后面我们会看到，这个分布是我们自己选取的。<br />
既然是要随机采样N个点，那么是不是随便用什么样的策略去采样，都可以达到同样的效果呢？这里用一幅图来说明，采样也是要讲究策略的，否则效果会很差：<br />
<img decoding="async" alt="monte carlo calculate definite integral" src="https://www.codelast.com/wp-content/uploads/2017/12/monte_carlo_importance_sampling_integration_2.png" style="text-align: center; width: 580px; height: 401px;" /><br />
由于定积分值就是曲线下的面积，显然，如果我们采样的点恰巧大部分处于圆圈内，那么这些点下的面积之和必然比较小，此时，我们按前面所说的计算矩形面积的方法算得的积分值，是远远不能反映积分的真实值的。也就是说，圆圈处的点，对积分值的贡献小，靠近&nbsp; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_118337530070f44bdf9c7cdeb8e35f9a.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="x = a" /></span><script type='math/tex'>x = a</script>  处的曲线上的点，对积分值的贡献大。<br />
所以，在实际采样的时候，靠近圆圈处的点应该少采一些，非圆圈处的点应该多采一些。<br />
这就是重要性采样（Importance Sampling）的概念由来了&mdash;&mdash;采样要按&ldquo;重要性&rdquo;来进行，不应该&ldquo;平等对待&rdquo;。<br />
如果采样恰到好处的话，可能只需要进行很少的采样（计算若干个点的函数值），就可以求出误差很小的积分值。</p>
<ul>
<li>
		<span style="background-color: rgb(0, 255, 0);">参考文献</span></li>
</ul>
<p>►&nbsp;<a href="https://zh.wikipedia.org/wiki/%E9%87%8D%E8%A6%81%E6%80%A7%E9%87%87%E6%A0%B7" rel="noopener noreferrer" target="_blank">维基百科：重要性采样</a><br />
►&nbsp;<a href="https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/monte-carlo-methods-in-practice/monte-carlo-integration" rel="noopener noreferrer" target="_blank">scratchapixel.com：Monte Carlo Methods in Practice</a></p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e9%87%8d%e8%a6%81%e6%80%a7%e9%87%87%e6%a0%b7%ef%bc%8fimportance-sampling/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 蒙特卡罗算法 对比 拉斯维加斯算法</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e8%92%99%e7%89%b9%e5%8d%a1%e7%bd%97%e7%ae%97%e6%b3%95-%e5%af%b9%e6%af%94-%e6%8b%89%e6%96%af%e7%bb%b4%e5%8a%a0%e6%96%af%e7%ae%97%e6%b3%95/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e8%92%99%e7%89%b9%e5%8d%a1%e7%bd%97%e7%ae%97%e6%b3%95-%e5%af%b9%e6%af%94-%e6%8b%89%e6%96%af%e7%bb%b4%e5%8a%a0%e6%96%af%e7%ae%97%e6%b3%95/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Thu, 11 Jul 2019 12:15:53 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[Monte Carlo method]]></category>
		<category><![CDATA[蒙特卡洛方法]]></category>
		<category><![CDATA[蒙特卡罗方法]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=10017</guid>

					<description><![CDATA[<p>
查看本系列文章合集，请点击<a href="https://www.codelast.com/?p=9951" rel="noopener noreferrer" target="_blank"><span style="background-color: rgb(255, 160, 122);">这里</span></a>。</p>
<ul>
<li>
		<span style="background-color:#00ff00;">区别</span></li>
</ul>
<p>讲到这里，稍微提一下，随机算法可以分为两类：蒙特卡洛算法 &#38; 拉斯维加斯算法。<br />
对蒙特卡洛算法来说，采样越多，越<span style="color:#0000ff;">近似最优解</span>。<br />
对拉斯维加斯算法来说，它永远给出正确解的随机化算法，总是给出正确结果，或是返回失败。<br />
<span id="more-10017"></span><br />
在固定的计算资源下，蒙特卡洛一定可以得到一个解，但拉斯维加斯不一定。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a></p>
<ul>
<li>
		<span style="background-color:#00ff00;">拉斯维加斯算法实例</span></li>
</ul>
<p>这里有一个很无趣的拉斯维加斯算法的例子：有10把钥匙，每次随机地取一把钥匙来开门，直到把门打开。如果只给你5次尝试的机会（在固定的计算资源下），那么可能能打开门（给出正确结果），也可能无法打开门（返回失败）。</p>
<p>当然，这个例子可能不能拿来套在蒙特卡罗算法身上，但这并不妨碍我们理解拉斯维加斯算法。</p>
<ul>
<li>
		<span style="background-color: rgb(0, 255, 0);">参考文献</span></li>
</ul>
<p>►&#160;<a href="https://zh.wikipedia.org/zh-hans/%E6%8B%89%E6%96%AF%E7%BB%B4%E5%8A%A0%E6%96%AF%E7%AE%97%E6%B3%95" rel="noopener noreferrer" target="_blank">维基百科：拉斯维加斯算法</a></p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&#160;版权声明&#160;<span style="color: rgb(255, 0, 0);">➤➤</span>&#160;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&#160;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" />&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e8%92%99%e7%89%b9%e5%8d%a1%e7%bd%97%e7%ae%97%e6%b3%95-%e5%af%b9%e6%af%94-%e6%8b%89%e6%96%af%e7%bb%b4%e5%8a%a0%e6%96%af%e7%ae%97%e6%b3%95/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
查看本系列文章合集，请点击<a href="https://www.codelast.com/?p=9951" rel="noopener noreferrer" target="_blank"><span style="background-color: rgb(255, 160, 122);">这里</span></a>。</p>
<ul>
<li>
		<span style="background-color:#00ff00;">区别</span></li>
</ul>
<p>讲到这里，稍微提一下，随机算法可以分为两类：蒙特卡洛算法 &amp; 拉斯维加斯算法。<br />
对蒙特卡洛算法来说，采样越多，越<span style="color:#0000ff;">近似最优解</span>。<br />
对拉斯维加斯算法来说，它永远给出正确解的随机化算法，总是给出正确结果，或是返回失败。<br />
<span id="more-10017"></span><br />
在固定的计算资源下，蒙特卡洛一定可以得到一个解，但拉斯维加斯不一定。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a></p>
<ul>
<li>
		<span style="background-color:#00ff00;">拉斯维加斯算法实例</span></li>
</ul>
<p>这里有一个很无趣的拉斯维加斯算法的例子：有10把钥匙，每次随机地取一把钥匙来开门，直到把门打开。如果只给你5次尝试的机会（在固定的计算资源下），那么可能能打开门（给出正确结果），也可能无法打开门（返回失败）。</p>
<p>当然，这个例子可能不能拿来套在蒙特卡罗算法身上，但这并不妨碍我们理解拉斯维加斯算法。</p>
<ul>
<li>
		<span style="background-color: rgb(0, 255, 0);">参考文献</span></li>
</ul>
<p>►&nbsp;<a href="https://zh.wikipedia.org/zh-hans/%E6%8B%89%E6%96%AF%E7%BB%B4%E5%8A%A0%E6%96%AF%E7%AE%97%E6%B3%95" rel="noopener noreferrer" target="_blank">维基百科：拉斯维加斯算法</a></p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e8%92%99%e7%89%b9%e5%8d%a1%e7%bd%97%e7%ae%97%e6%b3%95-%e5%af%b9%e6%af%94-%e6%8b%89%e6%96%af%e7%bb%b4%e5%8a%a0%e6%96%af%e7%ae%97%e6%b3%95/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 蒙特卡罗方法的实例2：计算定积分</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e8%92%99%e7%89%b9%e5%8d%a1%e7%bd%97%e6%96%b9%e6%b3%95%e7%9a%84%e5%ae%9e%e4%be%8b2%ef%bc%9a%e8%ae%a1%e7%ae%97%e5%ae%9a%e7%a7%af%e5%88%86/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e8%92%99%e7%89%b9%e5%8d%a1%e7%bd%97%e6%96%b9%e6%b3%95%e7%9a%84%e5%ae%9e%e4%be%8b2%ef%bc%9a%e8%ae%a1%e7%ae%97%e5%ae%9a%e7%a7%af%e5%88%86/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Thu, 11 Jul 2019 12:14:19 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[Monte Carlo method]]></category>
		<category><![CDATA[蒙特卡洛方法]]></category>
		<category><![CDATA[蒙特卡罗方法]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=10003</guid>

					<description><![CDATA[<p>
查看本系列文章合集，请点击<a href="https://www.codelast.com/?p=9951" rel="noopener noreferrer" target="_blank"><span style="background-color: rgb(255, 160, 122);">这里</span></a>。</p>
<p>为了对蒙特卡罗方法有一个直观的印象，本文再举一个实例（计算定积分），以说明蒙特卡罗方法的用途。</p>
<ul>
<li>
		<span style="background-color:#00ff00;">什么是定积分</span></li>
</ul>
<blockquote>
<p>
		对于一个给定的正实值函数 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_50bbd36e1fd2333108437a2ca378be62.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="f(x)" /></span><script type='math/tex'>f(x)</script> ，它在一个实数区间 [a,b]上的定积分 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_950c163edd420ea5410013869ef03ca6.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\int_a^b {f(x)dx} " /></span><script type='math/tex'>\int_a^b {f(x)dx} </script> 可以理解为在 OXY 坐标平面上，由曲线 (x,f(x))、直线 x=a,x=b以及x轴围成的曲边梯形的面积值。</p>
</blockquote>
<p><span id="more-10003"></span>如下图所示：<br />
<a href="https://www.codelast.com" rel="noopener noreferrer" style="text-align: center;" target="_blank"><img decoding="async" alt="monte carlo calculate definite integral" src="https://www.codelast.com/wp-content/uploads/2017/11/monte_carlo_example_calculate_definite_integral_1.png" style="width: 399px; height: 417px;" /></a><br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a></p>
<ul>
<li>
		<span style="background-color:#00ff00;">非蒙特卡罗方法</span></li>
</ul>
<p>其中一个计算定积分的方法就是用上面定积分的定义来求它：把面积均分成N个小矩形/梯形，求矩形/梯形面积之和。至于要分到多细才能良好地逼近定积分的真实值，我就不知道是否有什么理论上的说法了。</p>
<ul>
<li>
		<span style="background-color:#00ff00;">蒙特卡罗方法</span></li>
</ul>
<p>用蒙特卡罗方法来计算定积分，又分成好几种方法&#8212;&#8212;毕竟，蒙特卡罗方法是一类方法的统称，而不是指一种特定的方法。其中一种求定积分的蒙特卡罗方法，可以称之为<span style="color:#0000ff;">投点法</span>。和之前的文章中计算圆周率的做法类似，我们这里也用一个实际的函数  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_990d8846cdb8bfba65b16bf6252a7dd6.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="y = {x^2}" /></span><script type='math/tex'>y = {x^2}</script>  来说明问题：<br />
<a href="https://www.codelast.com" rel="noopener noreferrer" style="text-align: center;" target="_blank"><img decoding="async" alt="monte carlo calculate definite integral" src="https://www.codelast.com/wp-content/uploads/2017/11/monte_carlo_example_calculate_definite_integral_2.png" style="width: 475px; height: 370px;" /></a><br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
假设要计算函数在在[0, 1]区间的积分，也就是求红色部分的面积。<br />
然后，在1x1的正方形里生成大量随机点（m个），计算出在红色区域内的点的个数n&#8212;&#8212;判断条件： <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_e799055323eb79d077c822461d7628ff.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="y < {x^2}" /></span><script type='math/tex'>y < {x^2}</script> <br />
最后，n/m 即为红色部分的面积，也就是所求的积分值。<br />
投点法只是其中一种计算定积分的蒙特卡罗方法，当然也是一种很简单的蒙特卡罗方法，另外还有更麻烦的<span style="color:#0000ff;">平均值法</span>求定积分&#8212;&#8212;可以利用特殊的<span style="color:#0000ff;">采样</span>技巧来提高计算效率，在后面的文章中会讲到。<br />
在后面的文章中， 还会解释采样和蒙特卡罗方法有什么关系，先不要急。</p>
<ul>
<li>
		<span style="background-color: rgb(0, 255, 0);">参考文献</span></li>
</ul>
<p>►&#160;<a href="https://zh.wikipedia.org/wiki/%E7%A7%AF%E5%88%86" rel="noopener noreferrer" target="_blank">维基百科：积分</a><br />
►&#160;<a href="http://www.ruanyifeng.com/blog/2015/07/monte-carlo-method.html" rel="noopener noreferrer" target="_blank">蒙特卡罗方法入门</a></p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e8%92%99%e7%89%b9%e5%8d%a1%e7%bd%97%e6%96%b9%e6%b3%95%e7%9a%84%e5%ae%9e%e4%be%8b2%ef%bc%9a%e8%ae%a1%e7%ae%97%e5%ae%9a%e7%a7%af%e5%88%86/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
查看本系列文章合集，请点击<a href="https://www.codelast.com/?p=9951" rel="noopener noreferrer" target="_blank"><span style="background-color: rgb(255, 160, 122);">这里</span></a>。</p>
<p>为了对蒙特卡罗方法有一个直观的印象，本文再举一个实例（计算定积分），以说明蒙特卡罗方法的用途。</p>
<ul>
<li>
		<span style="background-color:#00ff00;">什么是定积分</span></li>
</ul>
<blockquote>
<p>
		对于一个给定的正实值函数 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_50bbd36e1fd2333108437a2ca378be62.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="f(x)" /></span><script type='math/tex'>f(x)</script> ，它在一个实数区间 [a,b]上的定积分 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_950c163edd420ea5410013869ef03ca6.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\int_a^b {f(x)dx} " /></span><script type='math/tex'>\int_a^b {f(x)dx} </script> 可以理解为在 OXY 坐标平面上，由曲线 (x,f(x))、直线 x=a,x=b以及x轴围成的曲边梯形的面积值。</p>
</blockquote>
<p><span id="more-10003"></span>如下图所示：<br />
<a href="https://www.codelast.com" rel="noopener noreferrer" style="text-align: center;" target="_blank"><img decoding="async" alt="monte carlo calculate definite integral" src="https://www.codelast.com/wp-content/uploads/2017/11/monte_carlo_example_calculate_definite_integral_1.png" style="width: 399px; height: 417px;" /></a><br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a></p>
<ul>
<li>
		<span style="background-color:#00ff00;">非蒙特卡罗方法</span></li>
</ul>
<p>其中一个计算定积分的方法就是用上面定积分的定义来求它：把面积均分成N个小矩形/梯形，求矩形/梯形面积之和。至于要分到多细才能良好地逼近定积分的真实值，我就不知道是否有什么理论上的说法了。</p>
<ul>
<li>
		<span style="background-color:#00ff00;">蒙特卡罗方法</span></li>
</ul>
<p>用蒙特卡罗方法来计算定积分，又分成好几种方法&mdash;&mdash;毕竟，蒙特卡罗方法是一类方法的统称，而不是指一种特定的方法。其中一种求定积分的蒙特卡罗方法，可以称之为<span style="color:#0000ff;">投点法</span>。和之前的文章中计算圆周率的做法类似，我们这里也用一个实际的函数  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_990d8846cdb8bfba65b16bf6252a7dd6.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="y = {x^2}" /></span><script type='math/tex'>y = {x^2}</script>  来说明问题：<br />
<a href="https://www.codelast.com" rel="noopener noreferrer" style="text-align: center;" target="_blank"><img decoding="async" alt="monte carlo calculate definite integral" src="https://www.codelast.com/wp-content/uploads/2017/11/monte_carlo_example_calculate_definite_integral_2.png" style="width: 475px; height: 370px;" /></a><br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
假设要计算函数在在[0, 1]区间的积分，也就是求红色部分的面积。<br />
然后，在1x1的正方形里生成大量随机点（m个），计算出在红色区域内的点的个数n&mdash;&mdash;判断条件： <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_e799055323eb79d077c822461d7628ff.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="y < {x^2}" /></span><script type='math/tex'>y < {x^2}</script> <br />
最后，n/m 即为红色部分的面积，也就是所求的积分值。<br />
投点法只是其中一种计算定积分的蒙特卡罗方法，当然也是一种很简单的蒙特卡罗方法，另外还有更麻烦的<span style="color:#0000ff;">平均值法</span>求定积分&mdash;&mdash;可以利用特殊的<span style="color:#0000ff;">采样</span>技巧来提高计算效率，在后面的文章中会讲到。<br />
在后面的文章中， 还会解释采样和蒙特卡罗方法有什么关系，先不要急。</p>
<ul>
<li>
		<span style="background-color: rgb(0, 255, 0);">参考文献</span></li>
</ul>
<p>►&nbsp;<a href="https://zh.wikipedia.org/wiki/%E7%A7%AF%E5%88%86" rel="noopener noreferrer" target="_blank">维基百科：积分</a><br />
►&nbsp;<a href="http://www.ruanyifeng.com/blog/2015/07/monte-carlo-method.html" rel="noopener noreferrer" target="_blank">蒙特卡罗方法入门</a></p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e8%92%99%e7%89%b9%e5%8d%a1%e7%bd%97%e6%96%b9%e6%b3%95%e7%9a%84%e5%ae%9e%e4%be%8b2%ef%bc%9a%e8%ae%a1%e7%ae%97%e5%ae%9a%e7%a7%af%e5%88%86/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 蒙特卡罗方法的定义、历史以及存在意义</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e8%92%99%e7%89%b9%e5%8d%a1%e7%bd%97%e6%96%b9%e6%b3%95%e7%9a%84%e5%ae%9a%e4%b9%89%e3%80%81%e5%8e%86%e5%8f%b2%e4%bb%a5%e5%8f%8a%e5%ad%98%e5%9c%a8%e6%84%8f%e4%b9%89/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e8%92%99%e7%89%b9%e5%8d%a1%e7%bd%97%e6%96%b9%e6%b3%95%e7%9a%84%e5%ae%9a%e4%b9%89%e3%80%81%e5%8e%86%e5%8f%b2%e4%bb%a5%e5%8f%8a%e5%ad%98%e5%9c%a8%e6%84%8f%e4%b9%89/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Thu, 11 Jul 2019 12:06:23 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[Monte Carlo method]]></category>
		<category><![CDATA[蒙特卡洛方法]]></category>
		<category><![CDATA[蒙特卡罗方法]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=9963</guid>

					<description><![CDATA[<p>
查看本系列文章合集，请点击<a href="https://www.codelast.com/?p=9951" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这里</span></a>。</p>
<ul>
<li>
		<span style="background-color:#00ff00;">定义</span></li>
</ul>
<p>来自维基百科：</p>
<blockquote>
<p>
		蒙特卡罗（洛）方法（Monte Carlo method），也称统计模拟方法，是1940年代中期由于科学技术的发展和电子计算机的发明，而提出的一种以概率统计理论为指导的数值计算方法。是指使用随机数来解决很多计算问题的方法。</p>
</blockquote>
<p>也就是说，蒙特卡罗方法并不是指一种特定的算法，而是一类算法的总称，这种算法主要利用了&#8220;随机&#8221;来实现。<span id="more-9963"></span></p>
<ul>
<li>
		<span style="background-color:#00ff00;">历史</span></li>
</ul>
<p>说到蒙特卡罗，这个名字是不是似曾相识？其实，它是摩纳哥大公国的一座城市，以蒙特卡洛大赌场闻名全球，始建于1858年。</p>
<div style="text-align: center;">
	<a href="https://www.codelast.com" rel="noopener noreferrer" target="_blank"><img decoding="async" alt="monte carlo casino" src="https://www.codelast.com/wp-content/uploads/2017/11/monte_carlo_casino_night_view.jpg" style="width: 646px; height: 358px;" /></a></div>
<div style="text-align: center;">
	(蒙特卡罗大赌场)</div>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
摩纳哥大公国是一个位于欧洲的城邦国家，它也是世界上面积第二小的国家。<br />
这个国家比较神奇的一点是，本地的摩纳哥人在他们自己的国家是&#8220;少数派&#8221;&#8212;&#8212;人口最多的是法国人，占32%，本地的摩纳哥人只占19%。<br />
摩纳哥还有很多与众不同的特点：不需要缴纳个人收入所得税，它是世界上最富裕最文明的国家（之一？），全世界很多富人都生活在这里。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
回到主题，蒙特卡罗方法和摩纳哥的蒙特卡罗这座城市有什么关系？<br />
说到这个，就不得不提蒙特卡罗方法的发明人&#8212;&#8212;冯&#183;诺伊曼、斯塔尼斯拉夫&#183;乌拉姆（数学家）、尼古拉斯&#183;梅特罗波利斯（物理学家），一般认为是这3人在194x年发明了蒙特卡罗方法。其中，乌拉姆的叔叔经常在蒙特卡洛赌场输钱，而蒙特卡洛方法正是以概率为基础的方法，因此得名。</p>
<ul>
<li>
		<span style="background-color:#00ff00;">意义</span></li>
</ul>
<div>
	蒙特卡罗方法的现实意义在于，很多问题计算量太大，难以遍历所有可能性来求解，通过采样，我们可以在短时间内求得近似解。<br />
	&#160;</div>
<ul>
<li>
		<span style="background-color:#00ff00;">参考文献</span></li>
</ul>
<p>► <a href="https://zh.wikipedia.org/wiki/%E8%92%99%E5%9C%B0%E5%8D%A1%E7%BE%85%E6%96%B9%E6%B3%95" rel="noopener noreferrer" target="_blank">维基百科：蒙特卡罗方法</a><br />
► <a href="https://zh.wikipedia.org/wiki/%E6%91%A9%E7%BA%B3%E5%93%A5" rel="noopener noreferrer" target="_blank">维基百科：摩纳哥</a></p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&#160;版权声明&#160;<span style="color: rgb(255, 0, 0);">➤➤</span>&#160;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&#160;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" />&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e8%92%99%e7%89%b9%e5%8d%a1%e7%bd%97%e6%96%b9%e6%b3%95%e7%9a%84%e5%ae%9a%e4%b9%89%e3%80%81%e5%8e%86%e5%8f%b2%e4%bb%a5%e5%8f%8a%e5%ad%98%e5%9c%a8%e6%84%8f%e4%b9%89/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
查看本系列文章合集，请点击<a href="https://www.codelast.com/?p=9951" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这里</span></a>。</p>
<ul>
<li>
		<span style="background-color:#00ff00;">定义</span></li>
</ul>
<p>来自维基百科：</p>
<blockquote>
<p>
		蒙特卡罗（洛）方法（Monte Carlo method），也称统计模拟方法，是1940年代中期由于科学技术的发展和电子计算机的发明，而提出的一种以概率统计理论为指导的数值计算方法。是指使用随机数来解决很多计算问题的方法。</p>
</blockquote>
<p>也就是说，蒙特卡罗方法并不是指一种特定的算法，而是一类算法的总称，这种算法主要利用了&ldquo;随机&rdquo;来实现。<span id="more-9963"></span></p>
<ul>
<li>
		<span style="background-color:#00ff00;">历史</span></li>
</ul>
<p>说到蒙特卡罗，这个名字是不是似曾相识？其实，它是摩纳哥大公国的一座城市，以蒙特卡洛大赌场闻名全球，始建于1858年。</p>
<div style="text-align: center;">
	<a href="https://www.codelast.com" rel="noopener noreferrer" target="_blank"><img decoding="async" alt="monte carlo casino" src="https://www.codelast.com/wp-content/uploads/2017/11/monte_carlo_casino_night_view.jpg" style="width: 646px; height: 358px;" /></a></div>
<div style="text-align: center;">
	(蒙特卡罗大赌场)</div>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
摩纳哥大公国是一个位于欧洲的城邦国家，它也是世界上面积第二小的国家。<br />
这个国家比较神奇的一点是，本地的摩纳哥人在他们自己的国家是&ldquo;少数派&rdquo;&mdash;&mdash;人口最多的是法国人，占32%，本地的摩纳哥人只占19%。<br />
摩纳哥还有很多与众不同的特点：不需要缴纳个人收入所得税，它是世界上最富裕最文明的国家（之一？），全世界很多富人都生活在这里。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
回到主题，蒙特卡罗方法和摩纳哥的蒙特卡罗这座城市有什么关系？<br />
说到这个，就不得不提蒙特卡罗方法的发明人&mdash;&mdash;冯&middot;诺伊曼、斯塔尼斯拉夫&middot;乌拉姆（数学家）、尼古拉斯&middot;梅特罗波利斯（物理学家），一般认为是这3人在194x年发明了蒙特卡罗方法。其中，乌拉姆的叔叔经常在蒙特卡洛赌场输钱，而蒙特卡洛方法正是以概率为基础的方法，因此得名。</p>
<ul>
<li>
		<span style="background-color:#00ff00;">意义</span></li>
</ul>
<div>
	蒙特卡罗方法的现实意义在于，很多问题计算量太大，难以遍历所有可能性来求解，通过采样，我们可以在短时间内求得近似解。<br />
	&nbsp;</div>
<ul>
<li>
		<span style="background-color:#00ff00;">参考文献</span></li>
</ul>
<p>► <a href="https://zh.wikipedia.org/wiki/%E8%92%99%E5%9C%B0%E5%8D%A1%E7%BE%85%E6%96%B9%E6%B3%95" rel="noopener noreferrer" target="_blank">维基百科：蒙特卡罗方法</a><br />
► <a href="https://zh.wikipedia.org/wiki/%E6%91%A9%E7%BA%B3%E5%93%A5" rel="noopener noreferrer" target="_blank">维基百科：摩纳哥</a></p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e8%92%99%e7%89%b9%e5%8d%a1%e7%bd%97%e6%96%b9%e6%b3%95%e7%9a%84%e5%ae%9a%e4%b9%89%e3%80%81%e5%8e%86%e5%8f%b2%e4%bb%a5%e5%8f%8a%e5%ad%98%e5%9c%a8%e6%84%8f%e4%b9%89/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 用人话解释蒙特卡罗方法/Monte Carlo method(文章合集)</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e7%94%a8%e4%ba%ba%e8%af%9d%e8%a7%a3%e9%87%8a%e8%92%99%e7%89%b9%e5%8d%a1%e7%bd%97%e6%96%b9%e6%b3%95-monte-carlo-method%e6%96%87%e7%ab%a0%e5%90%88%e9%9b%86/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e7%94%a8%e4%ba%ba%e8%af%9d%e8%a7%a3%e9%87%8a%e8%92%99%e7%89%b9%e5%8d%a1%e7%bd%97%e6%96%b9%e6%b3%95-monte-carlo-method%e6%96%87%e7%ab%a0%e5%90%88%e9%9b%86/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Thu, 11 Jul 2019 12:03:56 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[Monte Carlo method]]></category>
		<category><![CDATA[蒙特卡洛方法]]></category>
		<category><![CDATA[蒙特卡罗方法]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=9951</guid>

					<description><![CDATA[<p>
蒙特卡罗(洛)方法（Monte Carlo method），也称统计模拟方法，是1940年代中期由于科学技术的发展和电子计算机的发明，而提出的一种以概率统计理论为指导的数值计算方法。是指使用随机数来解决很多计算问题的方法。<br />
<span id="more-9951"></span><br />
下面的系列文章是《Deep Learning Book》一书第17章的部分肤浅读书笔记。由于本人理论水平有限，我认为满是公式、基本没有实例的《Deep Learning Book》第17章&#8220;很难看&#8221;、不利于学习。因此，我根据第17章的章节结构，自己去查阅了一些其他资料，找到了很多实例来写成了这些文章，我认为对初学者来说，它们可以让人更容易看懂。<br />
不过，还是需要事先声明，这些文章中的某些陈述，是我个人的理解，本人水平有限，难免会有各种错误，请自行辨别。</p>
<p><span style="background-color: rgb(0, 255, 0);">➤&#160;</span><a href="https://www.codelast.com/?p=9963" target="_blank" rel="noopener noreferrer">蒙特卡罗方法的定义、历史以及存在意义</a></p>
<p><span style="background-color: rgb(0, 255, 0);">➤&#160;</span><a href="https://www.codelast.com/?p=9983" target="_blank" rel="noopener noreferrer">蒙特卡罗方法的实例1：计算圆周率</a></p>
<p><span style="background-color: rgb(0, 255, 0);">➤&#160;</span><a href="https://www.codelast.com/?p=10003" target="_blank" rel="noopener noreferrer">蒙特卡罗方法的实例2：计算定积分</a></p>
<p><span style="background-color: rgb(0, 255, 0);">➤&#160;</span><a href="https://www.codelast.com/?p=10017" target="_blank" rel="noopener noreferrer">蒙特卡罗算法 对比 拉斯维加斯算法</a></p>
<p><span style="background-color: rgb(0, 255, 0);">➤&#160;</span><a href="https://www.codelast.com/?p=10022" target="_blank" rel="noopener noreferrer">重要性采样／Importance Sampling</a></p>
<p><span style="background-color: rgb(0, 255, 0);">➤&#160;</span>马尔科夫链蒙特卡罗／Markov chain Monte Carlo, MCMC</p>
<p><span style="background-color: rgb(0, 255, 0);">➤&#160;</span>马尔科夫链／Markov chain</p>
<p><span style="background-color: rgb(0, 255, 0);">➤&#160;</span>马尔科夫链的平稳分布</p>
<div>
	<span style="background-color: rgb(0, 255, 0);">➤&#160;</span>马尔科夫链的细致平稳条件／Detailed Balance
<p>	<span style="background-color: rgb(0, 255, 0);">➤&#160;</span>马尔科夫链的接受率／Acceptance Probability</p>
<p>	<span style="background-color: rgb(0, 255, 0);">➤&#160;</span>标准MCMC算法实现及其存在的问题</p>
<p>	<span style="background-color: rgb(0, 255, 0);">➤&#160;</span>Metropolis Hastings MCMC算法</p>
<p>	<span style="background-color: rgb(0, 255, 0);">➤&#160;</span>吉布斯采样／Gibbs Sampling</p></div>
<div>
	<br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a></div>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e7%94%a8%e4%ba%ba%e8%af%9d%e8%a7%a3%e9%87%8a%e8%92%99%e7%89%b9%e5%8d%a1%e7%bd%97%e6%96%b9%e6%b3%95-monte-carlo-method%e6%96%87%e7%ab%a0%e5%90%88%e9%9b%86/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>
蒙特卡罗(洛)方法（Monte Carlo method），也称统计模拟方法，是1940年代中期由于科学技术的发展和电子计算机的发明，而提出的一种以概率统计理论为指导的数值计算方法。是指使用随机数来解决很多计算问题的方法。<br />
<span id="more-9951"></span><br />
下面的系列文章是《Deep Learning Book》一书第17章的部分肤浅读书笔记。由于本人理论水平有限，我认为满是公式、基本没有实例的《Deep Learning Book》第17章&ldquo;很难看&rdquo;、不利于学习。因此，我根据第17章的章节结构，自己去查阅了一些其他资料，找到了很多实例来写成了这些文章，我认为对初学者来说，它们可以让人更容易看懂。<br />
不过，还是需要事先声明，这些文章中的某些陈述，是我个人的理解，本人水平有限，难免会有各种错误，请自行辨别。</p>
<p><span style="background-color: rgb(0, 255, 0);">➤&nbsp;</span><a href="https://www.codelast.com/?p=9963" target="_blank" rel="noopener noreferrer">蒙特卡罗方法的定义、历史以及存在意义</a></p>
<p><span style="background-color: rgb(0, 255, 0);">➤&nbsp;</span><a href="https://www.codelast.com/?p=9983" target="_blank" rel="noopener noreferrer">蒙特卡罗方法的实例1：计算圆周率</a></p>
<p><span style="background-color: rgb(0, 255, 0);">➤&nbsp;</span><a href="https://www.codelast.com/?p=10003" target="_blank" rel="noopener noreferrer">蒙特卡罗方法的实例2：计算定积分</a></p>
<p><span style="background-color: rgb(0, 255, 0);">➤&nbsp;</span><a href="https://www.codelast.com/?p=10017" target="_blank" rel="noopener noreferrer">蒙特卡罗算法 对比 拉斯维加斯算法</a></p>
<p><span style="background-color: rgb(0, 255, 0);">➤&nbsp;</span><a href="https://www.codelast.com/?p=10022" target="_blank" rel="noopener noreferrer">重要性采样／Importance Sampling</a></p>
<p><span style="background-color: rgb(0, 255, 0);">➤&nbsp;</span>马尔科夫链蒙特卡罗／Markov chain Monte Carlo, MCMC</p>
<p><span style="background-color: rgb(0, 255, 0);">➤&nbsp;</span>马尔科夫链／Markov chain</p>
<p><span style="background-color: rgb(0, 255, 0);">➤&nbsp;</span>马尔科夫链的平稳分布</p>
<div>
	<span style="background-color: rgb(0, 255, 0);">➤&nbsp;</span>马尔科夫链的细致平稳条件／Detailed Balance</p>
<p>	<span style="background-color: rgb(0, 255, 0);">➤&nbsp;</span>马尔科夫链的接受率／Acceptance Probability</p>
<p>	<span style="background-color: rgb(0, 255, 0);">➤&nbsp;</span>标准MCMC算法实现及其存在的问题</p>
<p>	<span style="background-color: rgb(0, 255, 0);">➤&nbsp;</span>Metropolis Hastings MCMC算法</p>
<p>	<span style="background-color: rgb(0, 255, 0);">➤&nbsp;</span>吉布斯采样／Gibbs Sampling</div>
<div>
	<br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
	转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
	感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
		<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e7%94%a8%e4%ba%ba%e8%af%9d%e8%a7%a3%e9%87%8a%e8%92%99%e7%89%b9%e5%8d%a1%e7%bd%97%e6%96%b9%e6%b3%95-monte-carlo-method%e6%96%87%e7%ab%a0%e5%90%88%e9%9b%86/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 蒙特卡罗方法的实例1：计算圆周率</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e8%92%99%e7%89%b9%e5%8d%a1%e7%bd%97%e6%96%b9%e6%b3%95%e7%9a%84%e5%ae%9e%e4%be%8b1%ef%bc%9a%e8%ae%a1%e7%ae%97%e5%9c%86%e5%91%a8%e7%8e%87/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e8%92%99%e7%89%b9%e5%8d%a1%e7%bd%97%e6%96%b9%e6%b3%95%e7%9a%84%e5%ae%9e%e4%be%8b1%ef%bc%9a%e8%ae%a1%e7%ae%97%e5%9c%86%e5%91%a8%e7%8e%87/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Wed, 22 Nov 2017 16:02:18 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[Monte Carlo method]]></category>
		<category><![CDATA[蒙特卡洛方法]]></category>
		<category><![CDATA[蒙特卡罗方法]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=9983</guid>

					<description><![CDATA[<p>
查看本系列文章合集，请点击<a href="https://www.codelast.com/?p=9951" rel="noopener noreferrer" target="_blank"><span style="background-color: rgb(255, 160, 122);">这里</span></a>。</p>
<p>为了对蒙特卡罗方法有一个直观的印象，我们先举一个实例（计算圆周率 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_522359592d78569a9eac16498aa7a087.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\pi " /></span><script type='math/tex'>\pi </script> ），让从来没有接触过蒙特卡罗方法的人产生&#8220;原来这就是Monte Carlo&#8221;的感觉，以减少刚开始学习的困惑。</p>
<ul>
<li>
		<span style="background-color:#00ff00;">非蒙特卡罗方法</span></li>
</ul>
<p>圆周率 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_522359592d78569a9eac16498aa7a087.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\pi " /></span><script type='math/tex'>\pi </script> 可以怎么计算？其中一个&#8220;常规&#8221;的方法就是利用 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_522359592d78569a9eac16498aa7a087.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\pi " /></span><script type='math/tex'>\pi </script> 的莱布尼茨公式：<br />
 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_aef37fd61f243ce0d16aadd617fe03c7.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\frac{\pi }{4} = \sum\limits_{n = 0}^\infty {\frac{{{{\left( { - 1} \right)}^n}}}{{2n + 1}}} = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} - \cdots " /></span><script type='math/tex'>\frac{\pi }{4} = \sum\limits_{n = 0}^\infty {\frac{{{{\left( { - 1} \right)}^n}}}{{2n + 1}}} = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} - \cdots </script> <br />
不断增大 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_7b8b965ad4bca0e41ab51de7b31363a1.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="n" /></span><script type='math/tex'>n</script> 的值，就能越来越逼近 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_9c05e99783ffbdc89e86bee35eb07484.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\frac{\pi }{4}" /></span><script type='math/tex'>\frac{\pi }{4}</script> <span id="more-9983"></span><br />
当 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_7b8b965ad4bca0e41ab51de7b31363a1.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="n" /></span><script type='math/tex'>n</script> 的最大值取30000时，可以计算得  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_5ff097626c52b389d39054bca2cab608.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\pi = 3.141559320256462" /></span><script type='math/tex'>\pi = 3.141559320256462</script> </p>
<ul>
<li>
		<span style="background-color:#00ff00;">蒙特卡罗方法</span></li>
</ul>
<p>相比之下，用蒙特卡罗方法来计算 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_522359592d78569a9eac16498aa7a087.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\pi " /></span><script type='math/tex'>\pi </script> ，可能就是一个比较&#8220;另类&#8221;的途径了。<br />
假设圆外部有一个相切的正方形，如下图所示：<br />
<a href="https://www.codelast.com" rel="noopener noreferrer" target="_blank"><img decoding="async" alt="monte carlo method calculate pi example" src="https://www.codelast.com/wp-content/uploads/2017/11/monte_carlo_example_calculate_pi_1.png" style="width: 255px; height: 210px;" /></a><br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e8%92%99%e7%89%b9%e5%8d%a1%e7%bd%97%e6%96%b9%e6%b3%95%e7%9a%84%e5%ae%9e%e4%be%8b1%ef%bc%9a%e8%ae%a1%e7%ae%97%e5%9c%86%e5%91%a8%e7%8e%87/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
查看本系列文章合集，请点击<a href="https://www.codelast.com/?p=9951" rel="noopener noreferrer" target="_blank"><span style="background-color: rgb(255, 160, 122);">这里</span></a>。</p>
<p>为了对蒙特卡罗方法有一个直观的印象，我们先举一个实例（计算圆周率 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_522359592d78569a9eac16498aa7a087.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\pi " /></span><script type='math/tex'>\pi </script> ），让从来没有接触过蒙特卡罗方法的人产生&ldquo;原来这就是Monte Carlo&rdquo;的感觉，以减少刚开始学习的困惑。</p>
<ul>
<li>
		<span style="background-color:#00ff00;">非蒙特卡罗方法</span></li>
</ul>
<p>圆周率 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_522359592d78569a9eac16498aa7a087.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\pi " /></span><script type='math/tex'>\pi </script> 可以怎么计算？其中一个&ldquo;常规&rdquo;的方法就是利用 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_522359592d78569a9eac16498aa7a087.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\pi " /></span><script type='math/tex'>\pi </script> 的莱布尼茨公式：<br />
 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_aef37fd61f243ce0d16aadd617fe03c7.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\frac{\pi }{4} = \sum\limits_{n = 0}^\infty {\frac{{{{\left( { - 1} \right)}^n}}}{{2n + 1}}} = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} - \cdots " /></span><script type='math/tex'>\frac{\pi }{4} = \sum\limits_{n = 0}^\infty {\frac{{{{\left( { - 1} \right)}^n}}}{{2n + 1}}} = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} - \cdots </script> <br />
不断增大 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_7b8b965ad4bca0e41ab51de7b31363a1.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="n" /></span><script type='math/tex'>n</script> 的值，就能越来越逼近 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_9c05e99783ffbdc89e86bee35eb07484.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\frac{\pi }{4}" /></span><script type='math/tex'>\frac{\pi }{4}</script> <span id="more-9983"></span><br />
当 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_7b8b965ad4bca0e41ab51de7b31363a1.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="n" /></span><script type='math/tex'>n</script> 的最大值取30000时，可以计算得  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_5ff097626c52b389d39054bca2cab608.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\pi = 3.141559320256462" /></span><script type='math/tex'>\pi = 3.141559320256462</script> </p>
<ul>
<li>
		<span style="background-color:#00ff00;">蒙特卡罗方法</span></li>
</ul>
<p>相比之下，用蒙特卡罗方法来计算 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_522359592d78569a9eac16498aa7a087.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\pi " /></span><script type='math/tex'>\pi </script> ，可能就是一个比较&ldquo;另类&rdquo;的途径了。<br />
假设圆外部有一个相切的正方形，如下图所示：<br />
<a href="https://www.codelast.com" rel="noopener noreferrer" target="_blank"><img decoding="async" alt="monte carlo method calculate pi example" src="https://www.codelast.com/wp-content/uploads/2017/11/monte_carlo_example_calculate_pi_1.png" style="width: 255px; height: 210px;" /></a><br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
设圆面积为C，正方形面积为S，则利用面积公式可以轻易算得： <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_623c1f945c60bccf841dc43aa5c4e088.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\frac{C}{S} = \frac{{\pi {r^2}}}{{{{(2r)}^2}}} = \frac{\pi }{4}" /></span><script type='math/tex'>\frac{C}{S} = \frac{{\pi {r^2}}}{{{{(2r)}^2}}} = \frac{\pi }{4}</script> <br />
然后，我们在正方形内随机生成30000个点（当然可以生成更多，这里只是用30000举个例子），分别计算这些点与圆心的距离，距离&lt;r 表示点在圆内部：<br />
<a href="https://www.codelast.com" rel="noopener noreferrer" target="_blank"><img decoding="async" alt="monte carlo method calculate pi example" src="https://www.codelast.com/wp-content/uploads/2017/11/monte_carlo_example_calculate_pi_2.png" style="width: 216px; height: 213px;" /></a><br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
从面积之比可知：如果点是均匀分布的，则圆内的点的数量应该占所有点数量的 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_9c05e99783ffbdc89e86bee35eb07484.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\frac{\pi }{4}" /></span><script type='math/tex'>\frac{\pi }{4}</script> ，计算数量之比，再乘以4，即可得圆周率。<br />
在某一次实验中，模拟30000个点， <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_522359592d78569a9eac16498aa7a087.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\pi " /></span><script type='math/tex'>\pi </script> 的估算值与真实值相差0.07%<br />
所以，这里正是巧妙地利用了&ldquo;随机&rdquo;这个技术，来计算了圆周率，这种方法就属于蒙特卡罗方法。</p>
<ul>
<li>
		<span style="background-color:#00ff00;">参考文献</span></li>
</ul>
<p>► <a href="https://zh.wikipedia.org/wiki/%CE%A0%E7%9A%84%E8%8E%B1%E5%B8%83%E5%B0%BC%E8%8C%A8%E5%85%AC%E5%BC%8F" rel="noopener noreferrer" target="_blank">维基百科： <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_522359592d78569a9eac16498aa7a087.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\pi " /></span><script type='math/tex'>\pi </script> 的莱布尼茨公式</a><br />
►&nbsp;<a href="http://www.ruanyifeng.com/blog/2015/07/monte-carlo-method.html" rel="noopener noreferrer" target="_blank">蒙特卡罗方法入门</a></p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e8%92%99%e7%89%b9%e5%8d%a1%e7%bd%97%e6%96%b9%e6%b3%95%e7%9a%84%e5%ae%9e%e4%be%8b1%ef%bc%9a%e8%ae%a1%e7%ae%97%e5%9c%86%e5%91%a8%e7%8e%87/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
