[英]Importance sampling on mixed normal distribution
How do you compute the mean of a gaussian mixture model via importance sampling?如何通过重要性采样计算高斯混合 model 的平均值? Say i have a model such that there is a 60% chance of being sampled from a N(-1,1) distribution, and a 40% chance of being sampled from a N(2,1/9) distribution.假设我有一个 model ,因此有 60% 的机会从 N(-1,1) 分布中抽样,有 40% 的机会从 N(2,1/9) 分布中抽样。 Below is what i have from the standard importance sampling format of g*f(x)/h;以下是我从 g*f(x)/h 的标准重要性采样格式中得到的内容; but i do not think my function f is correct because I used a summation mix function instead of truly sampling through probability.但我不认为我的 function f 是正确的,因为我使用了总和混合 function 而不是真正通过概率抽样。 Is there any suggestion on what I should change?关于我应该改变什么有什么建议吗? thanks!谢谢!
set.seed(100)
N = 100
x = rnorm(N,mean=0,sd=0.6) # Sample x samples from the proposal distribution h(x)
h = dnorm(x,mean=0,sd=0.6) # Evaluate h(x)
g = rep(0,N)
g=x
f = function(x) { # Evaluate f(x)
f = 0.6*rnorm(x,-1,1)+0.4*rnorm(x,2,1/9)
return(f)
}
mean(g*f(x)/h)
For importance sampling, I guess there are several points that you should change in your code:对于重要性抽样,我想您应该在代码中更改几点:
N
您可能需要足够大的N
dnorm
, rather than rnorm
in your function f
您应该在 function f
中使用dnorm
而不是rnorm
An example is as below:一个例子如下:
N = 1e8
X = rnorm(N) # Sample x samples from the proposal distribution h(x)
h = dnorm(X) # Evaluate h(x)
f = function(x) 0.6*dnorm(x,-1,1)+0.4*dnorm(x,2,1/9) # Evaluate f(x)
mean(X*f(X)/h)
# [1] 0.2002296
As you can see, the theoretical mean of the mixed normal distribution is 0.6*(-1) + 0.4*2 = 0.2
, which is consistent to the result via importance sampling.如您所见,混合正态分布的理论平均值为0.6*(-1) + 0.4*2 = 0.2
,这与重要性采样的结果一致。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.