Download code

Jump to: navigation, search

Back to Probability_distributions_(MATLAB)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

script_gbimodal.m

 1 % The authors of this work have released all rights to it and placed it
 2 % in the public domain under the Creative Commons CC0 1.0 waiver
 3 % (http://creativecommons.org/publicdomain/zero/1.0/).
 4 % 
 5 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 % 
13 % Retrieved from: http://en.literateprograms.org/Probability_distributions_(MATLAB)?oldid=7867
14 
15 
16 q = [.4 .6];
17 m = [0 50];
18 s = [5 5];
19 distrib = struct('mu', m, 'sigma', s, 'weight', q)
20 nb = 200;
21 
22 %% Construction of two scaled and translated gaussians
23 X       = randn(nb,length(q)).*repmat(s,nb,1)+repmat(m,nb,1);
24 
25 %% Selection of one gaussian or the other
26 rsel    = rand(nb,1);
27 idx1    = (repmat(rsel,1,length(q))>repmat(cumsum(q),nb,1));
28 idx2    = (repmat(rsel,1,length(q))<repmat(cumsum(q),nb,1));
29 idx1(:,2:end) = idx1(:,1:end-1).*idx2(:,2:end);
30 idx1(:,1) = idx2(:,1);
31 X       = sum(X.*idx1,2);
32 
33 %% Plot result
34 [y,x]=ksdensity(X);figure;plot(x,y,'linewidth',2)


hijacker
hijacker
hijacker
hijacker

sript_multinomial.m

 1 % The authors of this work have released all rights to it and placed it
 2 % in the public domain under the Creative Commons CC0 1.0 waiver
 3 % (http://creativecommons.org/publicdomain/zero/1.0/).
 4 % 
 5 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 % 
13 % Retrieved from: http://en.literateprograms.org/Probability_distributions_(MATLAB)?oldid=7867
14 
15 
16 p = [1 2 3 4 5];
17 p = p/sum(p)
18 q = cumsum(p)/sum(p)
19 nb = 200;
20 
21 
22 rs     = rand(nb,1);
23 chosen = sum(repmat(rs,1,length(q))>repmat(q,nb,1),2)+1
24 
25 rep = sparse(chosen,1,1);
26 full(rep)'/sum(rep)*100


hijacker
hijacker
hijacker
hijacker