大數定律(Law of Large Numbers)的原理及 Python 實現


本文以拋擲硬幣(tossing coins)為例, 來理解大數定律(Law of Large Numbers), 並使用 Python 語言實現.

原理

大數定律, 簡單來說, 就是隨着拋擲硬幣的次數的增多, 正面向上出現的比例(the ratio of heads)會越來越接近正面朝上的概率(the probability of heads). 

Python 實現

在示例代碼中, 假定正面朝上的概率(the probability of heads)為0.51, 模擬進行10個系列的硬幣投擲(coin tosses), 每個投擲系列, 投擲硬幣 10000 次, 然后, 將正面朝上的比例(the ratio of heads)隨着投擲次數的變化進行顯示, 並保存到 images/ 目錄下. 具體代碼如下:

#-*- coding: utf8 -*-
from __future__ import print_function import numpy as np import matplotlib.pyplot as plt import os def law_of_large_numbers(num_series=10, num_tosses=10000, heads_prob=0.51, display=True): """ Get `num_series` series of biased coin tosses, each of which has `num_tosses` tosses, 
    and the probability of heads in each toss is `heads_prob`.
""" # 1 when less than heads_prob; 0 when no less than heads_prob coin_tosses = (np.random.rand(num_tosses, num_series) < heads_prob).astype('float32') cumulative_heads_ratio = np.cumsum(coin_tosses, axis=0)/np.arange(1, num_tosses+1).reshape(-1,1) if display: plot_fig(cumulative_heads_ratio, heads_prob) def save_fig(fig_id, dirname="images/", tight_layout=True): print("Saving figure", fig_id) if tight_layout: plt.tight_layout() # First, ensure the directory exists if not os.path.isdir(dirname): os.makedirs(dirname) # Then, save the fig_id imagename image_path = "%s.png" % os.path.join(dirname, fig_id) plt.savefig(image_path, format='png', dpi=300) def plot_fig(cumulative_heads_ratio, heads_prob, save=True): # Get the number of tosses in a series num_tosses = cumulative_heads_ratio.shape[0] # Set the width and height in inches plt.figure(figsize=(8, 3.5)) # Plot cumulative heads ratio plt.plot(cumulative_heads_ratio) # Plot the horizontal line of value `heads_prob`, with black dashed linetype plt.plot([0, num_tosses], [heads_prob, heads_prob], "k--", linewidth=2, label="{}%".format(round(heads_prob*100, 1))) # Plot the horizontal line of value 0.5 with black solid linetype plt.plot([0, num_tosses], [0.5, 0.5], "k-", label="50.0%") plt.xlabel("Number of coin tosses") plt.ylabel("Heads ratio") plt.legend(loc="lower right") # Set x ranges and y ranges xmin, xmax, ymin, ymax = 0, num_tosses, 0.42, 0.58 plt.axis([xmin, xmax, ymin, ymax]) if save: save_fig("law_of_large_numbers_plot") plt.show() if __name__ == '__main__': num_series, num_tosses = 10, 10000 heads_proba = 0.51 law_of_large_numbers(num_series, num_tosses, heads_proba)

 顯示結果, 如下圖所示

參考資料

[1] Aurélien Géron. Hands-On Machine Learning with Scikit-Learn and TensorFlow. O'Reilly Media, 2017.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM