Download code
From LiteratePrograms
Back to Probability_distributions_(MATLAB)
Download for Windows: zip
Download for UNIX: zip, tar.gz, tar.bz2
script_gbimodal.m
1 % Copyright (c) 2010 the authors listed at the following URL, and/or 2 % the authors of referenced articles or incorporated external code: 3 % http://en.literateprograms.org/Probability_distributions_(MATLAB)?action=history&offset=20061020163719 4 % 5 % Permission is hereby granted, free of charge, to any person obtaining 6 % a copy of this software and associated documentation files (the 7 % "Software"), to deal in the Software without restriction, including 8 % without limitation the rights to use, copy, modify, merge, publish, 9 % distribute, sublicense, and/or sell copies of the Software, and to 10 % permit persons to whom the Software is furnished to do so, subject to 11 % the following conditions: 12 % 13 % The above copyright notice and this permission notice shall be 14 % included in all copies or substantial portions of the Software. 15 % 16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 % 24 % Retrieved from: http://en.literateprograms.org/Probability_distributions_(MATLAB)?oldid=7867 25 26 27 q = [.4 .6]; 28 m = [0 50]; 29 s = [5 5]; 30 distrib = struct('mu', m, 'sigma', s, 'weight', q) 31 nb = 200; 32 33 34 %% Construction of two scaled and translated gaussians 35 X = randn(nb,length(q)).*repmat(s,nb,1)+repmat(m,nb,1); 36 37 38 %% Selection of one gaussian or the other 39 rsel = rand(nb,1); 40 idx1 = (repmat(rsel,1,length(q))>repmat(cumsum(q),nb,1)); 41 idx2 = (repmat(rsel,1,length(q))<repmat(cumsum(q),nb,1)); 42 idx1(:,2:end) = idx1(:,1:end-1).*idx2(:,2:end); 43 idx1(:,1) = idx2(:,1); 44 X = sum(X.*idx1,2); 45 46 47 %% Plot result 48 [y,x]=ksdensity(X);figure;plot(x,y,'linewidth',2) 49
sript_multinomial.m
1 % Copyright (c) 2010 the authors listed at the following URL, and/or 2 % the authors of referenced articles or incorporated external code: 3 % http://en.literateprograms.org/Probability_distributions_(MATLAB)?action=history&offset=20061020163719 4 % 5 % Permission is hereby granted, free of charge, to any person obtaining 6 % a copy of this software and associated documentation files (the 7 % "Software"), to deal in the Software without restriction, including 8 % without limitation the rights to use, copy, modify, merge, publish, 9 % distribute, sublicense, and/or sell copies of the Software, and to 10 % permit persons to whom the Software is furnished to do so, subject to 11 % the following conditions: 12 % 13 % The above copyright notice and this permission notice shall be 14 % included in all copies or substantial portions of the Software. 15 % 16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 % 24 % Retrieved from: http://en.literateprograms.org/Probability_distributions_(MATLAB)?oldid=7867 25 26 27 p = [1 2 3 4 5]; 28 p = p/sum(p) 29 q = cumsum(p)/sum(p) 30 nb = 200; 31 32 33 rs = rand(nb,1); 34 chosen = sum(repmat(rs,1,length(q))>repmat(q,nb,1),2)+1 35 36 rep = sparse(chosen,1,1); 37 full(rep)'/sum(rep)*100 38 39
