presentazione di powerpoint - lasar€¦ · francesco di maio fx xx 1 e o sample r from u r (r) and...
TRANSCRIPT
Basics of Monte Carlo Simulations: practical exercises
Francesco DI MAIO
EXERCISE 1
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
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)
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
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
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);
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)
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
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
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)
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))])
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)
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
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
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;
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)]);
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
Francesco DI MAIO
EXERCISE 2
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
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
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
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
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)
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;
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;
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;
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;
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
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
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;