1 Introduction

Subset Simulation provides a powerful tool for the assessment of small failure probability and optimization design in engineering design field. It is originally developed from the concept of conditional probability and Markov Chain Monte Carlo (MCMC) technique by Au and Beck (2001), and well-known as an efficient Monte Carlo technique for variance reduction in reliability analyses. Many variants of standard Subset Simulation have appeared in the literature due to its elegant idea, robustness in high dimensions, high efficiency, etc. Ching et al. introduced the splitting of a trajectory into MCMC algorithm to increase the acceptance rate of a candidate sample when there is a causal relationship between inputs and outputs (Ching et al. 2005a). They further developed a hybrid Subset Simulation method to improve their version (Ching et al. 2005b). By separating the calculation of linear elastic and inelastic structural response, Katafygiotis and Cheung presented a two-stage Subset Simulation for estimating the reliability of inelastic structural systems subjected to Gaussian random excitations (Katafygiotis and Cheung 2005). Along the spirit of decomposing the failure region into a series of sub-regions, Katafygiotis and Cheung also proposed a spherical Subset Simulation for high dimensional problems (Katafygiotis and Cheung 2007). Zuev et al. developed an optimal scaling strategy for the modified Metroplis-Hasting algorithm (MMH), provided a theoretical basis for the optimal value of the conditional failure probability, and proposed a Bayesian Subset Simulation which can produce the posterior probability density function (PDF) of the failure probability instead of a constant value (Zuev et al. 2012). Apart from abovementioned variants of Subset Simulation, some researchers aimed at reducing the correlation in conditional samples generated by MCMC techniques (Santoso et al. 2011; Zuev and Katafygiotis 2011), and other researchers concentrated on combining Subset Simulation with surrogate models, e.g., artificial neural network (Papadopoulos et al. 2012), support vector machine (Bourinet et al. 2011), to further improve its efficiency.

Subset Simulation was originally developed for solving reliability analysis and risk assessment of civil structures subjected to uncertain earthquake ground motions (Au and Beck 2001; Ching et al. 2005a, b; Katafygiotis and Cheung 2005, 2007; Au and Beck 2003; Au and Wang 2014; Tee et al. 2014). It has been widely applied in many engineering fields, such as aerospace engineering (Pellissetti et al. 2006; Song et al. 2009), geotechnical engineering (Santoso et al. 2011; Wang et al. 2010, 2011; Li et al. 2015, 2016), nuclear engineering (Zio and Pedroni 2012; Wang et al. 2015), and also as simulation engine for improving efficiency in different stochastic simulation algorithms, for example (Chiachío et al. 2014a, b). Please refer to Au and Wang (Au and Wang 2014) for a comprehensive summary of the applications of Subset Simulation.

Subsequent studies by Li and Au (2010); (Li 2011); Li and Ma (2015), showed that Subset Simulation can be efficiently used for structural optimization. Based on the idea that an optimization problem can be formulated as a reliability problem by augmenting the design variables as random variables, an artificial reliability problem can be constructed, which follows a similar idea for solving reliability problem using Monte Carlo simulation (Au 2005). As a result, Subset Simulation is extended to solve constrained optimization problems (Li and Au 2010), unconstrained optimization problems (Li 2011) and discrete optimization problems (Li and Ma 2015) as a stochastic searching and optimization algorithm.

In this paper, two efficient and compact Matlab codes are presented to perform reliability analysis and structural optimization using Subset Simulation. The Matlab codes presented here are intended to facilitate engineering education and applications of Subset Simulation, which will provide instructions to students and newcomers to Subset Simulation. Matlab is a high-level computational language which has many outstanding characteristics, e.g., accessible syntax, excellent debugging tools and extensive graphics output. These allow users to concentrate on the physical and mathematical modeling of engineering problems without being distracted by how to implement it. Therefore, Matlab is the ideal environment for learning to program, solving computationally problems and writing prototype programs. Both codes include three parts: (1) algorithm parameter definition, (2) direct Monte Carlo (DMC), and (3) MCMC. These two codes are organized and written as a built-in function in Matlab so that no modification is required for new applications except for input information.

This paper starts with a brief theoretical background of Subset Simulation for reliability analysis and structural optimization. Section 3 shows the details and instructions of Matlab codes of Subset Simulation. The usage of Matlab codes are explained using two numerical examples. Section 4 discuses several extensions to the developed codes. Section 5 presents the numerical implementation procedures and results of two practical problems, one for structural reliability analysis and the other one for structural optimization design. Finally, major conclusions drawn from this paper are summarized in Section 5. The codes are provided in Appendix 1 and 2, and can also be downloaded from the website: https://sites.google.com/site/rasosubsim/.

2 Theoretical background

Subset Simulation is a stochastic simulation procedure for estimating small failure probabilities and solving optimization problems. Specifically, we consider some engineering systems subject to random input parameters. Let us define the failure region F as the subregion in the x-space that exceeds a response function (or the system performance function) g(x) below a specific threshold value b, as follows:

$$ F=\left\{\mathbf{x}:g\left(\mathbf{x}\right)<b\right\} $$
(1)

where x is the input random vector which models all uncertain parameters in the system. In fact, g(x) can be a nonlinear and implicit function of x. The target failure probability P F associated with the target failure event F may be very small (P F  < < 1). Under the setting where a single system response analysis requires the solution of a numerical model (e.g., finite element model), an excessive number of simulations may be required to estimate the target failure probability with a desired accuracy. The basic idea of Subset Simulation is to convert a small probability into a product of a sequence of large conditional probabilities, each of which has a smaller coefficient of variation (c.o.v.). This conversion is achieved by dividing the input parameter space into subset failure domains. Thus, a sequence of intermediate events are required to be defined in the same way of the target failure event

$$ {F}_j=\left\{\mathbf{x}:g\left(\mathbf{x}\right)<{b}_j\right\},j=1,\dots, m $$
(2)

where b j is a series of threshold values of the system response and m is the total number of intermediate events. Importantly, it is assumed that F 1, F 2, ⋯, F m are a sequence of nested events, i.e., F 1 ⊃ F 2 ⊃ ⋯ ⊃ F m  = F. Note that the values of b j cannot be determined in advance. However, the determination of b j and further the intermediate events can be achieved in an adaptive manner, i.e., setting the conditional probabilities P(F i |F i−1) equal to a specified value (Au and Beck 2001). To ensure the nestedness of F j ,  j = 1, …, m, the threshold values are arranged such as b 1 > b 2 > … > b m  = 0. Because of the nested nature of all intermediate events, the target failure probability can be rewritten as

$$ {P}_F=P(F)=P\left({F}_1\right){\displaystyle \prod_{j=1}^{m-1}P\left({F}_j\left|{F}_{j-1}\right.\right)} $$
(3)

By (3), the simulation of a rare event F is subdivided to the simulations of a series of frequently conditional events F j |F j−1. Therefore, generating conditional samples in F j |F j−1 is the pivotal of successful implementation of Subset Simulation. This can be achieved using MCMC techniques. In the following subsections, we briefly review Subset Simulation for reliability analysis and structural optimization, and also give an overview of MCMC methods.

2.1 Subset simulation for reliability analysis

For reliability analysis, Subset Simulation starts with DMC in the first step. The probability P 1 associated with the first intermediate event F 1 is estimated as

$$ {P}_1=P\left({F}_1\right)\approx \frac{1}{N}{\displaystyle \sum_{i=1}^N{I}_{F_1}\left(g\left({\mathbf{x}}_i\right)\right)} $$
(4)

where N is the number of samples in the first simulation level, {x i } are independent and identically distributed (i.i.d.) samples generated according to the probability density function (PDF) f(x) and \( {I}_{F_1}\left(\cdot \right) \) is the indictor function

$$ {I}_{F_1}\left(\cdot \right)=\left\{\begin{array}{l}0\kern0.75em \mathrm{if}\kern1.5em g\left({\mathbf{x}}_i\right)\ge {b}_1\\ {}1\kern0.75em \mathrm{if}\kern1.5em g\left({\mathbf{x}}_i\right)<{b}_1\end{array}\right. $$
(5)

In (5), however, b 1 is unknown, and so is the first intermediate event F 1. If a fixed value p 0 is set to P 1, one can employ (4) to determine the value of b 1 and the first intermediate event F 1. After generating {x i }, calculate the system response function {g(x i )} for all i, and sort them in an ascending order such that g(x 1) ≤ g(x 2) ≤ ⋯ ≤ g(x N ). Let b 1 be the sample P 1-quantile of the system response, i.e., \( {b}_1=g\left({\mathbf{x}}_{\left[{P}_1N\right]}\right) \). Herein, the symbol [ ⋅ ] in the subscript denotes to round the argument. In this way, samples \( {\mathbf{x}}_1,{\mathbf{x}}_2,\dots, {\mathbf{x}}_{\left[{P}_1N\right]} \) belong to the first intermediate event F 1, and they automatically satisfy (4).

The subsequent conditional probabilities P j  = P(F j |F j−1) require the samples conditioning on F j−1 with implicit conditional PDF

$$ f\left(\mathbf{x}\left|{F}_{j-1}\right.\right)=f\left(\mathbf{x}\right){I}_{F_{j-1}}\left(\cdot \right)/P\left({F}_{j-1}\right) $$
(6)

One may generate conditional samples based on (6). However, this method is in general inefficient because the acceptance probability of sample is proportional to the inverse of P(F j−1). This means that it would reject about 1/P(F j−1) samples before obtaining one proper conditional samples on average. It is still a DMC estimator for P(F j |F j−1), being consistent with the estimator in (4). Given that one already have [NP j−1] samples belonging to F j−1, a simulation procedure based on MCMC can be employed to obtain the required conditional samples {x i } and then the estimator for P(F j |F j−1)

$$ {P}_j=P\left({F}_j\left|{F}_{j-1}\right.\right)\approx \frac{1}{N}{\displaystyle \sum_{i=1}^N{I}_{F_j}\left(g\left({\mathbf{x}}_i\right)\right)} $$
(7)

where x i  ~ f(x|F j − 1), i = 1, …, N are generated by a modified Metropolis-Hasting type MCMC (Au and Beck 2001), which has been proved that its stationary distribution is equal to the desired conditional distribution. We will discuss this MCMC approach in detail in subsection 2.3. After generating new N − [NP j−1] conditional samples in F j−1 and combining the previously selected [NP j−1] samples, we can compute the system response function {g(x i )} for all i, and sort them in an ascending order. Let b j be the sample P j -quantile of N system responses in F j−1, i.e., \( {b}_j=g\left({\mathbf{x}}_{\left[{P}_jN\right]}\right) \). In this way \( \left\{{\mathbf{x}}_1,{\mathbf{x}}_2,\dots, {\mathbf{x}}_{\left[{P}_jN\right]}\right\} \) belong to the next intermediate event F j , and are guaranteed to satisfy (7) for some fixed P j .

Repeat the above procedure until the sample P j -quantile of N system responses in F j−1 is less than b, i.e., \( {b}_j=g\left({\mathbf{x}}_{\left[{P}_jN\right]}\right)<b \). Then, by this point, the target failure domain F m is already arrived by the algorithm, i.e., j = m and b m  = b. The estimator for the last conditional probabilityP(F m |F m−1) is obtained as

$$ P\left({F}_m\left|{F}_{m-1}\right.\right)\approx {P}_m=\frac{1}{N}{\displaystyle \sum_{i=1}^N{I}_{F_m}\left(g\left({\mathbf{x}}_i\right)\right)} $$
(8)

Combining (4), (7) and (8), the failure probability of the target event is expressed as

$$ {P}_F={\displaystyle \prod_{j=1}^m{P}_j} $$
(9)

In practical engineering applications of Subset Simulation, a typical value of P j ,  j = 1, …, m − 1 is some fixed p 0 ∈ 0.1 ~ 0.3 (Zuev et al. 2012).

2.2 Subset simulation for structural optimization

Consider solving the following unconstrained optimization problem

$$ \begin{array}{l} \min \kern0.3em W\left(\mathbf{d}\right)\\ {}\kern1.25em {\mathbf{d}}^L\le \mathbf{d}\le {\mathbf{d}}^U\end{array} $$
(10)

where W(d) is the objective function, d is the design vector that contains design variables, d L and d U are the lower bound and upper bound for the design vector. The analogy between an optimization problem and a reliability problem is presented firstly, which allows us to solve an optimization problem potentially using reliability analysis methods (Li and Au 2010; Li 2011). The aim of reliability analyses is to evaluate the probability of a target failure event F, while the aim of an optimization problem is to search a point or region where the objective function is minimized under the problem setting in (10), i.e., taking extreme values. We employ a one-dimensional example, as shown in Fig. 1, to explain this analogy. In this illustrative example, only one variable d is involved, and h is a function of d. The optimization problem is defined as finding the minimal value of h in Fig. 1a, i.e., min h(d). By augmenting the design variable d to be a random variable, a reliability problem can also be defined to estimate the probability of h less than a given threshold h 0, as shown in Fig. 1b. This means that the target event is F = {h < h 0} and the corresponding probability is P F  = P(h < h 0). It is well-known that engineering reliability problems are often rare event simulation problems since very small failure probabilities are generally involved in practice. Geometrically, the region of F = {h < h 0} of a reliability problem absolutely covers the minimum points of an optimization problem. In other words, the minimum points to be found of an optimization problem is a reduced region of a reliability problem. Therefore, we can deal with optimization problems under the framework of the reliability analysis because an extreme event is viewed as a special case of a rare event concerned in reliability analyses.

Fig. 1
figure 1

The analogy between an optimization problem and a reliability problem

Based on the analogy between an optimization problem and a reliability problem, we can construct an artificial reliability problem by randomizing the design variables, i.e.,

$$ {P}_F=P(F)=P\left(W\left(\mathbf{d}\right)\le {W}_{opt}\right) $$
(11)

where W opt is the minimum value of the objective function, F = {W(x) ≤ W opt } is the artificial target event, and P F is the corresponding failure probability of F. It is clear that P F has a zero value because W opt is the minimum value of the objective function. However, in an optimization problem, we are interested in the point d opt , where the zero failure probability can be attained, rather than the zero failure probability itself. Since the design vector d is augmented to be random, the type of probability distribution of design vector may have influences on the performance of the transformation in (11). Experiences shows that the truncated normal distributions is sufficient and convenient to characterize design variables themselves and their respective bounds, as suggested by Li and Au (2010); Li (2011).

Equation (11) maps a multidimensional objective function W(d) into a random variable W. According to the definition of cumulative distribution function (CDF) for a random variable, it is a monotonic, non-decreasing, and right-continuous function, such that it displays values lying in the interval of [0, 1]. Therefore, the CDF value at W opt is zero. This is consistent with the zero failure probability discussed above. It is worthwhile to point out that, based on the transformation in (11), local optimums can be avoided, at least, from a theoretical perspective view. The objective function may be a many-to-one relationship between multiple design variables and itself, while the CDF function of W is a one-to-one relationship after mapping manipulation.

In Subset Simulation for structural optimization, the governing equation is still given by (3). Its implementation starts with the initialization of distributional parameters for the design vector d in the first step. The following steps for structural optimization are similar to these for reliability analyses described in subsection 2.1. The optimization iterations are not terminated until a stopping criterion is reached. Two groups of stopping criterion have been developed in the literature. The idea of first group stems from other stochastic optimization method, employing a maximum iteration number or the difference of objective function values between two consecutive iterations smaller than a specified tolerance to enhance for convergence. While the idea of the other group is based on sample statistics, the stopping criterion is defined as (Li and Au 2010; Li 2011)

$$ \max \left({\widehat{\sigma}}_k\right)\kern0.5em or\kern0.5em \max \left(\left|{\widehat{\sigma}}_k-{\widehat{\sigma}}_{k-1}\right|\right)\le \varepsilon $$
(12)

where \( {\widehat{\sigma}}_k \) is the estimator of standard deviation of the samples in the k − th simulation level and ɛ is the specified tolerance.

2.3 Markov Chain Monte Carlo

The main challenge for implementing Subset Simulation is to generate conditional samples in (7) for the estimation of conditional probability. Difficulties arise because the conditional PDF f( ⋅ |F j ) for an intermediate event has an implicit expression. Markov Chain Monte Carlo (MCMC) is a stochastic simulation technique for generating samples from an arbitrary PDF, and it provides a powerful tool to generate conditional samples in the implementation of Subset Simulation.

The working principle of MCMC is that starting with an arbitrary sample x 0, a Markov Chain is generated using a transition kernel whose stationary distribution is equal to the desired distribution. Therefore, the most important component of MCMC is the transition from current state sample x i to the next state x i+1 of the chain. MCMC method was originated from (Metropolis et al. 1953), and was further modified by Hastings to allow nonsymmetrical proposal PDF (Hastings 1970). Today, the Metropolis-Hastings (M-H) algorithm is still the standard implementation of MCMC because it imposes minimal requirements on the desired distribution. The transition x i  → x i+1 in an M-H algorithm is achieved by two steps. First, a candidate state x is generated from a proposal PDF q (·|x i ) which describes how to go from x i to x i+1 and is easier to simulate. Here, it is clear that the proposal PDF is a conditional PDF, however, some variants of the standard version may not require this constraint. Second, the candidate state x is either accepted or rejected with a certain acceptance probability ρ(x, x ). These two steps can be viewed as a local random walk in the neighborhood of the current state x i . For the use in Subset Simulation, there is another step to make sure that the accepted candidate x also lies in the current intermediate failure domain F l−1. If x  ∊ F l−1, x is accepted as the next state; otherwise, x is rejected and x i is repeated in the Markov Chain. This step ensures the correct conditioning in the samples. The final acceptance probability is the product of acceptance probabilities in the last steps, i.e., \( \rho \left(\mathbf{x},{\mathbf{x}}^{\mathbf{\prime}}\right)\cdot {I}_{F_{l-1}}\left({\mathbf{x}}^{\mathbf{\prime}}\right) \).

From above three steps, it can be seen that the MCMC samples are not statistical independent because repeated states occur in a Markov Chain. However, the ergodic theorem can guarantee that statistical inference based on the MCMC samples with stationary distribution f( ⋅ |F l−1) converges to the one based on i.i.d. samples from f( ⋅ |F l−1).

Typically, a Markov Chain starting with an arbitrary sample x 0 may has the burn-in issue for assessing the convergence of a Markov Chain, i.e., the first few samples generated by the M-H algorithm cannot be used for statistical inference. However, the nested setting of Subset Simulation perfectly avoids this issue. Since the seed samples for all Markov Chains follow the desired distribution f( ⋅ |F l−1), all the subsequent samples on the same Markov Chains naturally follow the same desired distribution. This technique is termed perfect simulation in the literature (Robert and Casella 2004).

Another issue associated with the standard M-H type MCMC is ‘the curse of dimension’. As the dimension of problem increases, the acceptance probability ρ(x, x ) in the second step exponentially decreases to zero. For a high dimension n, the candidate x may be always rejected, leading to a large number of repeated samples. Au and Beck (Li and Au 2010); Au and Wang (2014) have examined the issue in detail. They also proposed a component-wise scheme to overcome the high dimension issue and the degeneration of ρ(x, x ), which is attributed to the fact that ρ(x, x ) is a product of PDF ratios and tends to a small number as n increasing. In the modified Metropolis-Hastings (MMH) algorithm, one-dimensional proposal PDF replaces the n-dimensional proposal PDF so that the acceptance probability of a component in the random vector is only a ratio of one-dimension PDFs. This scheme dramatically decreases the probability of having repeated values simultaneously for all the components during the simulation, making it feasible in high dimension problems.

After solving the high-dimensional issue, the next implementation issue may be the choice of the type of one-dimensional proposal PDF and the spread around the current sample since they seem to control the simulation efficiency of MCMC. Simulation experiences show that the efficiency of the MMH algorithm is not sensitive to the type of proposal PDF, while the spread of the one-dimensional proposal PDF is important for the balance of efficiency and robustness. Using large spreads lead to the reduction of the acceptance probability, repeated component samples and slow convergence. In contrast, using small spreads introduces large dependence among component samples due to their proximity and also slows down convergence. Au and Beck (2001) suggested an adaptive manner which uses some fraction of sample standard deviation calculated from all samples or only the [p 0 N] seed samples generated in the previous simulation level. Chiachio et al. (2014a) also provided a sensitivity analysis and proposed to adaptively choose the variance of the jth intermediate level so that the monitored acceptance rate belong to the interval [0.2, 0.4].

3 Matlab implementation

The Matlab codes given in Appendixes 1 and 2 can be used to solve reliability analysis problems and structural optimization problems, respectively. Both Matlab implementations for reliability analysis and structural optimization are written as Matlab build-in functions so that they are considered as standard codes and can be easily used without the need to be a Matlab expert.

3.1 Subset simulation for reliability analysis

In this section, we explain the usage and important details of the 78-line Matlab code in Appendix 1 for reliability analysis. The code is divided into three parts: (1) input parameter definition, (2) DMC, and (3) MCMC, and can be used as a standard code of Matlab. Modification is not needed for different applications. In the following subsections, detailed explanations are given through a high-dimensional example with analytical solution.

3.1.1 Function arguments and outputs

For simplification, the function name is shorten as SS, and the SS function is called from the Matlab command prompt by the line

where is a Matlab function handle which is used to calculate limit state values of interest, is the dimension of the problem, paras is the collection of all algorithm parameters, is the estimator of failure probability, is a record of limit state values that have been used in Subset Simulation, is the corresponding CDF values to limit state values stored in , and is the record of samples corresponding to limit state values stored in .

The data type of SS function arguments have been specified and cannot be overridden. Argument is a Matlab function handle which is bound to the Matlab function for limit state functions and argument is a specified value. Argument is a structure data type with three fields: and . Field is the number of samples in each simulation level. Field is the conditional probability and Field is the maximum number of simulation levels which is used to terminate the simulation procedure so as to avoid infinite loop when the target domain cannot be reached. It should be pointed out that Subset Simulation for reliability analysis has only two algorithm parameters, i.e., and , while is added just for avoiding infinite loop.

To illustrate the use of SS function, consider estimating the failure probability of the following limit state function

$$ h\left(\mathbf{x}\right)=b-\frac{1}{\sqrt{n}}{\displaystyle \sum_{i=1}^n{\mathbf{x}}_i} $$
(13)

where {x 1, …, x n } are i.i.d. standard normal variables, and b is a constant value. It can be easily reasoned that the target failure region is a linear half-space, i.e., F = {h(x) < 0}, and h is a normal variable so that the failure probability is analytically given by

$$ {P}_F=P\left(h\left(\mathbf{x}\right)<0\right)=\varPhi \left(-b\right) $$
(14)

where Φ(⋅) is the CDF of standard normal distribution. It is obvious that (14) is independent of the problem dimension n. In the case of b = 3 and n = 1000, the exact failure probability is Φ(−3) = 1.35 × 10− 3. The following lines represent the evaluation of (13) in Matlab.

For this problem, the arguments for SS function are initialized as follows:

This line means that the number of samples in each simulation level is 300, the conditional probability is taken as 0.1 and the maximum number of simulation level is equal to 10. Then, this problem is solved by calling with the input line

Finally, the calculated results are stored in .

3.1.2 Algorithm parameter definition (lines 3–6)

Symbol is used to represent the number of samples in each simulation level and it receives the passing value from Symbol denotes the conditional probability and it receives the passing value from Symbols and are used for the sample quantile of system responses and the length of Markov Chain, respectively. Note that the product of and an integer. A built-in function in Matlab, i.e., , is employed to round it to the nearest integer (line 5). The similar operation is carried on the length of a Markov Chain (line 6).

3.1.3 Direct Mont Carlo simulation (lines 7–20)

DMC simulation starts with generating samples from standard normal distribution (line 8). Then, the system responses for these samples are evaluated using the evaluation function in Matlab (line 11). Lines 14–15 are the Matlab implementation of sorting and determining the sample quantile of the system response. Lines 16–20 represent the record of SS function outputs.

3.1.4 Markov Chain Monte Carlo (lines 21–78)

Lines 23–25 are some basic preparation for the following MCMC simulation procedure. As mentioned before, the spread of proposal distribution is estimated from the seed samples (line 23).

The Matlab code for MCMC is divided into four parts: (1) selecting seed samples (lines 27–28), (2) generating conditional samples by the MMH (lines 31–62), (3) storing and recording the calculated results (lines 64–69), and (4) terminating the simulation procedure (lines 71–77).

Variables and denote the seed samples and their corresponding system responses, and are used to generate the conditional samples. Line 31 is a counting command beginning with 1 since the first nt samples are selected from the previously simulation level. Additional N − nt conditional samples are supplied to keep the number of samples in a simulation level constant. Lines 34–47 represent the implementation of the first two steps in the MMH algorithm with component-wise strategy. A loop over all components is performed here (lines 34–47). The system response at a candidate sample is calculated in line 48. Then, comparing it with the threshold value (line 50), the corresponding candidate is accepted if it is less than the threshold value (line 51). Otherwise, a repeated sample appears in the current Markov Chain (lines 53–54). The sampling center is updated to the current state in line 56. It should be noted that there is an inner loop over samples in a Markov Chain starting in line 33 and an outer loop over Markov Chains starting in line 31. The same sorting and recording operations as in DMC stage are carried out in lines 64–69. Lines 71–77 are used to terminate the main loop if the threshold value is less than zero, or the number of iteration is larger than the maximum permissible number stored in

For completeness, the estimator of failure probability of (13) is 2.6 × 10−3, which is stored in the variable , using the following prompt lines:

The purpose of lines 2–4 is to set a fixed starting point for the random number generator in Matlab so that one can reproduce the calculated results reported here. The other results, such as the CDF values stored in variable and the corresponding values of LSF stored in variable , can be converted into a CDF plot of h(x), which shows the probability of h(x) smaller than a threshold value h 0, i.e., p(h < h 0). Figure 2 shows such a CDF plot for this problem by using the following prompt lines:

Fig. 2
figure 2

CDF plot for high dimensional problem

3.2 Subset simulation for structural optimization

In this section, we explain the usage and important details of the 98-line Matlab code in Appendix 2 for structural optimization. Similar to the code for reliability analysis shown in Appendix 1, the code also has three parts. We concentrate on the differences between them.

3.2.1 Function arguments and outputs

For simplification, the name of Matlab function for structural optimization is shorten as SSO. The SSO function can be simply called by the line.

Compared with SS function, one argument, i.e., , is added to SSO function to pass the boundaries of design variables to the main program. In the structure , a new field is added to define a specified tolerance for the termination of simulation procedure. For the function outputs, and are used for storing the optimal value of objective function and the corresponding solution vector, respectively. The total number of the objective function evaluation is denoted by . Function outputs and are employed to store the history of the optimal value of objective function and its corresponding solution vector.

To illustrate the usage of SSO function, consider the problem of minimizing the six-hump camel-back function

$$ \begin{array}{c}\hfill \min\;h(x)=4{x}_1^2-2.1{x}_1^4+{x}_1^6/3+{x}_1{x}_2-4{x}_2^2+4{x}_2^4\hfill \\ {}\hfill \mathrm{s}.\mathrm{t}.\;\mathbf{x}\in \left[-3,3\right]\hfill \end{array} $$

In the bounded region, this function has six minima, while four of six minima are local ones (Fig. 3). The global minimum is − 1.0316, and the corresponding solutions are x * = ( − 0.0898, 0.7126) and x * = (0.0898, − 0.7126).

Fig. 3
figure 3

The six-hump camel-back function

3.2.2 Algorithm parameter definition

The distributional parameters for design variables are written in lines 8–9 after they are augmented to be truncated normal variables.

3.2.3 Direct Monte Carlo simulation

Lines 13–19 represent the sampling method for truncated normal variables.

3.2.4 Markov Chain Monte Carlo

Compared with its counterpart in Appendix 1 (line23), a slight modification is made in line 33, calculating the standard deviation using all samples in a simulation level instead of the first Np 0 samples. This leads to a larger spread of the proposal distribution in Subset Simulation for structural optimization.

Since the design variables have boundaries in the parameter space, lines 47–53 are added to ensure that the generated candidate satisfies those constraints. In the MMH code, two PDF calculators are revised according to the definition of truncated distribution (lines 56–57). For structural optimization, the simulation procedure stops when the maximum sample standard deviation (line 83 and line 89) is less than the specified tolerance stored in or the number of simulation levels is greater than the specified maximum number stored in . At the end of the Matlab code, lines 97–98 determine the minimum of the objective function and the corresponding solution.

For the completeness of the illustrated example, we performed Subset Simulation to solve the problem shown in Fig. 3, and the input lines are

The optimization history is given in Fig. 4. After 28 simulation levels, SSO function finds a minimum of − 1.0136 with 2900 function evaluations. However, this minimum value is achieved at the 13th simulation level and after that this value does not change. This is caused by the adoption of sample statistics, i.e., sample standard deviation, for termination.

Fig. 4
figure 4

The optimization history of the six-hump camel-back function

4 Extensions

A number of extensions in the algorithms and codes can be thought of, a few of which are described in the following subsections.

4.1 Argument check

In order to make the code in Appendix 1 operate like a ‘professional’ function in Matlab, the following additional lines may be added just after the definition of SS function (line 1).

These lines provide default parameter setting and throw errors and prompts for the user. For SSO function, it can be done by making small changes to the above lines.

4.2 Post-processing

It is very simple to extend the code to provide some basic post-processing operations, such as the CDF plot for reliability analysis. In fact, this can be done by adding three additional lines just after line 78.

For structural optimization, the following three lines draw a history of optimization.

4.3 Alternative stop criteria for structural optimization

The stop criterion here is based on a combination of the sample standard deviation and the maximum number of simulation levels. Two alternative stop criteria can be adopted. The first one is based on the idea that when the CDF of the objective function approaches zero, the sequence of objective function also approaches its minimum point. Thus, the line 90 in Appendix 2 is changed to

The second stop criterion is based on the difference between the minima of objective function in two consecutive simulation levels.

4.4 Constraint-handling for structural optimization

Admittedly, constrained optimization algorithms are more preferable to unconstrained ones because most of optimization problems in engineering and science fields have a variety of restriction. It is also quite convenient to extend the SSO algorithm to account for multiple general constraints. Here, consider the following constrained optimization problem

$$ \begin{array}{l}\mathrm{M}\mathrm{i}\mathrm{n}\kern1.25em W\left(\mathbf{d}\right)\\ {}s.t.\kern1.5em {g}_i\left(\mathbf{d}\right)<0\kern1.5em i=1,\dots, {n}_e\end{array} $$
(15)

where n e is the number of constraints. In order to include the effects of constraints, a constraint fitness function is defined as

$$ {F}_{con}\left(\mathbf{d}\right)=-\underset{i}{ \max }{\mathbf{v}}_i $$
(16)

where {v i } are the violations of constraints, which are given by

$$ {\mathbf{v}}_i=\left\{\begin{array}{l}0\kern2.75em ,\kern0.75em {g}_i\left(\mathbf{d}\right)<0\\ {}{g}_i\left(\mathbf{d}\right)\kern1em ,\kern0.75em {g}_i\left(\mathbf{d}\right)>0\end{array}\right. $$
(17)

Four lines function, a sub-function of SSO function, can be easily added to realize this definition.

In the case of considering constraints, the sorting sequence is not unique, which is very important to provide seed samples for the next simulation level. Li and Au proposed a double-criterion ranking method to solve this issue successfully. Please refer to Ref. (Li and Au 2010) for more details about this method. The corresponding code is written in lines 103–112, just appended to SSO function as a subfunction.

Both the objective function and the constraint fitness function now are calculated using Fitness subfunction so that line 22 and line 65 are substituted by the following lines.

To sort the samples considering the objective function and the constraint fitness function, both line 25 and 81 become

To generate conditional samples, the constraint fitness function must be induced in the MMH algorithm, which means the lines 67–70 are changed to

5 Practical use

These two codes can be conveniently applied by users in practice. In this section, two applications are considered, one for structural reliability analysis and one for structural optimization design. Since the purpose of two applications is to illustrate the applications of Subset Simulation, we do not focus on comparing it with other methods for accuracy or efficiency.

5.1 Structural reliability analysis

The one-bay one-storey frame is taken from Ref.(Bucher 2009) and is concerned with its collapse subjected to static loads, including a horizontal load P 1 and a vertical load P 2, as shown in Fig. 5. Both P 1 and P 2 are random and normally distributed. The failure of the frame is defined by the first-order rigid-plastic hinge theory, and then three dominant collapse mechanisms can be identified, as shown in Fig. 5 as failure modes. The three failure modes are given by the following relations

$$ {h}_1(X)=4\frac{M_{pl}}{L}-{P}_1 $$
(18)
$$ {h}_2(X)=4\frac{M_{pl}}{L}-{P}_2 $$
(19)
$$ {h}_3(X)=6\frac{M_{pl}}{L}-{P}_1-{P}_2 $$
(20)

where M pl is the plastic moment, which is a deterministic quantity. The frame fails when any one of these three collapse mechanisms occurs. This means that it is a series system, i.e., the LSF of this frame h(x) = min (h 1(x), h 2(x), h 3(x)). Assuming that the mean value of P 1 and P 2 is \( 1.7\frac{M_{pl}}{L} \) and their coefficient of variation is 0.5. The above LSF is transferred into standard normal space, as shown in Fig. 6.

Fig. 5
figure 5

One-bay one-storey frame structure and failure modes

Fig. 6
figure 6

Limit state in the standard normal space

Based on the above given information, the following input lines are used to perform structural reliability analysis of this frame structure.

The CDF plot obtained from the code is shown in Fig. 7. Furthermore, the failure probability given by Subset Simulation is 5.660 × 10−5 with 2,300 samples, which can be known by checking the size of in the command window.

Fig. 7
figure 7

CDF plot for the one-bay one-storey frame problem

The CDF plots for p 0=0.15, 0.20, 0.25 and 0.30 are also given in Fig. 7. The estimated failure probabilities and the number of required samples are listed in Table 1. The purpose of this comparison is to see how Subset Simulation behaves differently by adopting different values of p 0 for the example provided here. It seems the CDF plot with p 0 = 0.3 significantly deviates from the rest. As reported in Ref.(Zuev et al. 2012), the values of p 0 selected from the interval [0.1, 0.3] would produce similar efficiency with similar accuracy. It should be pointed out that this conclusion was drawn based on the specified values of target failure probability and the total number of samples. In fact, the practical interval of p 0 may be narrower or larger than that reported by Zuev et al. (2012). In other words, the optimal value of p 0 is still problem-dependent somewhat although they provided a theoretical basis for the selection of p 0. For the currently investigated problem, p 0 = 0.1, 0.15, 0.20 and 0.25 are suitable for reliability analysis, while p 0 = 0.3 is not proper.

Table 1 Estimated failure probabilities and the number of samples with different p 0

5.2 Structural design optimization

Because constrained optimization problems are more common than unconstrained ones in engineering practice, we provide an example with 4 constraints in this section. It should be noted that, in order to solve this problem, the code in Appendix 2 for structural optimization must include all the extension changes described in the subsection 4.4 entitled “Extensions”.

Consider the tension-compression string design problem taken from Ref. (Coello Coello 2000), as shown in Fig. 8. The objective is to minimize the string weight under constraints on deflection, shear stress, surge frequency, limits on outside diameter and on design variables. There are three design variables in this problem: the wire diameter d, the mean coil diameter D, and the number of active coils P, i.e., the design vector [x 1, x 2, x 3] = [d, D, P] (Fig. 8).

Fig. 8
figure 8

The tension-compression string design problem

Fig. 9
figure 9

The optimization history of the tension-compression string design problem

The problem can be formulated as

$$ \min\ f\left(\mathbf{x}\right)=\left({x}_3+2\right){x}_2{x}_1^2 $$
(21)

Subject to

$$ {g}_1\left(\mathbf{x}\right)=1-\frac{x_2^3{x}_3}{71785{x}_1^4}\le 0 $$
(22)
$$ {g}_1\left(\mathbf{x}\right)=\frac{4{x}_2^2-{x}_1{x}_2}{12566\left({x}_2{x}_1^3-{x}_1^4\right)}+\frac{1}{5108{x}_1^2}-\le 0 $$
(23)
$$ {g}_3\left(\mathbf{x}\right)=1-\frac{140.45{x}_1}{x_2^2{x}_3}\le 0 $$
(24)
$$ {g}_4\left(\mathbf{x}\right)=\frac{x_2+{x}_1}{1.5}-1\le 0 $$
(25)
$$ 0.05\le {x}_1\le 2.0,0.25\le {x}_2\le 1.3,2.0\le {x}_3\le 15.0 $$
(26)

The objective function and all constraints are calculated by a Matlab function named .

Based the above information, the input lines for this optimization problem are

Figure 9 shows the optimization history of this problem. After 93 simulation levels, SSO finds a minimum of 0.0127 at design vector [d, D, P] = [0.0528, 0.3849, 9.8076]. The total number of function evaluation is 23,500. This optimization result is a feasible solution by re-examining all constraints’ values.

6 Summary and conclusions

This paper presents two very simple Matlab codes of Subset Simulation for reliability analysis and structural optimization. The two codes for reliability analysis and structural optimization are comprised of 78 lines and 98 lines, respectively. The codes can be easily extended to include more considerations. For instance, constraint-handling method is included to deal with constrained optimization problems. We made attempts to organize the codes in the way of Matlab standard functions so that users can conveniently use them without modifications for their specific applications. This will be very helpful for the beginners in reliability engineering and structural optimization using Subset Simulation for their own applications. The usages of SS and SSO function are demonstrated through four numerical examples, including a high-dimensional LSF, structural reliability analysis of the one-bay one-storey frame, minimizing the six-hump, camel-back function, and structural optimization design of a tension-compression spring. To facilitate future communication with the readers who may be interested in this research, we have casted a website. Both codes and examples can be downloaded from the webpage: https://sites.google.com/site/rasosubsim/.