30
Basics of Monte Carlo Simulations: practical exercises Piero Baraldi Politecnico di Milano, Energy Department Phone: +39 02 2399 6345 [email protected]

Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Basics of Monte Carlo Simulations: practical exercises

Piero Baraldi

Politecnico di Milano, Energy Department

Phone: +39 02 2399 6345

[email protected]

Page 2: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

EXERCISE 1

Page 3: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

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. Verify whether the obtained distribution provides a

good approximation of the Weibull distribution

Page 4: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

Sampling Random Numbers from FX(x)

Page 5: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

MATLAB COMMAND

• rand(M,1) provides a column of M random numbers sampled

from an uniform distribution in the range [0,1)

• N = hist(Y) bins the elements of vector Y into 10 equally

spaced counters and returns the number of elements in

each counter. More options if you write ‘help hist’

Page 6: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

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 7: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

% 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 8: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

% Verify the distribution

% Estimated pdf

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);

% Compute the analytic pdf

analytic_weibull = alpha*beta*channel_centers.^(alpha-

1).*exp(-beta*channel_centers.^alpha);

Page 9: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

% plot the pdf

figure % pdf

plot(channel_centers,analytic_weibull); hold on;

plot(channel_centers,pdf_est,'-sr'); grid

legend('Analytic pdf','Sampled values distribution pdf',0)

% plot the cdf

figure 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 10: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

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 11: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

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 12: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

EXERCISE 2

Page 13: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

13Excercise 2

Write the pseudo code for the estimation of the instantaneous

unavailability of a continuously monitored component with constant

failure rate (𝜆) and repair rate (𝜇)

Hints:

▪ You can assume a mission time of 10^3

▪ You can compute the instantaneous availability at times:

0,1,2,3,…10^5

values

3· 10-3 h-1

m 25· 10-3 h-1

Page 14: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

14

%Initialize parameters

Tm=1000; %mission time;

M=10000; %number of trials;

lambda=3E-3;

mu=25E-3;

Dt=1; %bin length;

Time_axis=0:Dt:Tm;

counter_q=zeros(1,length(Time_axis));

Excercise 2 - Solution

Page 15: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

15

for i=1:M %simulation of M MC trials

%parameter initialization for each trial

t=0;

state=0; %state=0 = working;

%state=1 = failed;

while t<Tm

if state == 0 %system is working sampling of failure time

t=t-[log(1-rand)]/lambda; %failure time

failure_time=t;

state=1; % new state=failed

lower_b=min(find(Time_axis>=failure_time)); % first unavailability counter to be increased

else %system is failed then sampling of repair time

t=t-[log(1-rand)]/mu; %repair_time

state=0;

repair_time=t;

if t<Tm

upper_b=max(find(Time_axis < repair_time)); %last unavailability counter to be increased

else %repair ends after mission time

upper_b=length(Time_axis);

end

counter_q(lower_b:upper_b)= counter_q(lower_b:upper_b)+1; %increase all unavailability counter between lower_b and

%upper_b

end

end

end

Excercise 2 - Solution

Page 16: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

16

Unav_MC=counter_q(:)/M;

Unav_true=(lambda)/(lambda+mu)-(lambda/(lambda+mu))*exp(-

(lambda+mu).*Time_axis);

plot(Time_axis,Unav_true,'blue')

hold on

plot(Time_axis,Unav_MC,'red')

Excercise 2 - Solution

M=10000

Page 17: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

17Excercise 2 - Solution

M=100000

Page 18: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

EXERCISE 3

Page 19: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

Exercise 3

• 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 20: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

Components’ times of transition between states are exponentially distributed

Arrival

Initial1 2 3

1(nominal) 0 ,C)(BA )(31BA

2 (failed) m ,C)(BA 0 )(32

BA

3 (failed) )(13BA

)(23

BA 0

Indirect Monte Carlo

A

CB

Page 21: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

• 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 22: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

• 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 23: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

• 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

m CBA 1,2,1

Analog Monte Carlo Trial

Sampling the kind of Transition (2)

Page 24: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

24

• When the system enters a failed, tallies are

appropriately collected for the unreliability estimates;

• After performing a large number of trials M, we can

obtain estimates of the system unreliability by simply

dividing by M, the number of MC simulation ended in a

failure configuration

Unreliability and Unavailability Estimation

Page 25: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

% Reliability of Simple Systems (A\\(B+C))

% 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...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

% 2 = Failure; 1 = Working

Trans_A = [0 lambda_A; mu_A 0];

Trans_B = [0 lambda_B; mu_B 0];

Trans_C = [0 lambda_C; mu_C 0];

failed_states = [2 1 2; 2 2 1; 2 2 2];

initial_state = [1 1 1];

% Mission time

Tmiss = 500;

Page 27: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

% MC simulation parameters

M = 1e3;

% MC cycle

unrel_counter = zeros(1,N);

for n = 1:M % Main Monte Carlo cycle

unrel_flag = 0; % 0 if no failures before Tmiss, 1 otherwise

t = 0;

current_state = initial_state;

Page 28: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

while (t < Tmiss)

% check if the system is in a failure configuration

for jj = 1:3 % for each one of the 3 failure states:

if sum(current_state == failed_states(jj,1:3)) == 3

unrel_flag = 1;

end

end

% find the system transition rate (Lambda_sys)

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...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

% Sample transition time (t_trans)

t_trans = -1/lambda_sys*log(rand);

t = t+t_trans;

if t < Tmiss % if the transition time falls within Tmiss 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

%Change the state of the component that makes the transition

old_st = current_state(comp);

current_state(comp) = 3-current_state(comp);

end

Page 30: Presentazione di PowerPoint - LASAR...Presentazione di PowerPoint Author simon Created Date 5/5/2018 3:31:27 PM

Piero BARALDI

else %(unreliability flag is 1)

break % break the while loop

end %(end of the if of the unreliability flag)

end %(of the while loop)

unrel_counter(n) = unrel_flag;

end % end of the N simulation

% Estimate the reliability by the MC samples

rel_MC = 1-mean(unrel_counter);

var_MC = var(unrel_counter)/M;