Scilab Manual for Probability Theory and Random Processes by Prof Shital Thakkar Others Dharmsinh Desai University1 Solutions provided by Prof Shital Thakkar Others Dharmsinh Desai University July 12, 2017 1 Funded by a grant from the National Mission on Education through ICT, http://spoken-tutorial.org/NMEICT-Intro. This Scilab Manual and Scilab codes written in it can be downloaded from the ”Migrated Labs” section at the website http://scilab.in 1 Contents List of Scilab Solutions 3 1 Write a program to find probability of tossing a coin and rolling a die through large no. of experimentation. 5 2 To generate Uniform, Gaussian and Exponential distributed data for given mean and variance. 9 3 Write a program to generate M trials of a random experiment having specific number of outcomes with specified probabilities. 14 4 To find estimated and true mean of Uniform, Gaussian and Exponential distributed data. 17 5 To find density and distribution function of a function of random variable Y = 2X + 1. where X is gaussian R.V. 21 6 Estimate the mean and variance of Y = 2X + 1, where X is a gaussian random variable. 24 7 Plot Joint density and distribution function of sum of two Gaussian random variable (Z = X + Y). 26 8 Estimate the mean and variance of a R.V. Z = X+Y. Where X and Y are also random variables. 30 9 Simulation of Central Limit Theorem. 2 32 List of Experiments Solution 1.1Calculates probability of sum of tossing two dice . . . Solution 1.2Finds probability of getting Head when a coin is tossed Solution 2.1Generation of Uniform Data . . . . . . . . . . . . . . . Solution 2.2Gaussian Data Generation . . . . . . . . . . . . . . . . Solution 2.3Exponential Data Generation . . . . . . . . . . . . . . Solution 3.1Random experiment with outputs in specific range . . Solution 4.1Comaparison of True and estimated statics of Uniform Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . Solution 4.2Comaparison of True and estimated statics of Gaussian Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . Solution 4.3Comaparison of True and estimated statics of Exponential Data . . . . . . . . . . . . . . . . . . . . . . . . . Solution 5.1Density and Distribution plot generation for one function of Random Variable . . . . . . . . . . . . . . . . . Solution 6.1Statistics of Function of one random variable . . . . . Solution 7.1Joint Density and Distribution of Function of two random varible . . . . . . . . . . . . . . . . . . . . . . . . Solution 8.1Estimation of mean and variance of sum of two random variable . . . . . . . . . . . . . . . . . . . . . . . . . . Solution 9.1Summation of two random variable leads to Gaussian density function . . . . . . . . . . . . . . . . . . . . . 3 5 6 9 10 12 14 17 18 19 21 24 26 30 32 List of Figures 1.1 Finds probability of getting Head when a coin is tossed . . . 7 2.1 2.2 2.3 Generation of Uniform Data . . . . . . . . . . . . . . . . . . Gaussian Data Generation . . . . . . . . . . . . . . . . . . . Exponential Data Generation . . . . . . . . . . . . . . . . . 10 11 12 5.1 Density and Distribution plot generation for one function of Random Variable . . . . . . . . . . . . . . . . . . . . . . . . 22 7.1 7.2 9.1 Joint Density and Distribution of Function of two random varible . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Joint Density and Distribution of Function of two random varible . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summation of two random variable leads to Gaussian density function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 27 27 33 Experiment: 1 Write a program to find probability of tossing a coin and rolling a die through large no. of experimentation. Scilab code Solution 1.1 Calculates probability of sum of tossing two dice 1 2 3 4 5 6 7 8 9 10 // O p e r a t i n g System : Windows XP o r l a t e r , // S c i l a b : 5.3.3 // Program o f T o s s i n g two d i c e and o b s e r v i n g P r o b a b i l i t y o f sum o f t h e i r f r o n t f a c e . // f o r e . g . P r o b a b i l i t y o f sum o f two d i c e = 2 i s 1 / 3 6 a s t h e r e a r e 36 p o s s i b i l i t i e s and sum = 2 can // o c c u r o n l y one c o m b i n a t i o n t h a t i s b o t h face = 1 clc ; clear ; clf ; N = 10000; // Number o f t i m e s t o s s i n g o f d i e performed count = 0; // C o u n t e r f o r c o u n t i n g number o f t i m e s 5 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 sum o f d i e for i = 1: N y1 = ceil ( rand (1) *6) ; // o u t p u t o f d i e 1 y2 = ceil ( rand (1) *6) ; // o u t p u t o f d i e 2 if (( y1 + y2 ) == 3) // c h e c k f o r sum o f f r o n t f a c e o f b o t h d i e i s == 3 ( c h a n g e sum and ) count = count + 1; // i n c r e m e n t t h e c o u n t v a l u e when sum = 3 o c c u r s end prob1 ( i ) = count / i ; // no . o f t i m e s sum o f d i e = 3/ t o t a l no . t r i a l s end plot ( prob1 ) xlabel ( ’ Number o f T r i a l s ’ ) ; ylabel ( ’ P r o b a b i l i t y ’ ) ; title ( ’ P r o b a b i l i t y o f g e t t i n g sum o f d o t s on f a c e s o f a d i c e t o be 3 ’ ) ; // A s s i g n m e n t : Program can be c h e c k e d f o r o t h e r v a l u e s o f sum a t l i n e number 1 0 . Scilab code Solution 1.2 Finds probability of getting Head when a coin is tossed 1 2 3 4 5 // O p e r a t i n g System : Windows XP o r l a t e r , // S c i l a b : 5.3.3 // T h i s program f i n d p r o b a b i l i t y o f g e t t i n g Head when a coin i s tossed . 6 // P r o b a b i l i t y = 1/2 = 0 . 5 a s t h e r e a r e two p o s s i b l e outcomes in c o i n t o s s i n g experiment . 6 Figure 1.1: Finds probability of getting Head when a coin is tossed 7 8 9 10 11 12 13 14 15 16 17 18 19 20 clc ; clear all ; clf ; a1 = 1000; count1 = 0; for i = 1: a1 x = round ( rand (1) ) ; // round : t h e e l e m e n t s t o n e a r e s t i n t e g e r // r a n d : r e t u r n s a pseudorandom , s c a l a r v a l u e drawn from a u n i f o r m // d i s t r i b u t i o n on t h e u n i t i n t e r v a l . if ( x ==1) // HEAD− ’ 1 ’ , c o n d i t i o n t h a t d e t e c t s ’ HEAD’ comes o r n o t count1 = count1 + 1; // i n c r e m e n t t h e c o u n t v a l u e when head o c c u r s end p ( i ) = count1 / i ; // p r o b a b i l i t y o f head o c c u r i n g a t ith t r a i l 7 21 end 22 plot (1: a1 , p ) 23 // p l o t t h e p r o b . a t i t h trail ( plots discrete sequence ) xlabel ( ’ Number o f T r i a l s ’ ) ; ylabel ( ’ P r o b a b i l i t y ’ ) ; title ( ’ P r o b a b i l i t y o f g e t t i n g head ’ ) ; // A s s i g n m e n t : // 1 . perform above experiment with n = 100 ,1000 . // 2 . Extend t h e a b o v e e x p e r i m e n t t o f i n d p r o b a b i l i t y o f 3 heads in 4 coin t o s s e s . 30 // Match t h e r e s u l t t h e o r e t i c a l l y . 24 25 26 27 28 29 8 Experiment: 2 To generate Uniform, Gaussian and Exponential distributed data for given mean and variance. Scilab code Solution 2.1 Generation of Uniform Data //To g e n e r a t e Uniform , G a u s s i a n and E x p o n e n t i a l d i s t r i b u t e d data . 2 // O p e r a t i n g System : Windows XP o r l a t e r , 3 // S c i l a b : 5.3.3 4 //NOTE: EXECUTE ONE BY ONE SEGEMENT 1 5 6 // [ 1 ] Uniform Data G e n e r a t i o n 7 clc ; 8 clear all ; 9 // b = i n p u t ( ’ h i g h e r l i m i t o f u n i f o r m r . v . b = ’ ) // 10 Enter h i g h e r l i m i t o f uniform r . v . // a = i n p u t ( ’ l o w e r l i m i t o f u n i f o r m r . v . a = ’) // Enter lower l i m i t o f uniform r . v . 9 Figure 2.1: Generation of Uniform Data 11 12 13 14 15 16 17 18 clf () ; b =4; // h i g h e r r a n g e o f u n i f o r m d a t a a =2; // l o w e r r a n g e o f u n i f o r m d a t a x = a + (b - a ) .* rand (1 ,1000 , ’ u n i f o r m ’ ) ; // u n i f o r m d a t a g e n e r a t i o n o f l e n g t h 100 histplot (1000 , x ) // h i s t o g r a m o f g e n e r a t e d d a t a xlabel ( ’ Number o f S a m p l e s ’ ) ; ylabel ( ’ Range o f V a l u e s o f U n i f o r m l y g e n e r a t e d d a t a ’ ); title ( ’ U n i f o r m l y d i s t r i b u t e d d a t a i n t h e r a n g e o f a to b ’ ); Scilab code Solution 2.2 Gaussian Data Generation 10 Figure 2.2: Gaussian Data Generation //To g e n e r a t e Uniform , G a u s s i a n and E x p o n e n t i a l d i s t r i b u t e d data . 2 // O p e r a t i n g System : Windows XP o r l a t e r , 3 // S c i l a b : 5.3.3 1 4 5 6 7 8 9 10 11 12 13 14 15 16 // [ 2 ] E x p o e n e n t i a l d a t a g e n e r a t i o n & Mean and Variance Calcultaion of Exponential d i s t r i b u t e d data . clc ; clear all ; clf () ; // ( i ) E x p o n e n t i a l d a t a g e n e r a t i o n lambda = 2; // o r lambda = i n p u t ( ’ e n t e r lemda v a l u e f o r e x p o n e n t i a l r . v . ’ ) // lemda o f e x p o n e n t i a l d a t a ; X = grand (10000 ,1 , ” exp ” , 1/ lambda ) ; Xmax = max ( X ) ; histplot (40 , X , style =2) x = linspace (0 , max ( Xmax ) ,100) ’; plot2d (x , lambda * exp ( - lambda * x ) , strf = ” 000 ” , style =5) legend ([ ” e x p o n e n t i a l random s a m p l e h i s t o g r a m ” ” e x a c t 11 Figure 2.3: Exponential Data Generation d e n s i t y c u r v e ” ]) ; 17 xlabel ( ’ s a m p l e v a l u e ’ ) ; 18 ylabel ( ’ E x p o n e n t i a l o u t p u t v a l u e s ’ ) ; 19 title ( ’ E x p o n e n t i a l d i s t r i b u t e d d a t a ’ ) ; Scilab code Solution 2.3 Exponential Data Generation //To g e n e r a t e Uniform , G a u s s i a n and E x p o n e n t i a l d i s t r i b u t e d data . 2 // O p e r a t i n g System : Windows XP o r l a t e r , 3 // S c i l a b : 5.3.3 1 4 5 6 // [ 3 ] G a u s s i a n d a t a g e n e r a t i o n // G a u s s i a n d a t a g e n e r a t i o n 12 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 clc ; clear all ; clf () ; //m = i n p u t ( ’ e n t e r mean v a l u e f o r G a u s s i a n r . v . ’ ) // v a r i = i n p u t ( ’ e n t e r mean v a l u e f o r G a u s s i a n r . v . ’ ) m = 2; // mean v a l u e o f g a u s s i a n d a t a sd = 1; // s t a n d a r d d e v i a t i o n vari = sd ^2; X = grand (100000 ,1 , ” n o r ” , m , sd ) ; Xmax = max ( X ) ; clf () histplot (40 , X , style =2) x = linspace ( -10 , max ( Xmax ) ,100) ’; plot2d (x ,(1/( sqrt (2* %pi * vari ) ) ) * exp ( -0.5*( x - m ) .^2/ vari ) , strf = ” 000 ” , style =5) xlabel ( ’ s a m p l e v a l u e ’ ) ; ylabel ( ’ G a u s s i a n o u t p u t v a l u e s ’ ) ; title ( ’ G a u s s i a n d i s t r i b u t e d d a t a ’ ) ; legend ([ ” G a u s s i a n random s a m p l e h i s t o g r a m ” ” e x a c t d e n s i t y c u r v e ” ] ,2) ; 13 Experiment: 3 Write a program to generate M trials of a random experiment having specific number of outcomes with specified probabilities. Scilab code Solution 3.1 Random experiment with outputs in specific range 1 2 3 4 5 6 7 8 9 10 11 // O p e r a t i n g System : Windows XP o r l a t e r , // S c i l a b : 5.3.3 // W r i t e a program t o g e n e r a t e M t r i a l s o f a random experiment having s p e c i f i c // number o f o u t c o m e s w i t h s p e c i f i e d p r o b a b i l i t i e s . // h e r e No . o f t r i a l s = 1 0 0 0 , no . o u t c o m e s ( r v ) = 3 w i t h s p e c i e d p r o b a b i l i t y e n t e r e d by u s e r clc ; clear all ; rand ( ’ s e e d ’ ) // c h e c k M = 1000; // Number o f t r i a l s o f random e x p e r i m e n t outcomes = 3; // P o s s i b l e number o f o u t c o m e s o f 14 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 random e x p e r i m e n t for i = 1: outcomes -1 r ( i ) = input ( ’ e n t e r u p p e r r a n g e o f p r o b a b i l i t y o f r . v . ( v a l u e s i n t h e 0<r <1) : ’ ) // e n t e r v a l u e s i n t h e 0<r <1 if r ( i ) >1 then error ( ’ E n t e r v a l u e s i n t h e r a n g e 0<r <1 ’ ) end end x = zeros (M ,1) ; for i = 1: M u = rand (1 ,1) ; // random outcome if u <= r (1) then x (i ,1) =1; // a s s i g n r v v a l u e = 1 i f u<=r ( 1 ) elseif u > r (1) & u <= r (2) x (i ,1) =2; // s e c o n d r v v a l u e else x (i ,1) =3; // t h i r d r v v a l u e end end count1 =0; count2 =0; count3 =0; for i =1:1000 if x (i ,1) ==1 then count1 = count1 + 1; elseif x (i ,1) == 2 count2 = count2 + 1; else count3 = count3 + 1; end end estP1 = count1 / M ; disp ( estP1 ) // e s t i m a t e d p r o b a b i l i t y o f g e n e r a t e d random v a r i a b l e estP2 = count2 / M ; disp ( estP2 ) // e s t i m a t e d p r o b a b i l i t y o f g e n e r a t e d random v a r i a b l e estP3 = count3 / M ; disp ( estP3 ) // e s t i m a t e d p r o b a b i l i t y o f g e n e r a t e d random v a r i a b l e // A s s i g n m e n t : 15 // 1 . Extend t h i s program f o r 4 number o f random variable 45 // 2 . Extend t h i s program f o r more number o f t r i a l s . i . e . M = 5000 ,10000 etc . 44 16 Experiment: 4 To find estimated and true mean of Uniform, Gaussian and Exponential distributed data. Scilab code Solution 4.1 Comaparison of True and estimated statics of Uniform Data 1 2 3 4 5 6 7 8 9 10 11 12 13 // O p e r a t i n g System : Windows XP o r l a t e r , // S c i l a b : 5.3.3 // h e r e we g e n e r a t e u n i f o r m l y d i s t r i b u t e d d a t a compare i t s s t a t i s t i c s w i t h c a l c u l a t e d u s i n g equation . // [ 1 ] Mean and V a r i a n c e C a l c u l t a i o n o f U n i f o r m l y d i s t r i b u t e d data clc ; clear all ; b =4; // h i g h e r r a n g e o f u n i f o r m d a t a a =2; // l o w e r r a n g e o f u n i f o r m d a t a x = a + (b - a ) .* rand (1 ,1000) ; // Uniform d a t a g e n e r a t i o n 17 14 15 16 17 18 19 20 21 22 23 24 using function Uni_true_mean = mean ( x ) mprintf ( ’ Uniform True Mean = %f ’ , Uni_true_mean ) Uni_true_var = variance ( x ) mprintf ( ’ \n Uniform True Mean = %f ’ , Uni_true_var ) px = 1/( b - a ) // p d f c a l c u l t a i o n o f u n i f o r m r . v . m_uniform = integrate ( ’ x ∗ px ’ , ’ x ’ ,a , b ) // mean c a l c u l t a i o n of uniform r . v . fsq_uniform = integrate ( ’ ( x ˆ 2 ) ∗ px ’ , ’ x ’ ,a , b ) // mean square value of uniform r . v . var_uniform = fsq_uniform - ( m_uniform ) .^2 // v a r i a n c e of uniform r . v . mprintf ( ’ \n Uniform C a l c u l a t e d Mean = %f ’ , m_uniform ) mprintf ( ’ \n Uniform C a l c u l a t e d V a r i a n c e = %f ’ , var_uniform ) Scilab code Solution 4.2 Comaparison of True and estimated statics of Gaussian Data 1 2 3 4 5 6 7 8 9 10 11 12 13 // O p e r a t i n g System : Windows XP o r l a t e r , // S c i l a b : 5.3.3 // h e r e we g e n e r a t e G a u s s i a n d i s t r i b u t e d d a t a compare i t s s t a t i s t i c s with c a l c u l a t e d using equation . // [ 3 ] Mean & V a r i a n c e c a l c u l a t i o n o f G a u s s i a n Data clc ; clear all ; m = 2; // mean v a l u e o f g a u s s i a n d a t a sd = 1; // s t a n d a r d d e v i a t i o n vari = sd ^2; X = grand (10000 ,1 , ” n o r ” , m , sd ) ; // g a u s s i a n d a t a generation using function 18 14 15 16 17 18 19 20 21 22 23 24 Gaus_true_mean = mean ( X ) mprintf ( ’ G a u s s i a n True Mean = %f ’ , Gaus_true_mean ) Gaus_true_var = variance ( X ) mprintf ( ’ \n G a u s s i a n True V a r i a n c e = %f ’ , Gaus_true_var ) // Mean and v a r i a n c e c a l c u l t a i o n u s i n g f o r m u l a gs_mean = integrate ( ’ x ∗ ( 1 / s q r t ( 2 ∗ %pi ∗ v a r i ) ∗ exp ( −(x−m) ˆ 2 / ( 2 ∗ v a r i ) ) ) ’ , ’ x ’ ,0 ,100) gs_fsq = integrate ( ’ ( x ˆ 2 ) ∗ ( 1 / s q r t ( 2 ∗ %pi ∗ v a r i ) ∗ exp ( −(x− m) ˆ 2 / ( 2 ∗ v a r i ) ) ) ’ , ’ x ’ ,0 ,100) ; gs_var = gs_fsq - gs_mean .^2; mprintf ( ’ \n G a u s s i a n C a l c u l a t e d Mean = %f ’ , gs_mean ) mprintf ( ’ \n G a u s s i a n C a l c u l a t e d V a r i a n c e = %f ’ , gs_var ) Scilab code Solution 4.3 Comaparison of True and estimated statics of Exponential Data 1 2 3 4 5 6 7 8 9 10 // O p e r a t i n g System : Windows XP o r l a t e r , // S c i l a b : 5.3.3 // h e r e we g e n e r a t e E x p o n e n t i a l l y d i s t r i b u t e d d a t a compare i t s s t a t i s t i c s w i t h c a l c u l a t e d u s i n g equation . // [ 2 ] Mean and V a r i a n c e C a l c u l a t i o n o f E x p o n e n t i a l data clc ; clear all ; lambda = 2; // o r lambda = i n p u t ( ’ e n t e r lemda v a l u e f o r e x p o n e n t i a l r . v . ’ ) // lemda o f e x p o n e n t i a l d a t a ; X = grand (1000 ,1 , ” exp ” , 1/ lambda ) ; // E x p o n e n t i a l d a t a generation using function 19 11 12 13 14 15 16 17 18 19 20 21 Expo_true_mean = mean ( X ) mprintf ( ’ E x p o n e n t i a l True Mean = %f ’ , Expo_true_mean ) Expo_true_var = variance ( X ) mprintf ( ’ \n E x p o n e n t i a l True V a r i a n c e = %f ’ , Expo_true_var ) // Mean and v a r i a n c e c a l c u l t a i o n u s i n g f o r m u l a ex_mean = integrate ( ’ x ∗ ( lambda ∗ exp (−lambda ∗ x ) ) ’ , ’ x ’ ,0 ,100) ex_fsq = integrate ( ’ ( x ˆ 2 ) ∗ ( lambda ∗ exp (−lambda ∗ x ) ) ’ , ’ x ’ ,0 ,100) ; ex_var = ex_fsq - ex_mean .^2 mprintf ( ’ \n E x p o n e n t i a l C a l c u l a t e d Mean = %f ’ , ex_mean ) mprintf ( ’ \n E x p o n e n t i a l C a l c u l a t e d V a r i a n c e = %f ’ , ex_var ) 20 Experiment: 5 To find density and distribution function of a function of random variable Y = 2X + 1. where X is gaussian R.V. Scilab code Solution 5.1 Density and Distribution plot generation for one function of Random Variable 1 2 3 4 // O p e r a t i n g System : Windows XP o r l a t e r , // S c i l a b : 5.3.3 //To f i n d d e n s i t y ( p d f ) and d i s t r i b u t i o n ( c d f ) f u n c t i o n o f a f u n c t i o n o f random v a r i a b l e 5 //Y = 2X + 1 ( h a v i n g form Y = aX + b ) . where X i s g a us s i an R.V. 6 7 clc ; 8 clear all ; 9 clf () ; 10 mean_x = 1; // mean v a l u e o f 21 g a u s s i a n data Figure 5.1: Density and Distribution plot generation for one function of Random Variable 11 sd_x = 1; // s t a n d a r d d e v i a t i o n 12 vari_x = sd_x .^2; 13 lgd = []; 14 //PDF and CDF o f G a u s s i a n Random V a r i a b l e X 15 x = linspace ( -10 ,10 ,100) ; 16 plot2d (x ,((1/( sqrt (2* %pi * vari_x ) ) ) * exp ( -0.5*( x - mean_x ) .^2/ vari_x ) ) ,2) ; // p l o t s p d f o f X 17 set ( gca () ,” a u t o c l e a r ” ,” o f f ” ) 18 plot2d (x , cdfnor ( ”PQ” ,x , mean_x * ones ( x ) , sd_x * ones ( x ) ) ,3) ; // c d f o f g a u s s i a n RV X 19 set ( gca () ,” a u t o c l e a r ” ,” on ” ) 20 xlabel ( ’ s a m p l e p o i n t s ’ ) ; 21 ylabel ( ’PDF & CDF ’ ) ; 22 // t i t l e ( ’ d e n s i t y and d i s t r i b u t i o n 23 function for Gaussian function ’ ) ; legend ([ ’PDF o f n o r m a l d i s t r i b u t i o n ’ ; ’CDF o f n o r m a l d i s t r i b u t i o n ’ ] ,2) ; 24 22 25 //PDF and CDF o f Y = aX + b where a = 2 , b = 1 26 a = 2; 27 b = 1; 28 y = a * x + b ; // F u n c t i o n o f One Random V a r i a b l e 29 mean_y = a * mean_x + b ; 30 vari_y =( a * sd_x ) .^2; 31 figure (2 , ” B a c k g r o u n d C o l o r ” ,[1 ,1 ,1]) ; 32 plot2d (y ,((1/( sqrt (2* %pi * vari_y * a .^2) ) ) * exp ( -0.5*( y - mean_y ) .^2/ vari_y ) ) ,2) ; // p d f o f Y 33 set ( gca () ,” a u t o c l e a r ” ,” o f f ” ) 34 plot2d (x , cdfnor ( ”PQ” ,y ,( a * mean_x + b ) * ones ( x ) ,( a * sd_x ) * ones ( x ) ) ,3) ; // c d f o f y 35 set ( gca () ,” a u t o c l e a r ” ,” on ” ) 36 xlabel ( ’ s a m p l e p o i n t s ’ ) ; 37 ylabel ( ’PDF & CDF o f Y = 2X + 1 ’ ) ; 38 legend ([ ’PDF o f Y = 2X + 1 ’ ; ’CDF o f Y = 2X + 1 ’ ] ,2) ; 39 40 41 // A s s i g n m e n t : 42 // 1 . P e r f o r m t h e o p e r a t i o n f o r f u n c t i o n Y = 5X + 1 . 43 // 2 . G e n e r a t e p d f and c d f o f n o n l i n e a r f u n c t i o n b e t w e e n Y and X . 23 Experiment: 6 Estimate the mean and variance of Y = 2X + 1, where X is a gaussian random variable. Scilab code Solution 6.1 Statistics of Function of one random variable 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // O p e r a t i n g System : Windows XP o r l a t e r , // S c i l a b : 5.3.3 // True and E s t i m a t e d v a l u e o f mean and v a r i a n c e o f f u n c t i o n o f one random // v a r i a b l e h a v i n g form o f Y = aX +b . clc ; clear all ; rand ( ’ s e e d ’ , getdate ( ’ s ’ ) ) m = 0; // mean o f random v a r i a b l e x vari = 1; // v a r i a n c e o f random v a r i a b l e x m_est = 0; var_est = 0; for i = 1:1000 y (i ,1) = 1 +2* rand (1 ,1 , ” n o r m a l ” ) ; // Y = 2X + 1 24 15 where x i s g a u s s i a n d a t a m_est = m_est + ((1/1000) * y ( i ) ) ; // e s t i m a t i o n by averaging var_est = var_est +((1/1000) *( y ( i ) - m_est ) ^2) ; 16 17 end 18 printf ( ’ E s t i m a t e d mean o f Y(=2X + 1 ) 19 20 21 22 23 24 25 26 27 28 29 i s : Est mean=%f ’ , m_est ) printf ( ’ \n E s t i m a t e d v a r i a n c e o f Y) i s : E s t v a r i a n c e =%f ’ , var_est ) // C a l c u l a t i o n o f t r u e mean o f Y y_mean = integrate ( ’ ( 2 ∗ x +1) ∗ ( 1 / s q r t ( 2 ∗ %pi ∗ v a r i ) ∗ exp ( −( x−m) ˆ 2 / ( 2 ∗ v a r i ) ) ) ’ , ’ x ’ , -100 ,100) ; printf ( ’ \n True mean o f Y(=2X + 1 ) i s : True mean=%f ’ , y_mean ) // C a l c u l a t i o n o f t r u e v a r i a n c e o f Y // f o r a f u n c t i o n l i k e Y = aX + b t h e v a r i a n c e o f Y i s a ˆ2∗ V a r i a n c e o f X . gs_mean = integrate ( ’ x ∗ ( 1 / s q r t ( 2 ∗ %pi ∗ v a r i ) ∗ exp ( −(x−m) ˆ 2 / ( 2 ∗ v a r i ) ) ) ’ , ’ x ’ , -50 ,100) ; gs_fsq = integrate ( ’ ( ( x ˆ 2 ) ∗ ( 1 / s q r t ( 2 ∗ %pi ∗ v a r i ) ∗ exp ( −(x −m) ˆ 2 / ( 2 ∗ v a r i ) ) ) ) ’ , ’ x ’ , -50 ,100) ; gs_var = gs_fsq - ( gs_mean ) .^2; var_y = 2^2* gs_var ; // h e r e a = 2 printf ( ’ \n True v a r i a n c e o f Y(=2X + 1 ) i s : T r u e v a r i a n c e=%f ’ , var_y ) 30 31 // E x p e c t a t i o n o f Y i s E(Y)=E( 2X+1)=2E(X) +1. That ’ s why a n s w e r i s 1 . 32 // True v a r i a n c e o f Y i n t h i s f o r m a t i s e q u a l t o a ˆ2∗ v a r i a n c e o f X. 33 // A s s i g n m e n t : 34 // 1 . Assume X i s u n i f o r m random v a r i a b l e b e t w e e n a t o b . f i n d mean and v a r i a n c e . 25 Experiment: 7 Plot Joint density and distribution function of sum of two Gaussian random variable (Z = X + Y). Scilab code Solution 7.1 Joint Density and Distribution of Function of two random varible 1 2 3 4 5 // O p e r a t i n g System : Windows XP o r l a t e r , // S c i l a b : 5.3.3 // P l o t J o i n t d e n s i t y and d i s t r i b u t i o n f u n c t i o n o f sum o f two G a u s s i a n random v a r i a b l e ( Z = X + Y) . 6 clc ; 7 clear all ; 8 clf () ; 9 26 Figure 7.1: Joint Density and Distribution of Function of two random varible Figure 7.2: Joint Density and Distribution of Function of two random varible 27 10 //PDF o f G a u s s i a n Random V a r i a b l e X 11 mean_x = 0; // mean o f f i r s t g a u s s i a n RV 12 sd_x = 1; // s t a n d a r d d e v i a t i o n o f f i r s t g a u s s s i a n RV 13 vari_x = sd_x .^2; 14 15 x = linspace ( -10 ,10 ,50) ; // g e n e r a t i n g l i n e a r l y s p a c e d d a t a a s Random o u t p u t 16 X = ((1/( sqrt (2* %pi * vari_x ) ) ) * exp ( -0.5.*( x - mean_x ) .^2/ vari_x ) ) ; // f i n d i n g g a u s s i a n p d f o f a b o v e d a t a 17 subplot (2 ,1 ,1) ; 18 plot (x ,X , ’ r ’ ) 19 xlabel ( ’ s a m p l e p o i n t s ’ ) ; 20 ylabel ( ’PDF o f X ’ ) ; 21 title ( ’PDF o f one Random V a r i a b l e ’ ) 22 23 24 //PDF o f G a u s s i a n Random V a r i a b l e Y 25 mean_y = 0; 26 sd_y = 1; 27 vari_y = sd_y .^2; 28 29 y = linspace ( -10 ,10 ,50) ; 30 Y = ((1/( sqrt (2* %pi * vari_y ) ) ) * exp ( -0.5.*( y - mean_y ) 31 32 33 34 35 36 37 38 .^2/ vari_y ) ) ; subplot (2 ,1 ,2) ; plot (y ,Y , ’ b ’ ) xlabel ( ’ s a m p l e p o i n t s ’ ) ; ylabel ( ’PDF o f Y ’ ) ; title ( ’PDF o f s e c o n d Random V a r i a b l e ’ ) // J o i n t p d f o f sum o f random v a r i a b l e X & Y // When two IID random v a r i a b l e a r e summen up , t h e i r J o i n t PDF i s c o n v o l u t i o n b e t w e e n i n d i v i d u a l p d f s o f Random v a r i a b l e s 39 z = convol (X , Y ) ; 40 figure (2 , ” B a c k g r o u n d C o l o r ” ,[1 ,1 ,1]) ; 41 subplot (2 ,1 ,1) ; plot ( linspace ( -20 ,20 ,99) ,z ) // J o i n t PDF 28 42 xlabel ( ’ s a m p l e p o i n t s ’ ) ; 43 ylabel ( ’PDF o f Z ’ ) ; 44 title ( ’ J o i n t d e n s i t y f u n c t i o n o f Z = X + Y ’ ) 45 P = cdfnor ( ”PQ” , linspace ( -20 ,20 ,99) , mean ( z ) * ones ( z ) , 46 47 48 49 50 51 52 53 sqrt ( variance ( z ) ) * ones ( z ) ) ; set ( gca () ,” a u t o c l e a r ” ,” o f f ” ) subplot (2 ,1 ,2) ; plot2d (1: length ( P ) ,P ,3) ; xlabel ( ’ s a m p l e p o i n t s ’ ) ; ylabel ( ’ D i s t r i b u t i o n o f Z ’ ) ; title ( ’ J o i n t d i s t r i b u t i o n f u n c t i o n o f Z = X + Y ’ ) set ( gca () ,” a u t o c l e a r ” ,” on ” ) // A s s i g n m e n t : // Change mean and v a r i a n c e o f i n d i v i d u a l random v a r i a b l e X and Y and o b s e r v e o u t p u t 29 Experiment: 8 Estimate the mean and variance of a R.V. Z = X+Y. Where X and Y are also random variables. Scilab code Solution 8.1 Estimation of mean and variance of sum of two random variable 1 2 3 4 // O p e r a t i n g System : Windows XP o r l a t e r , // S c i l a b : 5.3.3 // Concept : E s t i m a t i o n o f mean and v a r i a n c e o f sum o f two random v a r i a b l e Z = X + Y, where X and Y a r e random v a r i a b l e . 5 // Above c o n c e p t i s e x p l a i n e d w i t h e x a m p l e a s follows . 6 // Example : A l a r g e c i r c u l a r d a r t b o a r d i s s e t up w i t h a ” b u l l s e y e ” a t t h e c e n t e r o f t h e c i r c l e , which i s a t t h e c o o r d i n a t e ( 0 , 0 ) . A d a r t i s thrown a t t h e c e n t e r but l a n d s a t (X, Y) a r e two d i f f e r e n t G a u s s i a n random v a r i a b l e s . What i s a v e r a g e d i s t a n c e o f t h e d a r t from t h e b u l l s e y e ? What i s 30 v a r i a n c e o f data ? 7 // D i s t a n c e from c e n t e r i s g i v e n a s s q r t (Xˆ2+Yˆ 2 ) 8 9 clc ; 10 clear all ; 11 rand ( ’ s e e d ’ ,0) // s e t t i n g s e e d o f random g e n e r a t o r t o zero 12 m_est = 0; 13 for i = 1:1000 14 R (i ,1) = sqrt ( rand (1 ,1 , ’ n o r m a l ’ ) ^2+ rand (1 ,1 , ’ 15 n o r m a l ’ ) ^2) ; // c a l c u l a t i o n o f d i s t a n c e from center m_est = m_est +(1/1000) * R ( i ) ; // e s t i m a t i o n o f mean from d a t a 16 end 17 m_est 18 mprintf ( ’ Mean o f Sum o f Two Random v a r i a b l e that i s Mean o f Z = %f ’ , m_est ) 19 v_est = variance ( R ) // v a r i a n c e c a l c u l a t i o n 20 mprintf ( ’ \n V a r i a n c e o f Sum o f Two Random v a r i a b l e t h a t i s Mean o f Z = %f ’ , v_est ) 31 Experiment: 9 Simulation of Central Limit Theorem. Scilab code Solution 9.1 Summation of two random variable leads to Gaussian density function 1 2 3 4 5 6 // O p e r a t i n g System : Windows XP o r l a t e r , // S c i l a b : 5.3.3 // S i m u l a t i o n o f C e n t r a l L i m i t Theorem . // ( Which s a y s t h a t i f we k e e p on a d d i n g i n d e p e n d a n t Random V a r i a b l e s t h e n i t d e n s i t y f u n c t i o n 7 // a p p r o c h e s t o g a u s s i a n d i s t r i b u t i o n ) 8 // h e r e two u n i f o r m RVs a r e added . 9 10 11 12 13 14 15 clc ; clear all ; clf () ; n = 0:0.01:1; x = zeros ( length ( n ) ,1) ; i = 1:50; // l e n g t h o f Uniform r v 1 32 Figure 9.1: Summation of two random variable leads to Gaussian density function 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 x ( i ) = 2; subplot (3 ,3 ,1) ; plot (n ,x , ’ r−d ’ ) xlabel ( ’ p d f ’ ) ; ylabel ( ’ p d f o f r v 1 ’ ) title ( ’ p d f o f r v 1 ’ ) ; y = zeros ( length ( n ) ,1) ; j = 1:50; y ( j ) = 1*2; // l e n g t h o f Uniform r v 2 subplot (3 ,3 ,2) ; plot (n ,y , ’ bo −. ’ ) xlabel ( ’ p d f ’ ) ; ylabel ( ’ p d f o f r v 2 ’ ) title ( ’ p d f o f r v 2 ’ ) ; z1 = convol (x , y ) ; subplot (3 ,3 ,3) //When two i n d e p e n d e n t RVs a r e added t h e i r j o i n t 33 density i s convolution of marginal density plot ( z1 ) xlabel ( ’ r v v a l u e ’ ) ; ylabel ( ’ p d f ’ ) title ( ’ j o i n t p d f o f r v 1 and r v 2 ’ ) ; 34 35 36 37 38 39 for i = 4:9 // a d d i n g r v 9 t i m e s 40 subplot (3 ,3 , i ) 41 z1 = convol ( z1 , y ) ; 42 plot ( z1 ) 43 xlabel ( ’ r v v a l u e ’ ) ; 44 ylabel ( ’ p d f ’ ) 45 title ( ’ j o i n t p d f ’ ) ; 46 end 34
© Copyright 2026 Paperzz