java实现混合高斯_从r中的双高斯混合生成样本(MATLAB中给出的代码)

我正在尝试创建(在r中)等效于以下MATLAB函数,该函数将从N(m1,(s1)^ 2)和N(m2,(s2)^ 2)的混合生成n个样本,alpha,来自第一个高斯 .

我有一个开始,但MATLAB和R之间的结果明显不同(即,MATLAB结果偶尔给出-8的值,但R版本甚至不给出值-5) . Please help me sort out what is wrong here. 谢谢:-)

例如:绘制来自N(0,1)和N(0,36)的混合的1000个样本和来自第一高斯的95%的样本 . 将样本归一化为零和标准差1 .

MATLAB

功能

function y = gaussmix(n,m1,m2,s1,s2,alpha)

y = zeros(n,1);

U = rand(n,1);

I = (U < alpha)

y = I.*(randn(n,1)*s1+m1) + (1-I).*(randn(n,1)*s2 + m2);

履行

P = gaussmix(1000,0,0,1,6,.95)

P = (P-mean(P))/std(P)

plot(P)

axis([0 1000 -15 15])

hist(P)

axis([-15 15 0 1000])

结果情节

f876b11d-05e2-4b7e-b6d3-657a54ff3e48.jpg

得到的组织

6096cccf-3b22-4e71-8500-7c48431e7e0e.jpg

R

yn

s

sn

plot(sn, xlim=range(0,1000), ylim=range(-15,15))

hist(sn, xlim=range(-15,15), ylim=range(0,1000))

结果情节

795b17ae-55d3-41e1-b69e-329f4c4ceba8.jpg

得到的组织

13912990-f497-4a23-ba65-85f02b9db64e.jpg

一如既往,谢谢!

SOLUTION

gaussmix

U

I

y

(1-I)*rnorm(nsim,mean=mean_2,sd=std_2)

return(y)

}

z1

z1_standardized

z2

z2_standardized

z3

z3_standardized

par(mfrow=c(2,3))

hist(z1_standardized,xlim=c(-10,10),ylim=c(0,500),

main="Histogram of 95% of N(0,1) and 5% of N(0,36)",

col="blue",xlab=" ")

hist(z2_standardized,xlim=c(-10,10),ylim=c(0,500),

main="Histogram of 80% of N(0,1) and 10% of N(3,1)",

col="blue",xlab=" ")

hist(z3_standardized,xlim=c(-10,10),ylim=c(0,500),

main="Histogram of samples of LN(0,1)",col="blue",xlab=" ")

##

plot(z1_standardized,type='l',

main="1000 samples from a mixture N(0,1) and N(0,36)",

col="blue",xlab="Samples",ylab="Mean",ylim=c(-10,10))

plot(z2_standardized,type='l',

main="1000 samples from a mixture N(0,1) and N(3,1)",

col="blue",xlab="Samples",ylab="Mean",ylim=c(-10,10))

plot(z3_standardized,type='l',

main="1000 samples from LN(0,1)",

col="blue",xlab="Samples",ylab="Mean",ylim=c(-10,10))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
% This folder contains a collection of "fitting" functions. % (Some has demo options - the third section) % The GENERAL input to the functions should be samples of the distribution. % % for example, if we are to fit a normal distribution ('gaussian') with a mean "u" and varaince "sig"^2 % then the samples will distribute like: % samples = randn(1,10000)*sig + u % %fitting with Least-Squares is done on the histogram of the samples. % fitting with Maximum likelihood is done directly on the samples. % % % Contents of this folder % ======================= % 1) Maximum likelihood estimators % 2) Least squares estimators % 3) EM algorithm for estimation of multivariant gaussian distribution (mixed gaussians) % 4) added folders: Create - which create samples for the EM algorithm test % Plot - used to plot each of the distributions (parametric plot) % % % % % % Maximum likelihood estimators % ============================= % fit_ML_maxwell - fit maxwellian distribution % fit_ML_rayleigh - fit rayleigh distribution % (which is for example: sqrt(abs(randn)^2+abs(randn)^2)) % fit_ML_laplace - fit laplace distribution % fit_ML_log_normal- fit log-normal distribution % fit_ML_normal - fit normal (gaussian) distribution % % NOTE: all estimators are efficient estimators. for this reason, the distribution % might be written in a different way, for example, the "Rayleigh" distribution % is given with a parameter "s" and not "s^2". % % % least squares estimators % ========================= % fit_maxwell_pdf - fits a given curve of a maxwellian distribution % fit_rayleigh_pdf - fits a given curve of a rayleigh distribution % % NOTE: these fit function are used on a histogram output which is like a sampled % distribution function. the given curve MUST be normalized, since the estimator % is trying to fit a normalized distribution function. % % % % % Multivariant Gaussian distribution % ================================== % for demo of 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值