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

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

  • [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()
相关推荐
小饼干超人8 分钟前
pytorch返回张量元素总数量的方法 x.numel()
人工智能·pytorch·python
张3蜂13 分钟前
java springboot2.0 api ;.netcore8 api ;python GunicornAPI ,哪种更强?请从多个维度,对比分析
java·python·.netcore
u01092727116 分钟前
高级爬虫技巧:处理JavaScript渲染(Selenium)
jvm·数据库·python
七夜zippoe17 分钟前
Plotly + Dash:构建交互式数据仪表盘的艺术与实战
python·信息可视化·架构·dash·回到函数
市场部需要一个软件开发岗位20 分钟前
一个无人机平台+算法监督平台的离线部署指南
java·python·算法·bash·无人机·持续部署
喵手26 分钟前
Python爬虫实战:房产数据采集实战 - 链家二手房&安居客租房多页爬虫完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·房产数据采集·链家二手房/安居客房源采集·采集结果sqlite导出
不懒不懒28 分钟前
【机器学习:下采样 VS 过采样——逻辑回归在信用卡欺诈检测中的实践】
python·numpy·scikit-learn·matplotlib·pip·futurewarning
Leinwin29 分钟前
Moltbot 部署至 Azure Web App 完整指南:从本地到云端的安全高效跃迁
后端·python·flask
叫我辉哥e134 分钟前
新手进阶Python:办公看板集成AI智能助手+语音交互+自动化问答
python
真智AI1 小时前
用 FAISS 搭个轻量 RAG 问答(Python)
开发语言·python·faiss