% This is a transcript of the in-class demo 4-7-97, using the diary command. % All comments (such as this) have been inserted after class. % Some inputs and outputs have been removed or slightly modified % to correct my mistakes. % Open this file with a text editor and cut and paste any commands % you want to try out into the MATLAB command window. % Start by looking at some of the functions in the library for % generating PDFs and PMFs help dfuni dfuni(rangelow, rangehigh, samples) Generates a uniform pdf over the given range. samples sets the size of the numerical representation, and is optional. The default sample size is 1000. funi = dfuni(0,1); plotf(funi) help dfnorm dfnorm(u, sigsq, samples) Generates a Gaussian pdf with mean u and variance sigma squared (sigsq). samples sets the size of the numerical representation, and is optional. The default sample size is 1000. plotf( dfnorm(0,1) ) help mfbino mfbino(n, p) generates a binomial pmf with parameters n, p mb = mfbino(10, .3); plotf(mb)mb % Now look at the matrices that are produced by the above functions % Note the first row in each matrix contains identifiers that are % described in the helpfile.txt mb = 1.0000 1.0000 0 0.0282 1.0000 0.1211 2.0000 0.2335 3.0000 0.2668 4.0000 0.2001 5.0000 0.1029 6.0000 0.0368 7.0000 0.0090 8.0000 0.0014 9.0000 0.0001 10.0000 0.0000 dfuni(0,1,10) ans = 0 1.0000 0 1.0000 0.1000 1.0000 0.2000 1.0000 0.3000 1.0000 0.4000 1.0000 0.5000 1.0000 0.6000 1.0000 0.7000 1.0000 0.8000 1.0000 0.9000 1.0000 1.0000 1.0000 % The following example shows how to create one of these matrices for the % density f(x) = 3x^2 between 0 and 1, and o elsewhere x = [0: .001: 1]; f = 3 * x .^2; fnew = [ 0 1; x' f' ]; plotf(fnew) % Generating CDFs Funi = getcf( funi ); plotf(Funi) Fnew = getcf(fnew); plotf(Fnew) % Calculating mean and variance mmean(funi) ans = 0.5000 mmean(fnew) ans = 0.7500 var(funi) ans = 0.0833 % Convolving PDFs % Find PDF of sum of 2 independent uniform(0,1) random variables sum2 = convpf(funi, funi); plotf(sum2) % resize cuts out 0 part of matrix resulting from convolution sum2 = resize(sum2,0,2); plotf(sum2) % Now sum 3 sum3 = convpf( sum2, funi); plotf(sum3) sum3 = resize( sum3, 0,3); plotf(sum3) % Here's how to put more than one curve on the same plot plotf(sum2) hold on plotf(sum3, 'm') help plot PLOT Plot vectors or matrices. PLOT(X,Y) plots vector X versus vector Y. If X or Y is a matrix, then the vector is plotted versus the rows or columns of the matrix, whichever line up. PLOT(Y) plots the columns of Y versus their index. If Y is complex, PLOT(Y) is equivalent to PLOT(real(Y),imag(Y)). In all other uses of PLOT, the imaginary part is ignored. Various line types, plot symbols and colors may be obtained with PLOT(X,Y,S) where S is a 1, 2 or 3 character string made from the following characters: y yellow . point m magenta o circle c cyan x x-mark r red + plus g green - solid b blue * star w white : dotted k black -. dashdot -- dashed For example, PLOT(X,Y,'c+') plots a cyan plus at each data point. PLOT(X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...) combines the plots defined by the (X,Y,S) triples, where the X's and Y's are vectors or matrices and the S's are strings. For example, PLOT(X,Y,'y-',X,Y,'go') plots the data twice, with a solid yellow line interpolating green circles at the data points. The PLOT command, if no color is specified, makes automatic use of the colors specified by the axes ColorOrder property. The default ColorOrder is listed in the table above for color systems where the default is yellow for one line, and for multiple lines, to cycle through the first six colors in the table. For monochrome systems, PLOT cycles over the axes LineStyleOrder property. PLOT returns a column vector of handles to LINE objects, one handle per line. The X,Y pairs, or X,Y,S triples, can be followed by parameter/value pairs to specify additional properties of the lines. See also SEMILOGX, SEMILOGY, LOGLOG, GRID, CLF, CLC, TITLE, XLABEL, YLABEL, AXIS, AXES, HOLD, and SUBPLOT. % Now add the sum of 4 independent U(0,1) random variables sum4 = convpf( sum3, funi); sum4 = sum3; plotf(sum4, 'g:') % Use these to label your plots. These are standard MATLAB functions. title('This is 1,2,3 and 4 uniforms') xlabel('x') ylabel('y') gtext('Look here') % Simulation is another way to look at random variables % rvgen is a function in the probability libarary that "draws" or % samples from any PDF you have defined. Actually it uses a CDF, % but it will take either as input. help rvgen rvgen(f, M, N, seed) Generates a matrix of N samples of M experiments from a given probability or cumulative function. Each row of the matrix represent an experiment, and each column represents a sample. The seed sets the random number generator seed. So the resulting matrix is M by N. Trials may be duplicated by using the same seed. rvgen would rather have a cumulative function. suni = rvgen(funi, 4, 1000, 123); figure % This opens a new figure. To switch back to Fig 1, type figure(1) % See how uniform random numbers fill the unit square when you plot % one row as the x-coordinate and another row as the y-coordinate. plot( suni(1,:), suni(2,:) , '*' ) % Create a new simulation matrix which is the sum of two U(0,1) simulations sim2 = [ suni(1,:) + suni(2,:); suni(3,:) + suni(4,:)]; % These simulations can all be plotted with the MATLAB function hist, or % if you know the PDF that the simulation is from (or should be from) % you can use the library function histo to plot the histogram and the % PDF simultaneously. hist(sim2(1,:)) histo(sim2, sum2, 20, 'g', 'b:') % we can use sum2 as the PDF, since we figured that out above with convpf % Finally, a magic trick. Take the cube root of each number in the U(0,1) % simulated matrix. Then plot its histogram and see that it has the % same PDF as our "new" random variable, i.e. f(x) = 3x^2 for x in [0,1]. % You should learn in class soon why this works. % Hint: what is Fnew, and how does it relate to the cube root? simnew = suni .^(1/3); histo(simnew, fnew, 20, 'g', 'm:') 8436197 flops.