presentazione di powerpoint - lasar€¦ · francesco di maio fx xx 1 e o sample r from u r (r) and...
TRANSCRIPT
![Page 1: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/1.jpg)
Basics of Monte Carlo Simulations: practical exercises
![Page 2: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/2.jpg)
Francesco DI MAIO
EXERCISE 1
![Page 3: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/3.jpg)
Francesco DI MAIO
Exercise 1
Consider the Weibull distribution:
with
1. Sample N=400 values from
2. Show a Figure reporting the distribution of the
obtained samples
3. Provide an estimate of with N=400
and N=6400
4. Estimate the variance of with N=400 and N=6400
![Page 4: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/4.jpg)
Francesco DI MAIO
x
X exF 1
Sample R from UR(r) and find X:
RFX X
1
1
1 r 0
rUrR RPr xFX
R X x
x
X exFR 1
RRFX X 1ln11
Example: Exponential distribution
Sampling Random Numbers from FX(x)
![Page 5: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/5.jpg)
Francesco DI MAIO
Example: Weibull Distribution
• Time-dependent hazard rate
cdf:
pdf:
• Sampling a failure time T (by the inverse transform)
1 t
TF t P T t e
1 t
Tf t dt P t T t dt t e dt
1 t
R TR F r F t e
1
1 1ln 1TT F R R
1 tt
![Page 6: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/6.jpg)
Francesco DI MAIO
% Exercise 1
clear all; close all
% Weibull parameters
beta = 1;
alpha = 1.5;
% Sample N values from the Weibull distribution
N = 400; % number of samples
r = rand(N,1);
t = (-log(1-r)/beta).^(1/alpha); % inverse transform method
![Page 7: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/7.jpg)
Francesco DI MAIO
% Verify the distribution
delta_channel = 0.1;
channel_centers = [delta_channel/2:delta_channel:5-
delta_channel/2];
num_samples = hist(t,channel_centers);
pdf_est = num_samples/(N*delta_channel); %
Normalization
% Compute the analytic pdf
analytic_weibull = alpha*beta*channel_centers.^(alpha-
1).*exp(-beta*channel_centers.^alpha);
![Page 8: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/8.jpg)
Francesco DI MAIO
figure % pdf
plot(channel_centers,analytic_weibull); hold on;
plot(channel_centers,pdf_est,'-sr'); grid
legend('Analytic pdf','Sampled values distribution pdf',0)
figure % cdf
cdf_est = cumsum(pdf_est)*delta_channel;
plot(channel_centers,
cumsum(analytic_weibull)*delta_channel);hold on;
plot(channel_centers, cdf_est,'*r'); grid
legend('Analytic cdf','Sampled values distribution cdf',0)
![Page 9: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/9.jpg)
Francesco DI MAIO
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 50
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Analytic pdf
Sampled values distribution pdf
![Page 10: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/10.jpg)
Francesco DI MAIO
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 50
0.2
0.4
0.6
0.8
1
1.2
1.4
Analytic cdf
Sampled values distribution cdf
![Page 11: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/11.jpg)
Francesco DI MAIO
MC Evaluation of Definite Integrals (1D)
Analog Case
MC analog dart game: sample x from f(x)
• the probability that a shot hits x dx is f(x)dx
• the award is g(x)
Consider N trials with result {x1, x2, …, xn} and define the estimator:
N
i
iN xgN
G1
1
1 ; 0 pdf
b
a
b
a
dxxfxfxf
dxxfxgG
iN
N
xgVarN
GVar
GGE
1
2
2
1 11
1 2 11 1
E T
Var T
Real values (from theory)
![Page 12: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/12.jpg)
Francesco DI MAIO
% Estimating the mean of the distribution (definite integral)
GN = mean(t); % Sample mean
disp(['G_N = ',num2str(GN)])
S2GN = var(t)/N;
disp(['G_N variance = ', num2str(S2GN)])
disp(['Estimate = ',num2str(GN),' +-
',num2str(sqrt(S2GN))]);
disp([' '])
disp(['True Value = ', num2str(gamma(5/3))])
% mean_weibull=1/beta(gamma(1+1/alfa))
disp([' '])
disp(['Error = ', num2str(abs(gamma(5/3)-GN))])
![Page 13: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/13.jpg)
Francesco DI MAIO
Interpretation of the Variance of GN (1)
GN
1st run of the code:
400 samples from f
2nd run of the code:
400 different samples
100th run of the code:
400 different samples
GN is a random variable:
GN distribution is approximated by a Normal
iN
N
xgVarN
GVar
GGE
1
N
i
iN xgN
G1
1
GN
0.35 0.4 0.45 0.5 0.55 0.6 0.65 0.70
500
1000
1500
2000
2500
3000
3500
G
h(GN)
![Page 14: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/14.jpg)
Francesco DI MAIO
Interpretation of the Variance of GN (2)
GN
G
h(GN)
NGG NGG
0.68… ; 0.68
N NN G GP G G G
unknown
; 0.68N NN G N G
P G G G
GN
1st run of the code:
400 samples from f
2nd run of the code:
400 different sample
G
100th run of the code:
400 different samples
![Page 15: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/15.jpg)
Francesco DI MAIO
% Validation
clc; clear all;
Nval = 1000; % Number of estimates of the integral
inside = 0;
% Weibull parameters
beta = 1;
alpha = 1.5;
N = 400; % number of samples
![Page 16: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/16.jpg)
Francesco DI MAIO
for i=1:Nval
% Sampling N values from the Weibull distribution
r = rand(N,1);
t = (-log(r)/beta).^(1/alpha); % inverse transform method
% Verifying the distribution
GN(i) = mean(t);
S2GN(i) = var(t)/N;
SGN(i) = S2GN(i)^0.5;
ext_inf(i) = GN(i)-(S2GN(i))^0.5;
ext_sup(i) = GN(i)+S2GN(i)^0.5;
![Page 17: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/17.jpg)
Francesco DI MAIO
if((gamma(5/3)>ext_inf(i)) & (gamma(5/3)<ext_sup(i)))
inside = inside+1;
end
end
figure
errorbar(GN(1:30),SGN(1:30),'.');
hold on; plot(gamma(5/3)*ones(1,30),'r');
axis([0 31 0.5 1]);
%plot(ext_inf(1:100),'.b');hold on;
plot(ext_sup(1:100),'.k'),hold on;
plot(gamma(5/3)*ones(1,100),'r')
disp(['Validation = ',num2str(inside/Nval)]);
![Page 18: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/18.jpg)
Francesco DI MAIO
0 5 10 15 20 25 300.5
0.55
0.6
0.65
0.7
0.75
0.8
0.85
0.9
0.95
1
Validation = 0.692
![Page 19: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/19.jpg)
Francesco DI MAIO
EXERCISE 2
![Page 20: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/20.jpg)
Francesco DI MAIO
Exercise 2
• Consider the following system
• Transition rates (in arbitrary time-units):
Failure: λA = 0.001; λB = 0.002; λC = 0.005;
Repair: μA = 0.1; μB = 0.15; μC = 0.05;
• Estimate the reliability of the system at Tmiss = 500
A
CB
![Page 21: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/21.jpg)
Francesco DI MAIO
Components’ times of transition between states are exponentially distributed
Arrival
Initial1 2 3
1(nominal) 0 ,C)(BA )(31BA
2 (failed) ,C)(BA 0 )(32
BA
3 (failed) )(13BA
)(23
BA 0
Indirect Monte Carlo
A
CB
![Page 22: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/22.jpg)
Francesco DI MAIO
• The rate of transition of the system out of its current configuration (1,1, 1) is:
• We are now in the position of sampling the first system transitiontime t1, by applying the inverse transform method:
where Rt ~ U[0,1)
CBA 1,1,1
)1ln(1
1,1,101 tRtt
Analog Monte Carlo Trial
Sampling the time of transition
![Page 23: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/23.jpg)
Francesco DI MAIO
• Assuming that t1 < TM (otherwise we would proceed to the
successive trial), we now need to determine which component
has undergone the transition
• The probabilities of components A, B, C undergoing a transition
out of their initial nominal states 1, given that a transition occurs
at time t1, are:
• Thus, we can apply the inverse transform method to the discrete
distribution
1,1,11,1,11,1,1 , ,
CBA
RC U[0,1)
Analog Monte Carlo Trial
Sampling the kind of Transition (1)
100
Rc
C 1,1,1
A
1,1,1
B
1,1,1
C
![Page 24: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/24.jpg)
Francesco DI MAIO
• As a result of this first transition, at t1 the system is
operating in configuration (1,2,1).
• The simulation now proceeds to sampling the next
transition time t2 with the updated transition rate
CBA 1,2,1
Analog Monte Carlo Trial
Sampling the kind of Transition (2)
![Page 25: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/25.jpg)
Francesco DI MAIO
% Reliability of Simple Systems (A\\(B+C))
clc
choice = input('1 --> Simulation mode 2 --> Step by step
mode');
% System parameters
lambda_A = 0.001; mu_A = 0.1;
lambda_B = 0.002; mu_B = 0.15;
lambda_C = 0.005; mu_C = 0.05;
![Page 26: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/26.jpg)
Francesco DI MAIO
Trans_A = [0 lambda_A; mu_A 0];
Trans_B = [0 lambda_B; mu_B 0];
Trans_C = [0 lambda_C; mu_C 0];
% 2 = Failure; 1 = Working
failed_states = [2 1 2; 2 2 1; 2 2 2]; % cutset - the last one is
useless
initial_state = [1 1 1];
% Mission time
Tmiss = 500;
![Page 27: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/27.jpg)
Francesco DI MAIO
% MC simulation parameters
N = 1e3;
% MC cycle
unrel_counter = zeros(1,N);
for n = 1:N % Main Monte Carlo cycle
if choice == 2
disp([' '])
disp(['Cycle = ', num2str(n)])
pause
end
unrel_flag = 0; % 0 if no failures before Tmiss, 1 otherwise
t = 0;
current_state = initial_state;
![Page 28: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/28.jpg)
Francesco DI MAIO
while (t < Tmiss)
for jj = 1:3 % 3 is the number of cutset
if sum(current_state == failed_states(jj,1:3)) == 3
unrel_flag = 1;
end
end
if unrel_flag ~= 1
lambda_A_out = sum(Trans_A(current_state(1),:));
lambda_B_out = sum(Trans_B(current_state(2),:));
lambda_C_out = sum(Trans_C(current_state(3),:));
lambda_sys = lambda_A_out + lambda_B_out +
lambda_C_out;
![Page 29: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/29.jpg)
Francesco DI MAIO
t_trans = -1/lambda_sys*log(rand); % Sample transition time
t = t+t_trans;
if choice == 2
disp(['System transition time = ', num2str(t)])
pause
end
if t < Tmiss % if the transition time falls within Tmiss, then
sample the kind of transition
r = rand;
sum_l = cumsum([lambda_A_out lambda_B_out
lambda_C_out])/lambda_sys;
comp = min(find(sum_l>r)); % Component that makes
the transition
![Page 30: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/30.jpg)
Francesco DI MAIO
if choice == 2
disp(['Component = ', int2str(comp)])
pause
end
old_st = current_state(comp);
current_state(comp) = 3-current_state(comp);
if choice == 2
disp(['Transition = ', int2str(old_st), ' --> ',
int2str(current_state(comp))])
pause
end
end
![Page 31: Presentazione di PowerPoint - LASAR€¦ · Francesco DI MAIO FX xx 1 e O Sample R from U R (r) and X find X: F X R 1 1 r X1 0 Pr ^ R d r ` U R r F X x R x x R F X x e 1 O X F X R](https://reader034.vdocuments.net/reader034/viewer/2022051901/5fefbe03cec42428be2507ea/html5/thumbnails/31.jpg)
Francesco DI MAIO
else % if unrel_flag = 1 (failure before Tmiss)
break % break the while loop
end
end
unrel_counter(n) = unrel_flag;
end
% Compute the analytic reliability
rel_analytic = 1-(1-exp(-lambda_A*Tmiss))*(1-exp(-
lambda_B*Tmiss)*exp(-lambda_C*Tmiss));
% Estimate the reliability by the MC samples
rel_MC = 1-mean(unrel_counter);
var_MC = var(unrel_counter)/N;