程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

R law of large numbers (Python Chebyshev inequality verifies the law of large numbers) simulates pi

編輯:Python

Law of large Numbers

In probability theory , Law of large Numbers (LLN) A theorem describing the results of a large number of identical experiments . According to the law , The average value of the results obtained from a large number of tests shall be close to the expected value , And with the increase of test times, it tends to be close to the expected value .

LLN Very important , Because it guarantees the long-term stable result of the average value of some random events . for example , Although casinos may lose money in a single spin of roulette , But its returns will tend to be predictable percentages in a large number of rotations . Any winning streak of players will eventually be overcome by the parameters of the game . It should be noted that , The law only applies when a large number of observations are considered ( As shown in the name ). There is no principle that a few observations will be consistent with the expected values , Or the continuity of one value is immediately changed by other values “ Balance ”.

In addition, we should pay attention to ,LLN For average values only . therefore , although

lim ⁡ n → ∞ ∑ i = 1 n X i n = X ˉ \lim _{n \rightarrow \infty} \sum_{i=1}^{n} \frac{X_{i}}{n}=\bar{X} n→∞lim​i=1∑n​nXi​​=Xˉ

Other unproven formulas that look similar , For example, “ Theoretical results ” Original deviation of :

∑ i = 1 n X i − n × X ˉ \sum_{i=1}^{n} X_{i}-n \times \bar{X} i=1∑n​Xi​−n×Xˉ

Not only will it not follow n n n And converges to zero , But with n n n An increase in , Its absolute value also tends to increase .

simulation

For the sake of completeness , The following simulation will be estimated according to the following logic π \pi π Value :

Know that the area of a circle is π r 2 \pi r^{2} πr2, We know the radius ( r r r ) by 1 The area of the circle is just π \pi π . If we put the circle perfectly on the side with 2 The square of ( That is, the area is 4) Internal , We know that the ratio of the area of a circle to the area of a square is π 4 \frac{\pi}{4} 4π​.

therefore , If we randomly throw darts at a board of the same size several times , Then the ratio of darts in a circle to those that hit a square should be close to π 4 \frac{\pi}{4} 4π​. If we multiply this ratio by 4, We got it π \pi π The estimate of .

# Create a dartboard 
dart_board <- ggplot() +
geom_circle(aes(x0 = 0, y0 = 0, r = 1), fill = "black") +
geom_circle(aes(x0 = 0, y0 = 0, r = 0.99), fill = "red") +
geom_circle(aes(x0 = 0, y0 = 0, r = 0.7), fill = "lightyellow") +
geom_circle(aes(x0 = 0, y0 = 0, r = 0.2), fill = "red") +
geom_circle(aes(x0 = 0, y0 = 0, r = 0.1), fill = "darkgreen") +
coord_fixed() +
geom_segment(aes(x=-1,xend=1,y=1,yend=1)) +
geom_segment(aes(x=-1,xend=1,y=-1,yend=-1)) +
geom_segment(aes(x=1,xend=1,y=-1,yend=1)) +
geom_segment(aes(x=-1,xend=-1,y=-1,yend=1)) +
xlab("x")+ylab("y")+
theme_bw()
dart_board

Draw the convergence trend graph

Source code

Python Chebyshev inequality verifies the law of large numbers

Refer to the - Yatu inter


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved