em_generate_sample.m

来自「EM分群,matlab程式碼,用來分群用的」· M 代码 · 共 49 行

M
49
字号
function sim_data = EM_Generate_sample(n_points, distr_type, mix_weights, mod_par, rnd_state)

% FUNCTION sample_data = EM_Generate_sample(n_points, n_mix, pdf_type, mix_weights, mod_par, rand_seed)
%
% INPUT
%
% n_points      : Number of data points to be generated
% distr_type      : Cell array of distribution types
% mix_weights   : Array of weights
% mod_par       : (Cell) Array of PDFs' parameters 
%
% OUTPUT
%
% sample_data   : Generated data set, (x_i, z_i) i=1,...,n_points
%                 x_i observed data
%                 z_i hidden data (module indexes)



rand('state',rnd_state);

n_mix = length(distr_type);


for ii=1:n_mix    
    y = EM_Handle_list(distr_type{ii});        
    rnd_handle(ii) = y(3);    
end


mix_weights = reshape(mix_weights,n_mix,1)/sum(mix_weights);

cum_prob_coeff = cumsum(mix_weights);

sim_data = zeros(n_points,2);

 
for ii = 1:n_points

    z = rand;    
    i_mix =  min(find(cum_prob_coeff >= z));    
    y = feval(rnd_handle(i_mix),mod_par{i_mix});    
    sim_data(ii,:) = [y i_mix];    
    
end



⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?