Random Process Extra (Answer to Random Fitting Homework)

I wrote three random simulation codes of random processes in one night, and shared them for your reference.
Insert picture description here


1. If a random variable obeys the expectation as μ \muμ , the covariance matrix isΣ \SigmaThe normal distribution of Σ . Then if the multidimensional normal distribution is non-degenerate, then the covariance matrix must be positive definite (why), and combined with its own properties, you can knowΣ \SigmaΣ is a symmetric positive definite matrix. For such a matrix, we can performCholesky decomposition(leave the "pit") on it, and decompose it intoΣ = L ⋅ LT \Sigma=L\cdot L^TΣ=LLThe form of T , whereLLL is the lower triangular matrix.

In this case, if the random variable XXX obeysN (0, I) N(\textbf{0},I)N(0,I ) , thenY = L ⋅ X + μ Y=L\cdot X+\muY=LX+μ obeysN (μ, Σ) N(\mu,\Sigma)N ( μ ,Σ ) is normally distributed. According to this idea, we canXXX performs a linear transformation to get the random variable we hope to get.

The code I implemented in python is as follows:

from scipy import linalg
import numpy as np
import random
import matplotlib.pyplot as plt
'''
Mu=np.array([[1,1]]).T
print(np.shape(Mu))
Delta=np.array([[1,1],[1,3]])
L = linalg.cholesky(Delta, lower=True)
print(np.dot(L,L.T))
# L \cdot L^T =Delta=E((x-mu) \cdot (x-mu)^T)(2*1\cdot 1*2)=E(L y \cdot y^T L^T)
#设y服从N(0,E),则 x=Ly+Mu
'''
def TwoD_Normalnum(Mu,Delta):
    '''
    Create random number of 2-Dimension Norm distribution on expection of Mu and Coverence matrix of Delta.
    :param Mu: expection
    :param Delta: Coverence matrix
    :return: random vector
    '''
    L = linalg.cholesky(Delta, lower=True)#获得下三角矩阵
    x=random.normalvariate(0,1)
    y=random.normalvariate(0,1)
    Y=np.array([[x],[y]])
    X=np.dot(L,Y)+Mu
    return X

if __name__=='__main__':
    Mu=np.array([[1,1]]).T
    Delta=np.array([[1,1],[1,3]])
    x=[]
    y=[]
    for i in range(2000):
        a=TwoD_Normalnum(Mu,Delta)
        x.append(a[0])
        y.append(a[1])
    plt.scatter(x,y)
    plt.grid(True)
    plt.savefig('2003.jpg')
    plt.show()

The result is (1 1) \left( \begin{matrix}1 \\ 1\end{matrix}\right)(11) Is the expectation, with(1 1 1 3) \left( \begin{matrix}1&1 \\ 1&3\end{matrix}\right)(1113) Is the image of the two-dimensional normal distribution of the covariance matrix as follows:
Insert picture description here


2. First of all, we determine the overall idea and use a computer to simulate the law of large numbers. In fact, we see whether the frequency change process with n can converge to a point near. Therefore, by drawing n − F requent n-FrequentnF r e q u e n t image to verify the law of large numbers.

First of all, we have learned that the normal distribution conforms to the theorem of large numbers, while the Cauchy distribution does not satisfy the theorem of large numbers because the expectation does not exist.

The normal distribution has the corresponding function random.normalvariate() in the random library to generate random numbers, while the Cauchy distribution does not have a ready-made function.

At this time, we think of the existence theorem of random variables. For a given distribution function, we can obey U [0, 1] U[0,1]U [ 0 ,1 ] random variableXXX generates the corresponding random variableYYY
P ( F − 1 ( X ) < x 0 ) = P ( X < F ( x 0 ) ) = F ( x 0 ) P(F^{-1}(X)<x_0)=P(X<F(x_0))=F(x_0) P(F1(X)<x0)=P(X<F(x0))=F(x0)
Therefore, the random variableY = F − 1 (X) Y=F^(-1)(X)Y=F1 (X) isthe distribution functionFFRandom variable of F.

We use python code to implement it:

import matplotlib.pyplot as plt
import random
import numpy as np
import math

if __name__=='__main__':
    plt.subplot(1,2,1)
    #正态分布(n,frequent)图像
    n=list(np.arange(1,1000000,1))
    F1=[]
    sum=0
    for i in n:
        sum+=random.normalvariate(0,1)
        F1.append(sum/i)
    plt.plot(n,F1)
    plt.grid(True)
    plt.subplot(1,2,2)
    #Cauchy分布(n,frequent)图像
    #Cauchy分布:f=(1/\pi)*(1/(1+x^2))
    #需要通过F^(-1)(X),X~U[0,1]构造复合Cauchy分布的随机变量
    #x=tan(\pi*(F-1/2))
    F2= []
    sum = 0
    for i in n:
        sum += math.tan(math.pi*(random.uniform(0, 1)-1/2))
        F2.append(sum / i)
    plt.plot(n, F2)
    plt.grid(True)
    plt.savefig('1608.jpg')
    plt.show()

The obtained degree-frequency diagram corresponding to the normal distribution and the Cauchy distribution is shown in the following figure:
Insert picture description here


3. The third question is simulated by computer. The easiest to think of is the Monte Carlo method ( MC ), which obeys U [0, 1] × U [0, 1] U[0,1]×U[0,1 ]U [ 0 ,1]×U [ 0 ,1 ] two-dimensional random variable, check the position of the
Insert picture description here
drop point:if the drop point is below the curve, count the variableMMM is added by one, otherwise it is not added.
Then according to the law of large numbers, there should belim N → ∞ MN = I nt (e − x) S [0, 1] × [0, 1] lim_{N\rightarrow\infty}\frac{M}{N}= \frac{Int(e^{-x})}{S_{[0,1]×[0,1]}}l i mNNM=S[0,1]×[0,1]I n t ( ex)
We coded this idea and attached the results:

import random
import numpy as np
import math
import matplotlib.pyplot as plt

#Using MC mathod to culculate the internal of e^(-x) from 0 to 1.
n=list(np.arange(1,100000,1))
I=[]
sum=0
for i in n:
    X=random.uniform(0,1)
    Y=random.uniform(0,1)
    if Y<math.exp(-X):
        sum+=1
    I.append(sum/i)

plt.plot(n,I)
plt.text(70000,0.7,'stimulink result:'+str(round(I[-1],4)))
plt.text(70000,0.65,'culculate result:'+str(round(1-1/math.e,4)))
plt.savefig('1622.jpg')
plt.show()

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45014265/article/details/114947156