Second Laboratory Report. Probability and Random Variables.

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

1

Second Laboratory Report. Probability and random variables.


Elisa Lin Fu
Elisa Gómez Castro
Universidad Carlos III de Madrid, Group 65
Statistics
Pablo Morala Miguélez
18 March 2024
2

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.8

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…

>> c1 = 1*(v <= pa) + 0*(v > pa);

>> 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…

>> c2 = 1*(v2 <= p2)+ 0*(v2 > p2);

>> p3 =0.75

p3 =

0.7500

>> v3= rand(n,1)

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=rand (n,1)

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…

>> c4 = 1*(v4 <= p4)+ 0*(v4 > p4);

>> S = c1 & c2 & (c3 | c4);


answer=mean(S)

answer =

0.6550
6

The theoretical probability is 0.6375 as it would be P(A n B n (C u D))=


0.8*0.85*0.9375= 0.6375 and P(C u D) = P(C) + P(D) - P(C n D) = 0.75+0.75 -0.75²=0.9375
so the simulated answer we obtained is appropriate as it has a close margin and is
appropriate to the actual calculated answer(theoretical result).

1.2.

>> p5= 0.75

p5 =

0.7500

>> v5=rand (n,1)

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…

>> c5=1*(v5 <= p5)+ 0*(v5 > p5);

>> p6= 0.75

p6 =

0.7500

>> v6=rand (n,1)

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…

>> c6=1*(v6 <= p6)+ 0*(v6 > p6);

>> S2 = c1 & c2 & (c3 | c4 | c5 | c6);


answer2=mean(S2)

answer2 =

0.6990

>> Increase=((answer2-answer)/answer)*100

Increase =

6.7176

The theoretical result is:


P(A∩B∩(C∪D∪E∪F))=F))=P(A)*P(B)*P(C∪D∪E∪F)=0.8*0.85*0.99609=0.6784,
P(C∪D∪E∪F)=1−P(C∪D∪E∪F)=1−(1−P(C))*(1−P(D))*(1−P(E))*(1−P(F))=F)=1−(1−0.75)⁴
Increase = ((0.6784-0.6375)/0.6375)*100 =6.416
So the experimental answer we obtained is approximate to the theoretical result as it
there is a small difference between each other.

1.3.

>> p1n= 0.9

p1n =

0.9000

>> vn= rand(n,1)

vn =

0.7708
0.6858
0.8677
0.5805
0.6261
0.1111
0.2137
0.0364
0.4445
0.3266…
8

>> c1n = 1*(vn <= p1n) + 0*(vn > p1n);

>> p2n =0.95

p2n =

0.9500

>> v2n= rand(n,1)

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…

>> c2n = 1*(v2n <= p2n)+ 0*(v2n > p2n);

>> S3 = c1n & c2n & (c3 | c4);


answer3=mean(S3)

answer3 =

0.8040

>> Increase2=((answer3-answer)/answer)*100

Increase2 =

22.7481

>> p3n=0.95

p3n =

0.9500

>> v3n= rand(n,1)


9

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.95

p4n =

0.9500

>> v4n= rand(n,1)

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…

>> c4n = 1*(v4n <= p4n)+ 0*(v4n > p4n);

>> S4 = c1 & c2 & (c3n | c4n);


answer4=mean(S4)

answer4 =
10

0.6980

>> Increase3=((answer4-answer)/answer)*100

Increase3 =

6.5649

We can observe when we increase the probability of components A and B, it affects


the reliability the greatest, 22.74%. Therefore, the first case has a greater impact on the
system.

Exercise 2.
2.1.

>> v1 = rand(n, 1);


X1 = 1*(v1<=0.3) + 2*(v1>0.3 & v1<=0.5) + 3*(v1>0.5 & v1<=0.7) + 4*(v1>0.7 & v1<=0.9) +
5*(v1> 0.9);
>> averageX1= mean(X1)

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

These values should be approximate to these values: mean= (1 * 0.3) + (2 * 0.2) + (3


* 0.2) + (4 * 0.2) + (5 * 0.1)= 2.6 variance = ((1 - 2.6)^2 * 0.3) + ((2 - 2.6)^2 * 0.2) + ((3 -
2.6)^2 * 0.2) + ((4 - 2.6)^2 * 0.2) + ((5 - 2.6)^2 * 0.1)=1.84 which is true the mean and the
variance of X1 and X2 are similar to these values.
11

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

1.0000 317.0000 31.7000


2.0000 195.0000 19.5000
3.0000 212.0000 21.2000
4.0000 187.0000 18.7000
5.0000 89.0000 8.9000
>> x2

x2 =

1.0000 273.0000 27.3000


2.0000 209.0000 20.9000
3.0000 210.0000 21.0000
4.0000 206.0000 20.6000
5.0000 102.0000 10.2000

>> 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=

1000×1 logical array

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

We can calculate the density function of a continuous variable, by differentiating the


distribution function, just like in the next calculation shows.

Formula 1. Calculations for the density function.

{
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.

- Define the original function

- We fix the number of n inputs

- Generate n data u U ( 0 ,1 )

- Calculate the inverse function of the original

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.

Figure 3. Octave code.

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

Formula 2. Calculations for the distribution function.

{
β
β−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.

Formula 3. Distribution function.


β

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.

Formula 4. Calculations for the inverse function od the distribution function.

F ( x )=−e−α x inverse F−1 ( x )=


⇒ √
β log ⁡( u)
α
=x

Knowing all this information, we can actually state that:

Figure 6. Octave code.

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

Figure 7. Octave code.

When we ask MATLAB for the values of “m_formula”, “m_teorica”, “v_formula” and
“v_teorica”.

Figure 8. Values of the median.

Figure 9. Values of the variance.


17

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

You might also like