在RStudio中使用逆变换法生成Logistic与Cauchy分布随机变量求助
Got it, let's work through this problem step by step! The inverse transform method is perfect for this task—once we have the inverse of the cumulative distribution function (CDF), generating random variables is straightforward. Let's start with deriving the inverse CDFs for both distributions, then jump into the RStudio code.
First, let's recap the key formulas and solve for the inverse of each CDF (since we need to map uniform [0,1] random numbers to our target distributions).
1.1 Standard Logistic Distribution
The standard Logistic distribution (location μ=0, scale s=1) has:
- PDF: $f(x) = \frac{e^{-x}}{(1 + e{-x})2}$
- CDF: $F(x) = \frac{1}{1 + e^{-x}}$
To find the inverse $F^{-1}(u)$ (where $u$ is a uniform random variable on [0,1]):
Start with $u = \frac{1}{1 + e^{-x}}$
Rearrange: $1 + e^{-x} = \frac{1}{u}$
$e^{-x} = \frac{1}{u} - 1 = \frac{1 - u}{u}$
Take natural log of both sides: $-x = \ln\left(\frac{1 - u}{u}\right)$
Final inverse: $x = \ln\left(\frac{u}{1 - u}\right)$
1.2 Standard Cauchy Distribution
The standard Cauchy distribution (location μ=0, scale γ=1) has:
- PDF: $f(x) = \frac{1}{\pi(1 + x^2)}$
- CDF: $F(x) = \frac{1}{2} + \frac{1}{\pi}\arctan(x)$
Solving for the inverse $F^{-1}(u)$:
Start with $u = \frac{1}{2} + \frac{1}{\pi}\arctan(x)$
Rearrange: $\arctan(x) = \pi\left(u - \frac{1}{2}\right)$
Final inverse: $x = \tan\left(\pi\left(u - \frac{1}{2}\right)\right)$
The core idea is: generate 10000 uniform [0,1] random numbers, then plug each into the inverse CDF we derived. We'll also add code to verify the results match the expected distributions.
2.1 Generate Standard Logistic Random Variables
# Set random seed for reproducibility set.seed(123) # Generate 10000 uniform [0,1] values u_logistic <- runif(n = 10000) # Apply inverse CDF to get Logistic random variables logistic_rvs <- log(u_logistic / (1 - u_logistic)) # Optional: Verify with histogram + theoretical PDF hist(logistic_rvs, freq = FALSE, main = "Standard Logistic Distribution", xlab = "Random Variable Value", col = "lightgray") curve(dlogis(x), add = TRUE, col = "darkred", lwd = 2) legend("topright", legend = c("Empirical", "Theoretical"), col = c("lightgray", "darkred"), lwd = c(2,2))
2.2 Generate Standard Cauchy Random Variables
# Set a new seed (optional, but keeps results separate) set.seed(456) # Generate 10000 uniform [0,1] values u_cauchy <- runif(n = 10000) # Apply inverse CDF to get Cauchy random variables cauchy_rvs <- tan(pi * (u_cauchy - 0.5)) # Optional: Verify with histogram + theoretical PDF # Cauchy has heavy tails, so we'll limit x-axis to [-10,10] for readability hist(cauchy_rvs, freq = FALSE, main = "Standard Cauchy Distribution", xlab = "Random Variable Value", col = "lightblue", xlim = c(-10,10)) curve(dcauchy(x), add = TRUE, col = "darkblue", lwd = 2) legend("topright", legend = c("Empirical", "Theoretical"), col = c("lightblue", "darkblue"), lwd = c(2,2))
3. Non-Standard Parameters (If Needed)
If you need Logistic/Cauchy distributions with custom location/scale parameters, just adjust the inverse CDF results:
- For a Logistic distribution with location $\mu$ and scale $s$:
mu <- 2 s <- 0.5 logistic_nonstandard <- mu + s * log(u_logistic / (1 - u_logistic)) - For a Cauchy distribution with location $\mu$ and scale $\gamma$:
mu <- 1 gamma <- 3 cauchy_nonstandard <- mu + gamma * tan(pi * (u_cauchy - 0.5))
Let me know if you run into any issues with the code or need further clarification on the math!
内容的提问来源于stack exchange,提问作者Maria Bluee

