📄 genmix.m
字号:
function genmix(arg)
% GUI FOR GENERATING MULTIVARIATE RANDOM VARIABLES FROM MIXTURE
%
% GENMIX
%
% This GUI will generate random variables using a
% finite mixture model. The user can pick between
% several models:
% 1: COV = sigma^2*I (equal, diagonal covariances)
% 2: COV = sigma_k^2*I (unequal, diagonal covariances)
% 3: COV = SIGMA_hat (equal, full covariance matrix)
% 4. COV = SIGMA_k_hat (unconstrained covariances)
%
% The user can save the random variables to a text file (saved in
% row (observations) and column (variables) format. The data can also
% be saved to the MATLAB Workspace with a user-assigned variable name.
% Model-based Clustering Toolbox, January 2003
if nargin == 0
% Then initialize the GUI.
arg = 'initgui';
end
if strcmp(arg,'initgui')
% Then initialize the GUI.
H.fig = figure('units','normalized',...
'position',[ 0.253 0.168 0.692 0.746],...
'Name','Generate Random Variables from a Finite Mixture',...
'NumberTitle','off',...
'Toolbar','none');
uicontrol(H.fig,'style','text',...
'units','normalized',...
'position',[ 0.10 0.91 0.8 0.075],...
'horizontal','left',...
'string',['This GUI will generate random variables from a finite mixture model.',...
' Enter the required data and hit the button to generate the data set.',...
' The data can be saved to the MATLAB workspace or written to a text file.'],...
'fontweight','bold');
uicontrol(H.fig,'style','text',...
'units','normalized',...
'position',[0.10 0.85 0.3 0.04],...
'String','Step 1: Choose the number of dimensions:',...
'horizontal','left');
uicontrol(H.fig,'style','text',...
'units','normalized',...
'position',[0.10 0.78 0.3 0.04],...
'String','Step 2: Enter the number of observations:',...
'horizontal','left');
uicontrol(H.fig,'style','text',...
'units','normalized',...
'position',[0.1 0.71 0.3 0.04],...
'string','Step 3: Choose the number of components:',...
'horizontal','left');
uicontrol(H.fig,'style','text',...
'units','normalized',...
'position',[0.1 0.64 0.3 0.04],...
'string','Step 4: Choose the model:',...
'horizontal','left');
uicontrol(H.fig,'style','frame',...
'units','normalized',...
'position',[ 0.412 0.53 0.488 0.147]);
uicontrol(H.fig,'style','text',...
'units','normalized',...
'position', [0.1 0.45 0.3 0.06],...
'String','Step 5: Enter the component weights, separated by commas or blanks:',...
'horizontal','left');
uicontrol(H.fig,'style','text',...
'units','normalized',...
'position',[0.1 0.36 0.3 0.06],...
'String','Step 6: Enter the means for each component - push button:',...
'horizontal','left');
uicontrol(H.fig,'style','text',...
'units','normalized',...
'position',[0.1 0.27 0.3 0.06],...
'String','Step 7: Enter the covariance matrices for each component - push button:',...
'horizontal','left');
uicontrol(H.fig,'style','text',...
'units','normalized',...
'position',[0.1 0.18 0.3 0.06],...
'String','Step 8: Push button to generate random variables:',...
'horizontal','left');
H.plot = uicontrol(H.fig,'style','pushbutton',...
'units','normalized',...
'position',[0.1 0.07 0.15 0.065],...
'String','Plot Data',...
'Callback','genmix(''plotdata'')');
H.saveworkspace = uicontrol(H.fig,'style','pushbutton',...
'units','normalized',...
'position',[0.3 0.07 0.15 0.065],...
'String','Save to Workspace',...
'Callback','genmix(''saveworkspace'')');
H.savefile = uicontrol(H.fig,'style','pushbutton',...
'units','normalized',...
'position',[0.5 0.07 0.15 0.065],...
'String','Save to File',...
'CallBack','genmix(''savefile'')');
H.close = uicontrol(H.fig,'style','pushbutton',...
'units','normalized',...
'position',[0.7 0.07 0.15 0.065],...
'String','Close',...
'CallBack','close(gcf)');
H.dims = uicontrol(H.fig,'style','popup',...
'units','normalized',...
'position',[0.45 0.85 0.1 0.04],...
'String','2|3|4|5|6|7|8|9|10',...
'Backgroundcolor','w');
H.nobs = uicontrol(H.fig,'style','edit',...
'units','normalized',...
'position',[0.45 0.78 0.1 0.04],...
'String','1000',...
'Backgroundcolor','w');
H.ncomp = uicontrol(H.fig,'style','popup',...
'units','normalized',...
'position',[0.45 0.71 0.1 0.04],...
'String','1|2|3|4|5|6|7|8|9|10',...
'Backgroundcolor','w',...
'Value',2);
H.model(1) = uicontrol(H.fig,'style','radio',...
'units','normalized',...
'position',[0.44 0.62 0.2 0.047],...
'String','M1 - spherical/equal',...
'Value',1,...
'CallBack','genmix(''model'')');
H.model(2) = uicontrol(H.fig,'style','radio',...
'units','normalized',...
'position',[0.44 0.55 0.2 0.047],...
'String','M2 - spherical/unequal',...
'CallBack','genmix(''model'')');
H.model(3) = uicontrol(H.fig,'style','radio',...
'units','normalized',...
'position',[0.66 0.62 0.2 0.047],...
'String','M3 - elliptical/equal',...
'CallBack','genmix(''model'')');
H.model(4) = uicontrol(H.fig,'style','radio',...
'units','normalized',...
'position',[0.66 0.55 0.2 0.047],...
'String','M4 - elliptical/unequal',...
'CallBack','genmix(''model'')');
H.pies = uicontrol(H.fig,'style','edit',...
'units','normalized',...
'position',[ 0.45 0.46 0.4 0.04],...
'String','0.5, 0.5',...
'backgroundcolor','w',...
'horizontal','left',...
'CallBack','genmix(''checkpies'')');
H.mus = uicontrol(H.fig,'style','pushbutton',...
'units','normalized',...
'position',[0.45 0.36 0.2 0.065],...
'String','Enter means...',...
'CallBack','genmix(''entermeans'')');
uicontrol(H.fig,'style','text',...
'units','normalize',...
'position', [0.65 0.41 0.3, 0.05],...
'String','Press to view in command window:');
uicontrol(H.fig,'style','pushbutton',...
'units','normalized',...
'position',[0.7 0.36 0.2 0.065],...
'String','View Current Means',...
'Tooltipstring','View current means in command window.',...
'CallBack','genmix(''viewmeans'') ');
H.covs = uicontrol(H.fig,'style','pushbutton',...
'units','normalized',...
'position',[0.45 0.27 0.2 0.065],...
'String','Enter covariance matrices...',...
'CallBack','genmix(''entercovs'')');
uicontrol(H.fig,'style','pushbutton',...
'units','normalized',...
'position',[0.7 0.27 0.2 0.065],...
'String','View Current Covariances',...
'Tooltipstring','View current covariances in command window.',...
'CallBack',' genmix(''viewcovs'')');
H.genrv = uicontrol(H.fig,'style','pushbutton',...
'units','normalized',...
'position',[0.45 0.18 0.2 0.065],...
'String','Generate RV''s ...',...
'CallBack','genmix(''genrvs'')');
% Initialize parameters
H.weights = [0.5, 0.5];
H.means = [3, -3; 3, -3];
H.covms(:,:,1) = eye(2);
H.covms(:,:,2) = eye(2);
H.data = [];
set(gcf,'userdata',H);
elseif strcmp(arg,'genrvs')
% Generate the random variables.
H = get(gcf,'userdata');
genrvs(H);
elseif strcmp(arg,'model')
% Make the model choices mutually exclusive.
H = get(gcf,'userdata');
% Get the handle for the callback object
Hon = gcbo;
% Get the handles for the radio buttons.
Hmods = H.model;
set(Hmods,'Value',0)
set(Hon,'Value',1)
elseif strcmp(arg,'checkpies')
% write some code to make sure the pies are ok.
% should check number of pies, also must be < 1, must sum to 1.
H = get(gcf,'userdata');
strg = get(H.pies,'string');
nc = get(H.ncomp,'value');
eval(['pies = [' strg '];']);
if length(pies) ~= nc
errordlg('The number of weights is incorrect.','Entry Error')
return
end
% Save the pies
H.weights = pies;
set(H.fig,'userdata',H)
elseif strcmp(arg,'entermeans')
% write the code to enter the means - use inputdlg box.
H = get(gcf,'userdata');
entermeans(H)
elseif strcmp(arg,'entercovs')
% write code to enter the covariances - use inputdlg box.
H = get(gcf,'userdata');
entercovs(H);
elseif strcmp(arg,'plotdata')
% write code to plot the data.
H = get(gcf,'userdata');
plotdata(H);
elseif strcmp(arg,'saveworkspace')
H = get(gcf,'userdata');
saveworkspace(H)
elseif strcmp(arg,'savefile')
H = get(gcf,'userdata');
savefile(H)
elseif strcmp(arg,'viewmeans')
H = get(gcf,'userdata');
viewmeans(H);
elseif strcmp(arg,'viewcovs')
H = get(gcf,'userdata');
viewcovs(H);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ENTER THE MEANS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function entermeans(H)
ndim = get(H.dims,'value')+1;
ncomp = get(H.ncomp,'value');
title = 'Input for Component Means';
prompt = cell(ncomp,1);
def = cell(ncomp,1);
% Get a temporary matrix for display purposes.
for i = 1:ncomp
% Set up the strings for the dialog box.
prompt{i} = ['Enter the ' int2str(ndim) '-dimensional mean for component ' int2str(i) ', separate values by commas or blanks:'];
tmp = mat2str(i*ones(1,ndim));
n = length(tmp);
def{i} = tmp(2:(n-1));
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -