(一)连续随机量的生成-基于分布函数

连续随机量的生成-基于分布函数

  • [1. 概率积分变换方法(分布函数)](#1. 概率积分变换方法(分布函数))
  • [2. Python编程实现指数分布的采样](#2. Python编程实现指数分布的采样)

1. 概率积分变换方法(分布函数)

Consider drawing a random quantity X X X from a continuous probability distribution with the distribution function F F F. We know F F F is a continues nondecreasing function if F F F has an inverse F − 1 F^{-1} F−1, then Z = F − 1 ( U ) Z=F^{-1}(U) Z=F−1(U), where U U U is a random quantity drawn from U ( [ 0 , 1 ] ) U([0,1]) U([0,1]), is a random quantity as desired. Indeed,
P ( X ⩽ z ) = P ( F − 1 ( U ) ⩽ z ) = P ( U ⩽ F ( z ) ) = F ( z ) , ∀ z ∈ R P(X \leqslant z)=P\left(F^{-1}(U) \leqslant z\right)=P(U \leqslant F(z))=F(z), \forall z \in \mathbb{R} P(X⩽z)=P(F−1(U)⩽z)=P(U⩽F(z))=F(z),∀z∈R

Example : Exponential distribution Exp ⁡ ( 1 ) \operatorname{Exp} (1) Exp(1).

Exp (1) has a probability density function: f ( z ) = { e − z , z ⩾ 0 , 0 , z < 0. f(z)= \begin{cases}e^{-z}, & z \geqslant 0, \\ 0, & z<0 .\end{cases} f(z)={e−z,0,z⩾0,z<0.

Its distribution function is F ( z ) = { 1 − e − z , z ⩾ 0 , 0 , z < 0. F(z)= \begin{cases}1-e^{-z}, & z \geqslant 0, \\ 0, & z<0 .\end{cases} F(z)={1−e−z,0,z⩾0,z<0.

We only need to concentrate on F ( z ) F(z) F(z) on [ 0 , ∞ ) [0, \infty) [0,∞), and have
F − 1 ( z ) = − log ⁡ ( 1 − z ) . F^{-1}(z)=-\log (1-z). F−1(z)=−log(1−z).

So F − 1 ( U ) = − log ⁡ ( 1 − U ) F^{-1}(U)=-\log (1-U) F−1(U)=−log(1−U) has a probability distribution Exp ( 1 ) (1) (1). Because 1 − U ∼ U ( [ 0 , 1 ] ) 1-U \sim U([0,1]) 1−U∼U([0,1]), we have − log ⁡ U ∼ Exp ⁡ ( 1 ) -\log U \sim \operatorname{Exp}(1) −logU∼Exp(1).

For a distribution function which does not have an inverse, we define a generalized inverse as the following:
F − ( z ) = inf ⁡ { x ∈ R : F ( x ) ⩾ z } . F^{-}(z)=\inf \{x \in \mathbb{R}: F(x) \geqslant z\} . F−(z)=inf{x∈R:F(x)⩾z}.

2. Python编程实现指数分布的采样

Assignment: Sample a random quantity Z ∼ Exp ⁡ ( λ ) Z \sim \operatorname{Exp}(\lambda) Z∼Exp(λ) for some λ > 0 \lambda>0 λ>0.

python 复制代码
import numpy as np
import matplotlib.pyplot as plt

# Parameter for the exponential distribution
lambda_value = 0.5

# Generate random quantity using CDF method
u = np.random.rand(1000)  # Uniform random numbers between 0 and 1
Z = -np.log(1 - u) / lambda_value

# Plot histogram
plt.hist(Z, bins=30, density=True, alpha=0.6, color='b', label='Sampled Data')
plt.xlabel('Value')
plt.ylabel('Density')
plt.title('Histogram of Exponential Distribution (Generated using CDF)')
plt.legend()
plt.grid(True)
plt.show()
相关推荐
Johny_Zhao36 分钟前
基于CentOS Stream 8的物联网数据采集与展示方案
linux·网络·python·mqtt·mysql·网络安全·信息安全·云计算·shell·yum源·系统运维
这里有鱼汤1 小时前
想成为下一个吉姆·西蒙斯,这十种经典K线形态你一定要记住
后端·python
Scoful1 小时前
快速用 uv 模拟发布一个 Python 依赖包到 TestPyPI 上,以及常用命令
开发语言·python·uv
xyl8661 小时前
Python 包管理器 uv 介绍
python·uv
databook1 小时前
规则学习:让机器学习像人类一样思考的可解释之路
python·机器学习·scikit-learn
cylat2 小时前
Day23 pipeline管道
人工智能·python·算法·机器学习
蓝桉~MLGT2 小时前
java高级——高阶函数、如何定义一个函数式接口类似stream流的filter
java·开发语言·python
IOT.FIVE.NO.13 小时前
Conda安装pytorch和cuda出现问题的解决记录
人工智能·pytorch·python
山海不说话9 小时前
视频行为标注工具BehaviLabel(源码+使用介绍+Windows.Exe版本)
人工智能·python·计算机视觉·视觉检测
liuzhenghua6611 小时前
Python任务调度模型
java·运维·python