Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Central limit Law by Python

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to achieve the central limit law of Python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve the central limit law of Python".

In the whole probability theory, the core problem is the distribution of random variables. As we can see in discrete and continuous distributions, there are many kinds of distributions. What is even more exaggerated is that under the premise of satisfying the probability axiom, we can design the distribution by ourselves. Imagine what a wonderful thing it would be if a Vamei distribution was printed on a math book one day. However, this wish is not so easy to achieve. Those "celebrity" distributions, such as "Poisson", "Gaussian" and "Bernoulli" distributions, are often very important in theory, so they have been deeply studied by mathematicians. the characteristics of "well-known" distributions (such as their expectations, variances and cumulative probability functions) can be easily found in mathematical manuals, and these research results have become an important part of the "arsenal" of probability theory.

On the other hand, is there anything in common with probability distribution? Many of our conclusions depend on the specific type of distribution. The conclusion that one distribution is valid may not be true for another distribution. A conclusion that holds for any distribution can greatly simplify our research. This is extremely important in the research of natural science and social science. There are many random variables in the study of these disciplines. For example, in order to study gold deposits, it is often necessary to know the probability distribution of gold content X in stones. However, the distribution types of these random variables cannot be known in advance (or even can never be accurately known). In this way, the whole research is stopped at the first step. If we can come to a conclusion that holds for any distribution, then we can continue along this conclusion.

Nature is sometimes more generous than we think. It gives a rather central set of laws in probability theory: the central limit law (central limit theorem). This set of laws not only holds true for any distribution, but also reminds us that we should pay special attention to normal distribution. Let's take a look at what the central limit law says.

Central limit law

Let's start with a version of the central limit law:

Random variables X1, X2, and the same distribution (IID, independent and identically distributed). The expectation of the distribution is μ μ, the variance is σ 2 σ 2, μ, σ μ, σ are all finite values, and σ ≠ 0 σ ≠ 0. The mean value of these random variables is X "= 1n ∑ ni=1XiX" = 1n ∑ i=1nXi. Let Zeta nasty − μ σ / n √ ζ nasty X − μ σ / n, then

Limn →∞ P (ζ n ≤ z) = Φ (z) limn →∞ P (ζ n ≤ z) = Φ (z)

Where Φ (z) Φ (z) is the distribution function of standard normal distribution.

To put it simply, we look for the mean X "X" of n IID random variables. When n approaches infinity, the mean (a new random variable) approaches a normal distribution.

(through the transformation of ζ n ζ n, the standard normal distribution ζ n ζ n can be derived from the X "X" of normal distribution.)

Demonstrate the central limit law

Let's take n IID random variables, make them all conform to the exponential distribution of λ = 1 λ = 1, and observe the distribution of their mean. To observe their distribution, we use a random number generator to sample 10000 times. That is, 100000 experiments were carried out, and each experiment obtained the value of a group of random variables and got a mean value. A total of 10000 averages were obtained. Draw a histogram of the mean distribution.

In three cases, let n equal to 1pm 20,100 respectively:

In the first case, X "= X1/1=X1X" = X1/1=X1, that is, X "X" itself is an exponential distribution.

In the second and third cases, the distribution of the mean deviates more and more from an exponential distribution, and the shape of the distribution approaches to a normal distribution.

The code is as follows:

# By Vamei # Central Limit Theory # X is exponential distribution with lambda = 1 import matplotlib.pyplot as pltimport numpy as npfrom scipy.stats import expon# Get one sample of (X1 + X2 +... + XN) / N def sample_mean (N): # exponential distribution, with lambda = 1 one_sample = expon.rvs (scale = 1, size = N) return one_sample.mean () # Increase N: 1, 20, 1000. # Demo of Central Limit Theory in histogramplt.figure (figsize= (12,4) for N, subp in zip ([1,20,1000], [131,132,133]): # generate samples all_means = np.array ([sample_mean (N) for i in range (10000)]) # plot figure plt.subplot (subp) plt.hist (all_means,bins=100 Color= "blue") plt.title ('Central Limit Theory nasty% i'% N) plt.xlabel (' sample means') plt.ylabel ('Frequency') plt.tight_layout () plt.savefig ('. / central_limit.png', dpi=None, facecolor='w')

Exercise: this code examines the mean of the exponential distribution. It can be rewritten to test whether other distributions conform to the central limit law, such as the mean value of a uniform distribution.

Prove

I will use the moment generating function to prove the above law. Suppose the moment generating function of Xi − μ Xi − μ is M (t) M (t). Therefore, M'(t) = μ, M (2) (t) = σ 2M'(t) = μ, M (2) (t) = σ 2.

When n approaches infinity, t / (σ n −−√) t / (σ n) approaches 0. M (t) can be expanded as follows:

M (t) = 1 + 12 σ 2t2 + o (T2) M (t) = 1 + 12 σ 2T2 + o (T2)

O (T2) o (T2) denotes the power of t of higher order than t2t2.

According to the properties of the moment generating function, the moment generating function of ζ n ζ n is written as

M ζ n = [M (t n σ −−√)] n = (1+t22n+o (t 2Pao)) n M é n = [M (t σ n)] n = (1+t22n+o (t 2Po)) n

O (T2) o (T2) indicates that when n tends to infinity, the term disappears earlier than t2/nt2/n.

According to calculus, it is proved that when n approaches infinity, the above expression approaches:

Maun (t) → et2/ 2Maun (t) → et2/2

This is the moment generating function of the standard normal distribution. Therefore, the distribution of ZnZn tends to the standard normal distribution.

The central limit law described above has a prerequisite that the N random variables that produce the mean are IID (independent, uniformly distributed) random variables. In other versions of the central limit law, random variables can not be completely independent. In fact, the central limit law is an area that is still under active research.

Lace

The prototype of the central limit law can be traced back to the study of de Moivre in the 18th century. Through experiments, he found that if a large number of heads flip coins, the average value of the result (1: heads, 0: tails) is a normal distribution. Here, the distribution studied by de Moivre is the mean of random variables of multiple Bernoulli distributions.

Coin toss: the distribution of the mean

Imagine that there was no computer, let alone a random number generator. To test the results, de Moivre really flipped a coin thousands of times. Mathematicians are amazing animals.

In order to understand the results of the central limit law more intuitively. Let's imagine that if a rice jar is mixed with two kinds of black and white rice, each share is half. Grab one of them randomly, and there are n grains of rice in this one. If n is relatively small, then there are likely to be some extreme values, such as n = 3, with three pure white rice grains. However, if the "handful" is very large, such as 1000 rice grains, then the probability that 1000 meters are all white is very small, while the probability of half of white rice and black rice is very high, that is, a distribution similar to normal distribution.

We can apply the central limit law to many statistical problems conveniently. It should be noted that the central limit law requires n to approach infinity. In practical applications, we often make n equal to a "sufficiently" large number, such as 1000 above. Is this figure large enough? It depends on what kind of distribution X is. For some distributions, the speed at which the mean distribution approaches to the normal distribution is very slow, which requires us to adopt a larger n value.

Thank you for your reading, the above is the content of "how to achieve the central limit law of Python". After the study of this article, I believe you have a deeper understanding of how to achieve the central limit law of Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report