概率密度函数可视化

15 篇文章 1 订阅
该文通过R语言展示了如何对一维和二维随机变量的概率密度函数进行可视化。一维情形中,以正态分布为例,使用`ggplot2`库绘制了密度图;在二维情形下,利用多元正态分布,通过`MASS`库生成随机数据并创建了二维密度图,进一步用`rayshader`库增强视觉效果。此外,还提供了联合密度函数的数学表达式以及使用`persp`函数创建的三维视角图。
摘要由CSDN通过智能技术生成

概率密度函数可视化

1 一维随机变量情形

以正态概率密度函数为例,其中位置参数为 μ \mu μ,尺度参数为 σ \sigma σ
f ( x ) = 1 2 π σ e − ( x − μ ) 2 2 σ 2 , x ∈ R f(x) = \dfrac{1}{\sqrt{2\pi}\sigma}e^{-\dfrac{(x-\mu)^2}{2\sigma^2}},x\in R f(x)=2π σ1e2σ2(xμ)2,xR

library(tidyverse)
# 一维情形
set.seed(1)
N <- 10000
data <- data.frame(v1 = rnorm(N, 1, 1))
data %>% ggplot(aes(x = v1)) +
  geom_density(colour = 2, size = 1.5, fill = "#009933") +
  xlab("X") +
  ylab("Density") +
  theme_bw() +
  theme(axis.title.x = element_text(vjust = 1, size = 15)) +
  theme(axis.title.y = element_text(vjust = 1, size = 15)) +
  theme(axis.text.x = element_text(vjust = 1, size = 15)) +
  theme(axis.text.y = element_text(vjust = 1, size = 15)) +
  annotate("text",
    x = 3, y = 0.3, parse = TRUE, size = 6,
    label = "y==frac(1, sqrt(2*pi)) * e^{frac(-(x-1)^2 , 2)}"
  )

在这里插入图片描述


2 二维随机变量情形

以多元正态概率密度函数为例,随机变量 X 1 … X n X_1\dots X_n X1Xn的联合正态概率密度函数为
p ( x ) = p ( x 1 , x 2 , … , x n ) = 1 ( 2 π ) n / 2 ∣ Σ ∣ 1 / 2 exp ⁡ ( − 1 2 ( x − μ ) T Σ − 1 ( x − μ ) ) p(x)=p\left(x_{1}, x_{2}, \ldots, x_{n}\right)=\frac{1}{(2 \pi)^{n / 2}|\Sigma|^{1 / 2}} \exp \left(-\frac{1}{2}(x-\mu)^{T} \Sigma^{-1}(x-\mu)\right) p(x)=p(x1,x2,,xn)=(2π)n/2∣Σ1/21exp(21(xμ)TΣ1(xμ))
其中 Σ \Sigma Σ为随机变量 X 1 … X n X_1\dots X_n X1Xn的方差协方差矩阵, μ \mu μ为各随机变量均值向量。当 n = 2 n=2 n=2时,概率密度函数为
p ( x ) = 1 2 π σ 1 σ 2 1 − ρ 2 exp ⁡ ( − 1 2 ( x 1 − μ 1 σ 1 ) 2 − 2 ρ ( x 1 − μ 1 σ 1 ) ( x 2 − μ 2 σ 2 ) + ( x 2 − μ 2 σ 2 ) 2 1 − ρ 2 ) p(x) = \frac{1}{2 \pi \sigma_{1} \sigma_{2} \sqrt{1-\rho^{2}}} \exp \left(-\frac{1}{2} \frac{\left(\frac{x_{1}-\mu_{1}}{\sigma_{1}}\right)^{2}-2 \rho\left(\frac{x_{1}-\mu_{1}}{\sigma_{1}}\right)\left(\frac{x_{2}-\mu_{2}}{\sigma_{2}}\right)+\left(\frac{x_{2}-\mu_{2}}{\sigma_{2}}\right)^{2}}{1-\rho^{2}}\right) p(x)=2πσ1σ21ρ2 1exp 211ρ2(σ1x1μ1)22ρ(σ1x1μ1)(σ2x2μ2)+(σ2x2μ2)2
其中 ρ \rho ρ表示 x 1 x_1 x1 x 2 x_2 x2的相关系数。

# 二维情形
rm(list = ls())
n <- 10000
mean <- c(-1, 1)
sigma <- matrix(c(1, 0.7, 0.7, 1), nrow = 2, ncol = 2)
mydata <- as.data.frame(MASS::mvrnorm(n, mean, sigma))

# 二维变量散点图
mydata %>% ggplot( aes(x = V1, y = V2)) +
  geom_bin2d(bins = 80) +
  scale_fill_viridis_c(option = "D") +
  theme_bw()+
  guides(fill = guide_colorbar(title = "频数"))+
  theme(legend.position = c(0.9,0.2), legend.title = element_text(size = 15))

在这里插入图片描述


# 二维随机变量联合密度
gg <- mydata %>%  ggplot( aes(x = V1, y = V2)) +
  stat_density_2d(aes(fill = stat(nlevel)), geom = "polygon", colour = "white") +
  # 轴标签
  xlab("随机变量1") +
  ylab("随机变量2") +
  # 背景颜色
  theme_bw() +
  # 填充颜色设置
  scale_fill_viridis_c(option = "D") +
  # x轴标签设置
  theme(axis.title.x = element_text(vjust = -1, size = 30)) +
  # y轴标签设置
  theme(axis.title.y = element_text(vjust = 1, size = 30)) +
  # x轴刻度尺标签设置
  theme(axis.text.x = element_text(vjust = 1, size = 30)) +
  # y轴刻度尺标签设置
  theme(axis.text.y = element_text(vjust = 1, size = 30)) +
  # 图例设置
  guides(fill = guide_colorbar(title = " ")) +
  theme(legend.position = c(0.15, 0.8), legend.title = element_text(size = 15))
rayshader::plot_gg(gg, multicore = TRUE, width = 10, height = 10, scale = 1000)
rgl:rgl.postscript("rgl1.svg", fmt = "svg",drawText = TRUE)

在这里插入图片描述


u1 = 0
u2 = 0
sigma1 = 1
sigma2 = 1
rho = 0.7

f <- function(X,Y){
  return( 1/(2*pi*sigma1*sigma2*sqrt(1-rho^2))*exp(-1/(2*sqrt(1-rho^2))*(X^2+Y^2-2*rho*X*Y)))
}
X = seq(-4,4,0.1)
Y = seq(-4,4,0.1)
Z = outer(X,Y,f)
library(shape)
persp(X,Y,Z,theta = -40,phi=20,expand = 0.6,r = 10,shade = 0.01,
      ticktype = "detailed",xlab = "X",ylab = "Y",zlab = expression(f(X,Y)),
      col =  drapecol(Z))

在这里插入图片描述


-END-
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值