Second Laboratory Report. Probability and Random Variables.
Second Laboratory Report. Probability and Random Variables.
Second Laboratory Report. Probability and Random Variables.
Table of contents
Second Laboratory Report. Probability and random variables..........................................................1
Exercise 1...............................................................................................................................................3
Exercise 2.............................................................................................................................................10
Exercise 3.............................................................................................................................................13
Exercise 4.............................................................................................................................................14
Annex..................................................................................................................................................18
Annex 1. Values of y....................................................................................................................18
Annex 2. Values of X. ..................................................................................................................22
3
Exercise 1.
1.1.
>> n=1000
n=
1000
pa =
0.8000
>> v=rand(n,1)
v=
0.8147
0.9058
0.1270
0.9134
0.6324
0.0975
0.2785
0.5469
0.9575
0.9649
0.1576
0.9706
0.9572
0.4854
4
0.8003
0.1419
0.4218…
>> p2 = 0.85
p2 =
0.8500
>> v2 = rand(n,1)
v2 =
0.6312
0.3551
0.9970
0.2242
0.6525
0.6050
0.3872
0.1422
0.0251
0.4211
0.1841…
>> p3 =0.75
p3 =
0.7500
v3 =
0.7449
0.8923
0.2426
0.1296
0.2251
0.3500
0.2871
0.9275
0.0513
0.5927
0.1629
0.8384
5
0.1676
0.5022
0.9993
0.3554
0.0471
0.2137
0.3978
0.3337
0.2296
0.9361
0.6832
0.9621
0.4380
0.9403
0.0058
0.6103…
>> c3 = 1*(v3 <= p3)+ 0*(v3 > p3);
>> p4=0.75
p4 =
0.7500
v4 =
0.3796
0.3191
0.9861
0.7182
0.4132
0.0986
0.7346
0.6373
0.0738
0.1205
0.9816
0.4968
0.0224
0.0538
0.1409
0.8935…
answer =
0.6550
6
1.2.
p5 =
0.7500
v5 =
0.4271
0.9554
0.7242
0.5809
0.5403
0.7054
0.0050
0.7825
0.9269
0.0083
0.8246
0.7673
0.9971
0.2277
0.9195…
p6 =
0.7500
v6 =
0.2323
0.7398
0.8890
0.8598
0.5971
0.6548
0.9150
0.4332
7
0.2898
0.6319
0.2954
0.6220
0.0475
0.9946…
answer2 =
0.6990
>> Increase=((answer2-answer)/answer)*100
Increase =
6.7176
1.3.
p1n =
0.9000
vn =
0.7708
0.6858
0.8677
0.5805
0.6261
0.1111
0.2137
0.0364
0.4445
0.3266…
8
p2n =
0.9500
v2n =
0.6791
0.4855
0.0347
0.1144
0.0766
0.6078
0.2335
0.1420
0.5302
0.9303
0.6169
0.6899
0.2344
0.2068
0.0374
0.6634
0.5073…
answer3 =
0.8040
>> Increase2=((answer3-answer)/answer)*100
Increase2 =
22.7481
>> p3n=0.95
p3n =
0.9500
v3n =
0.8829
0.5119
0.3762
0.8838
0.2458
0.1640
0.6586
0.2556
0.1576
0.2766
0.2987
0.0059
0.2287
0.6701
0.9817
0.2561
0.6192…
>> c3n = 1*(v3n <= p3n)+ 0*(v3n > p3n);
p4n =
0.9500
v4n =
0.1538
0.9618
0.8763
0.4886
0.4071
0.1266
0.9254
0.0056
0.1864
0.3241
0.0502
0.1445
0.7294
0.4823
0.3381
0.2368…
answer4 =
10
0.6980
>> Increase3=((answer4-answer)/answer)*100
Increase3 =
6.5649
Exercise 2.
2.1.
averageX1 =
2.5360
>> varX1=var(X1)
varX1 =
1.8025
>> v2 = rand(n, 1);
X2 = 1*(v2<=0.3) + 2*(v2>0.3 & v2<=0.5) + 3*(v2>0.5 & v2<=0.7) + 4*(v2>0.7 & v2<=0.9) +
5*(v2> 0.9);
>> averageX2= mean(X2)
averageX2 =
2.6050
>> varX2=var(X2)
varX2 =
1.8048
2.2.
x1 = tabulate(X1);
x2 = tabulate(X2);
>>bar(x1(:,3))
Figure 1. Frequency table for X1
>>bar(x2(:,3))
Figure 2. Frequency table for X2.
>> x1
x1 =
12
x2 =
>> tabulate(X1);
Value Count Percent
1 317 31.70%
2 195 19.50%
3 212 21.20%
4 187 18.70%
5 89 8.90%
>> tabulate(X2);
Value Count Percent
1 273 27.30%
2 209 20.90%
3 210 21.00%
4 206 20.60%
5 102 10.20%
2.3.
>> a=(2*X1==X2)
a=
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
13
0
0
0
0
0
0
1
0
0
0
1
…
>> mean(a)
ans =
0.0910
Exercise 3.
3.1
{
x x< 0
{
2
x 0 x <0
0≤ x≤1
Fx (x )= 2 f x ( x )=F x (x) f x ( x )= x 0 ≤ x ≤ 1
,
2
( 2−x ) ⇒ 2− x 1< x ≤ 2
1− 1< x ≤2 0 x> 2
2
1 x >2
3.2.
- Generate n data u U ( 0 ,1 )
Following these instructions, we can find the function that follows x f ( x ) which
generates random numbers from the random variable using the inverse transformation
method.
3.3.
14
A way to write an Octave code that generates input numbers for the function F(x), is
the one shown in figure 3.
3.4.
To calculate analytically the mean and the variance, we will use the MATLAB
commands mean(x) and var (x)
Figure 4. Mean.
Figure 5. Variance.
To check if the simulation in section 3.3. of this exercise, we can always calculate
each output for each input.
Exercise 4.
4.1.
We have the density function, so we will integrate it to have the distribution function:
1
To see the output, go to annex 1.
15
{
β
β−1 (−α x )
f ( x )= αβ x e x >0 integrating∫ αβ x β−1 e(−α x ) dx=−e−α x +Cβ β
0 x≤0 ⇒
The exercise doesn’t have any initial condition, for us to be able to calculate the
constant C, so we will assume that C=0.
F ( x )=−e−α x
To be able to generate the code, we will use the inverse transformation method to
generate random numbers from the random variable X.
4.2.
To check if the simulation is correct, I will compare the values that MATLAB gives me
with the short-cute “mean(x)” and “var(x)” with the formulas. The code we are going to use to
simulate the formulas in the next one:
2
To see the values of the ouput, go to the annex 2.
16
When we ask MATLAB for the values of “m_formula”, “m_teorica”, “v_formula” and
“v_teorica”.
It is evident that the mean formula yields negative values, which is incorrect given
that the distribution function involves negative exponents, implying that all values of the
distribution function are negative. Conversely, the formula for variance produces results
almost identical to the MATLAB shortcut, thereby demonstrating its precision and accuracy.
18
Annex.
Annex 1. Values of y.
y=
0.8939
2.8490
1.4114
2.0852
0.9069
2.3012
1.2512
2.4519
2.5525
2.7195
1.8503
0.5771
1.1347
3.1782
0.8568
2.9368
2.1143
3.4037
0.5518
1.8263
0.6752
3.3108
0.1055
19
2.7947
2.9131
3.0555
0.5798
1.6937
1.2407
2.8651
1.7917
3.1708
0.9668
1.2540
0.8306
0.7938
3.0571
2.2362
2.1484
0.8283
3.0122
2.3595
1.5397
2.0397
1.7001
0.5417
1.1725
0.7433
0.9743
20
1.1727
1.7481
0.4144
3.1491
3.2642
1.9726
1.9677
1.4973
3.1418
1.5979
0.6940
2.8097
1.6624
1.1786
1.7066
0.6321
0.7777
3.2567
3.2951
2.2230
0.4653
1.1548
1.5467
2.9239
0.2063
0.3794
21
0.9193
2.4376
2.6732
2.4337
1.8515
2.1400
1.3625
2.7098
0.9927
2.5455
0.9728
1.5954
2.3698
2.8096
0.5651
3.2221
2.7970
1.9603
1.8054
1.8389
1.3954
2.0255
2.0323
2.9140
2.8505
22
Annex 2. Values of X. 3
-0.0004
-0.0248
-0.0221
-0.0105
-0.0000
-0.0191
-0.0004
-0.0133
-0.0082
-0.0538
-0.3110
-0.0022
-0.0150
-0.0000
-0.0037
-0.0002
-0.0184
-0.0001
-0.0000
-0.0514
-0.1324
-0.0130
-0.0091
-0.0033
-0.0257
-0.0098
-0.0028
-0.0008
-0.0009
3
This are the first values of X, because they are 100000, and it is not efficient to put all of them.
23
-0.0796
-0.1012
-0.0982
-0.0027
-0.1561
-0.0032
-0.0223
-0.0178
-0.4234
-0.0063
-0.0002
-0.0070
-0.0002
-0.0025
-0.0241
-0.0268
-0.1056
-0.0420