MATLAB中的反正常混合cdf

时间:2017-03-22 11:51:36

标签: matlab

我正在尝试计算正常混合物的逆cdf。我写了以下代码:

clear vq
vd = randn(1000,1);%create uniform random samples that will 
%spread over the whole range of the forecast variable
X=(-5:0.0001:5)'; %set values to calculate CDF for to create a lookup table
k = 5; %number of mixture components
GMModel = fitgmdist(X,k); %Fit Normal mixture distribution to data
normalcdf=cdf(GMModel,X); %Calculate the normal distribution fuction CDF
LookupNormCDF=[X, normalcdf]; %Create a lookup table for quartiles of the CDF

for i=1:length(vd)
vq(i) = (interp1(normalcdf,X,vd(i))); %Use the lookup table to 
%find each quartiles from the cdf value (vd)

end %loop through this for each value of vd
vq=vq';

figure;
plot(X,pdf(GMModel,X))
hold on
plot(X,cdf(GMModel,X))

作为我上面代码的测试,我设置k = 1并与之比较:

vq000=invnormcdf(vd); 

当vd不在0到1的范围内时,两个脚本显然都给出了NaNs,但是当vd接近于零时,我得到的vq结果也是NaN,而在这种情况下,我得到vq000的值。结果之间似乎没有相关性(对于k = 1)我得到vq和vq000,尽管vd的值相同(脚本运行相同)。我原本期望vq和vq000更相似(例如+/- 0.2,显然它们不能与我使用两种不同的估算方法完全相同)。

我用于invnormcdf的脚本是:

function x = invnormcdf(p)
%INVNORMCDF(P)  Normal quantile function
% X = INVNORMCDF(P) returns the P-th quantile of the standard normal distribution.
% In other words, it returns X such that P = NORMCDF(X).

x = erfinv(2*p-1)*sqrt(2);

0 个答案:

没有答案