Prognostics 102: Efficient Bayesian-Based Prognostics Algorithm in MATLAB

An efficient Bayesian-based algorithm is presented for physics-based prognostics, which combines a physical model with observed health monitoring data. Unknown model parameters are estimated using the observed data, from which the remaining useful life (RUL) of the system is predicted. This paper focuses on the Bayesian method for parameter estimation of a damage degradation model where epistemic uncertainty in model parameters is reduced with the observed data. Markov-chain Monte Carlo sampling is used to generate samples from the posterior distribution, which are then propagated through the physical model to estimate the distribution of the RUL. A MATLAB script of 76 lines is included in this paper with detailed explanations. A battery degradation model and crack growth model are used to explain the process of parameter estimation, the evolution of degradation and RUL prediction. The code presented in this paper can easily be altered for different applications. This code may help beginners to understand and use Bayesian method-based prognostics.


Introduction
Structural health monitoring (SHM) [1,2] is the process of identifying damage and evaluating the safety of a system based on online and/or off-line data. It uses an array of sensors to obtain measurement data that are directly or indirectly related to damage. The statistical analysis of these measurements can help predict the future state of the system and thus improve the safety of the system. SHM can be found in a wide variety of applications such as bridges and dams, buildings, stadiums, platforms, airframes, turbines, etc. Prognostics is an extension of SHM, which is the process of estimating the time beyond which a system can no longer function to meet desired performances [3]. The time, in terms of cycles/hours, remaining to run the system before it fails is called the remaining useful life (RUL).
There are two types of prognostics methods: data-driven and physics-based approaches. The data-driven approaches are advantageous when many training data are available for a complex system, while the physics-based approaches are good when a physical model of damage degradation is available. The physics-based approach is used for prognostics in this paper with a well-defined physics model. Measured data is used to estimate model parameters, which are then used to predict the RUL.
Recently, many prognostics algorithms have been published in the literature [4][5][6][7][8]. However, many of the proposed algorithms are complex and not easily applicable. This complexity can present a serious hurdle for the beginner. In addition, using commercial programs may not be the best choice in teaching algorithms to students. As a continuation of our educational paper on prognostics algorithm [9], the objective of this paper is to explain the fundamentals of a Bayesian-based prognostics method and demonstrate how to use it using a simple MATLAB code.
The MATLAB code consists of 76 lines, which is further divided into three parts: (1) problem definition; (2) prognostics using the Bayesian method (BM); and (3) post-processing. The program is structured in such a way that the users only need to modify the problem definition part for their own application. This paper shows an example of battery degradation and crack growth models, and attempts to explain prognostics using BM with MATLAB code.
The remaining sections are organized as follows: In Section 2, the overall process of BM is explained; in Section 3, implementation of the code is explained with details using battery degradation example; and in Section 4, modification of the code for crack growth example is described, followed by conclusions in Section 5.

Methodology
In this section, a physics-based approach is explained using the procedure shown in Figure 1. The theoretical discussions in this section are mainly to help understand the MATLAB implementation in Section 3. The physics-based approach comprises of the following steps: (1) developing or identifying a physical model that describes the degradation of system health, (2) collecting data by operating the system under usage conditions and measuring degradation at a sequence of times/cycles, (3) estimating the model parameters by fitting the measured data, (4) progressing the physical model to the future times/cycles, and (5) predicting the RUL. A statistical inference technique called the Bayesian method (BM) is used in this paper to estimate the model parameters based on measured data. Many other methods, such as particle filter and Kalman filter, also use Bayesian inference to estimate the model parameters. In BM, all model parameters are estimated in the form of a joint probability density function (PDF), whose distribution can be represented using samples. Among various sampling methods, Markov-chain Monte Carlo (MCMC) algorithm is employed to draw samples from the distribution. These samples of model parameters are then substituted in the physical model to calculate the samples of RUL, from which the statistical distribution is evaluated.

Model definition
In this section, a degradation model of a battery is used to explain the physicsbased prognostics algorithm using the Bayesian method. The degradation model of crack growth will also be explained in Section 4. It is expected that the users develop a degradation model for their own application. This section explains the basic requirements of a degradation model.
In a lithium-ion battery, it is well known that the capacity of a secondary cell degrades over cycles in use. Therefore, the capacity can be used as a degradation feature. The degradation feature is an output of the degradation model that shows a monotonic trend as a function of time. The system is considered failed when the degradation feature goes beyond a threshold. In the case of a lithium-ion battery, the failure threshold is defined when the charging capacity fades by 30% of that of a pristine battery. In this paper, the C/1 capacity (capacity at a nominally rated current of 1A) of the battery is used as a degradation feature. Since the C/1 capacity is inversely proportional to the sum of the transfer resistance and the electrolyte resistance, it represents the overall performance of a battery.
Although the degradation process of a battery is complicated, a simple empirical model is available when the usage of the battery is the repetition of fully chargingdischarging cycles. In such a case, the degradation model can be written as a function of time only. Since the capacity of a battery degrades over time, the ratio of the capacity compared to that of the pristine battery is expressed by an exponential decaying model as [10] where b is the model parameter, t is the time, andỹ t; b ð Þ is the relative degradation of the C/1 capacity compared to the pristine capacity. The notation in Eq. (1) is chosen such that the degradation model depends on both the time of usage and model parameters. In general, the degradation model is either monotonically decreased (e.g., the capacity decay of a battery) or monotonically increased (e.g., crack growth).
The main goal of the physics-based prognostics is to predict the degradation behavior using the degradation model. If the model is perfect, then it can be used to find the time t EOL at the end of life from where y threshold is the failure threshold. Since the relative capacity is used as a degradation feature, the failure threshold is defined at y threshold ¼ 0:7. Let the current time be t CUR , then the RUL can be defined as In practice, however, the degradation model is not perfect in the sense that the model form, as well as the model parameters, may not be accurate. The error in the model form can be handled by introducing a model form error and identifying the error using measured data, which would be considered as the out of the scope of this paper. Interested readers can refer to Guan et al. [11].
Once the model form is accepted, the next task is to identify the accurate model parameters. In the case of the battery degradation model in Eq. (1), the parameter b needs to be identified. In most cases, the model parameters are not an intrinsic property but depend on operating conditions and environment. Therefore, these parameters can be different for different batteries and need to be identified for the specific battery of interest. In fact, the major task of physics-based prognostics is to identify the model parameters.
The model parameter b for a specific battery can be identified by measuring the capacity degradation during regular operation. The measuring process is often called health monitoring, where the degradation feature is measured over time. It is possible that the degradation feature can be monitored online. However, for the purpose of prognostics, the real-time continuous monitoring may not be necessary. Therefore, it is often suggested to collect data in a discrete set of times. Then, many different physics-based prognostics algorithms use these data to identify the model parameters so that the degradation model represents the degradation feature the best. For example, nonlinear least-squares method minimizes the error between measured data and the model prediction. Kalman filter, particle filter, and Bayesian methods are using Bayesian inference to estimate the model parameters. Different methods use different assumptions and different numerical approaches. Interested readers are referred to Kim et al. [3] for details of these methods.
If the measured data are accurate, then a small number of measured data should be good enough to estimate the model parameters. In reality, however, most measured data include noise and bias, which make the estimation process difficult. Noise is a random fluctuation of signals due to uncontrollable factors in the measurement environment, while bias is a systematic departure from the average data. If the measurement is repeated, noise can be changed, while the bias may remain the same. The bias can occur because of calibration error of the sensors, but it may also occur due to the model form error. The effect of the model form error can partially be addressed by introducing the bias in the estimation process. Bias can be added in the model as an extra term and estimated in the same way as other parameters. The distribution of estimated bias is a good indicator if the model can represent degradation data well enough. If estimated bias is widely distributed, it means model form error is large. If it is narrowly distributed and the mean is close to 0, it means the model is accurate. Since noise is random, it is important to compensate for its effect in the parameter estimation process. It is obvious that a large level of noise makes the process difficult. Therefore, it is important to keep the signal-to-noise ratio as high as possible. It is also important to understand the statistical characteristics of the noise. In this paper, it is assumed that the noise follows a Gaussian distribution with a zero mean and unknown standard deviation. On the other hand, the effect of bias will not be considered. Therefore, in addition to the unknown model parameters, it is necessary to estimate the unknown standard deviation of noise in data.
Because of noise and bias, it is often expected that a large number of data be required to estimate the model parameters accurately. In prognostics, it is often assumed that N data data are collected from a start time to the current time t ∈ t 0 ; t CUR ½ . Time does not have to be a physical time; it can also be the number of cycles of operation. Then, the model parameters are estimated using N data data, and future degradation is predicted using the degradation model with the estimated parameters. In particular, the goal is to accurately predict the end of life in Eq. (2) and the RUL in Eq. (3).
It is important to note that the data should show a significant change in the damage feature over time. In the case of crack growth in Section 4, for example, when the crack size is small, it grows very slowly. Therefore, the measurement data in an early stage do not show a significant change in the crack size. In such a case, the signal-to-noise ratio is too low and it is difficult to estimate the model parameters.
In this paper, instead of measuring the degradation of a real battery, the degradation data are generated based on an assumed true model. This has a couple of advantages. First, since the true model and its model parameters are known, it is possible to evaluate the accuracy of the estimation process and that of the RUL. It also allows us to investigate the effect of noise on the performance of prognostics algorithms. In this paper, the relative capacity data are generated based on Eq. (1) with the true model parameter b true = 0.012. It is assumed that the C/1 capacity of the battery is measured once a week up to the ninth week. In order to simulate the real measurement environment, a Gaussian noise ε $ N(0, 0.005 2 ) is added to the true data. The following MATLAB commands can be used to generate the measured data: >> time=(0:9)'; >> b=0.012; >> trueData=exp(-b*time); >> measuData=trueData+0.005*randn(10,1); Once the measurement data are generated, the true model parameters and the information of noise are not used. Table 1 and Figure 2 show the true degradation data and simulated measurement data up to the current time t CUR ¼ 9 weeks. Based on the true model, the end of life of the battery is t EOL ¼ 29:72 weeks, and thus, the true RUL should be t RUL ¼ 20:72 weeks.

Bayesian parameter estimation
Once the measurement data are available, the next step would be to estimate the model parameters. Among many parameter estimation methods, the Bayesian inference is explained in this section. In the following explanation, Θ represents the random variable of the unknown model parameter, and Y represents the random variable of degradation feature. A variable with an upper case denotes a random variable, while a variable with a lower case denotes a realization of the random variable. Bayesian inference estimates the degree of belief in a hypothesis based on collected evidence. Bayes [12] formulated the degree of belief using the identity in conditional probability: where P ΘjY ð Þis the conditional probability of Θ given Y. In the case of estimating the model parameter using measured data, the conditional probability of Θ when the probability of measured data Y is available can be written as where P Θ ð Þ is the prior probability of parameter Θ, which represents the preexisting knowledge on the parameter. P ΘjY ð Þis the posterior probability of parameter Θ after updating the prior with measurement data Y. P YjΘ ð Þis the likelihood function or the probability of obtaining data Y for a given parameter Θ. The measurement data affect the posterior probability through the likelihood function. The denominator, P(Y), is the marginal probability of Y and acts as a normalizing constant. The above equation can be used to improve the knowledge of P(Θ) when additional information P(Y) is available.
If the Bayes' theorem in Eq. (5) is going to be used for identifying unknown model parameters, it would be better to express the theorem in the form of a probability density function (PDF) [13], which is used in the present paper. Let f Θ θ ð Þ be a PDF of model parameter Θ. When there are more than one model parameters, f Θ θ ð Þ can be a joint PDF of multiple parameters. If the health monitoring measures a degradation feature Y, the measurement variability can be represented using PDF, f Y y ð Þ. Then, the conditional PDFs between Θ and Y can be related to the joint PDF and the marginal PDF, f Θ θ ð Þ and f Y y ð Þ, as It is obvious that the joint PDF can be written as f ΘY θ; y ð Þ ¼ f Θ θ ð Þf Y y ð Þ when Θ and Y are independent, and Bayesian inference cannot be used to improve the probability distribution of f Θ θ ð Þ. Similar to Eqs. (5) and (6) can be used for obtaining the Bayesian inference in the form of PDF as [14].
Since the denominator f Y y ð Þ is a constant and since the integral of f Θ θjY ¼ y ð Þis one from the property of PDF, the denominator in Eq. (7) can be considered as a normalizing constant. Similar to Eq. (5), f Θ θjY ¼ y ð Þis the posterior PDF of parameter Θ that is updated from the prior PDF f Θ θ ð Þ with the likelihood function f Y yjΘ ¼ θ ð Þ , which is the probability density value of measured data y given model parameter Θ ¼ θ. The process of updating the posterior distribution f Θ θjY ¼ y ð Þof the model parameter using the measured data y is called Bayesian inference.
The Bayesian inference can be extended to the case when many data are available. In general, it is possible that the posterior PDF can be obtained by applying all data simultaneously or by iteratively applying each data at a time. Although two approaches are theoretically equivalent, they end up numerically different methods. For example, the particle filter method uses a single measurement to update the posterior distribution, and the previous posterior distribution is used as a prior distribution for the following measurement. On the other hand, Bayesian method uses all measurement data together to build a single posterior distribution, which is used in this paper. Let us consider that y ¼ y 1 ; y 2 ; …; y N data n o is the vector or N data measurements. In such a case, the Bayes' theorem can be written as where K is the product of all marginal PDFs. However, it can be considered as a normalizing constant to make the integration of the posterior PDF to be one. It is noted that the total likelihood function is the product of the likelihood functions of individual data, which is then multiplied by the prior PDF followed by normalization to yield the posterior PDF.
In contrast to the traditional least-squares method, the Bayes' theorem can estimate not only the best values of parameters but also the uncertainty structure of the identified parameters. Since these uncertainty structures are derived from that of the prior distribution and likelihood function, the uncertainty of the posterior distribution is directly related to that of the likelihood and the prior distribution.
In the Bayesian method, it is assumed that the users know the prior distribution of model parameters and the distribution type of measurement noise. In this paper, it is assumed that the prior distribution is given as a uniform distribution with a lower-and upper-bound. It is also assumed that the measurement noise is a Gaussian distribution; that is ε $ N 0; s 2 ð Þ, where s is the standard deviation of noise. However, users can change these assumptions easily. For example, the case when noise in data follows a lognormal distribution is considered in the crack growth problem in Section 4. In most cases, since the standard deviation of noise is unknown, it should be a part of unknown model parameters. In the case of the battery model, therefore, the vector of unknown model parameters is defined as By assuming that the two model parameters are statistically independent, the prior joint PDF of the two parameters can be defined as Once the prior distribution is determined, it is necessary to build the likelihood function using the measurement data and to yield the posterior distribution shown in Eq. (8). The meaning of the likelihood function is the PDF value of obtaining the measured data y k for given model parameters θ ¼ b; s f g. Since the measured data are fixed, the likelihood function is a function of model parameters, which makes the likelihood function different from the PDF. If the model prediction is close to the measured data, then the likelihood is large, while the likelihood is small when the two values are significantly different. In order to build the likelihood, it is necessary to measure degradations at different times. Since the measured degradation data y k ; t k À Á , k ¼ 1, 2, …, N data are given at discrete times, the degradation model is also evaluated at the same discrete times as y k b ð Þ ¼ỹ t k ; b ð Þ, k ¼ 1, 2, …, N data . Since the times between the measurement and the model are synchronized,ỹ k b ð Þ is only a function of model parameter b. The measured data y k include the random noise that is governed by s, while the model predictionỹ k b ð Þ depends on b. Then, the likelihood function of the k-th measured data can be defined as As shown in Eq. (8), the likelihoods of multiple data can be multiplied to obtain the posterior distribution. With N data data, y ¼ y 1 ; y 2 ; …; y N data n o , the posterior joint PDF can be calculated by multiplying all likelihood functions and the prior PDF as where K is again a normalizing constant.

Markov chain Monte Carlo sampling
Bayesian parameter estimation in Eq. (11) shows the functional expression of the posterior joint PDF of unknown model parameters. When the prior and posterior are conjugate, the posterior distribution can be expressed in the form of a standard probability distribution. In general cases, however, the posterior distribution can be expressed as a product of complex functions, such as the posterior PDF shown in Eq. (11).
The posterior PDF is then used to calculate the degradation trend and predict the RUL. For complex nonlinear models, it is difficult to propagate uncertainty in the parameters to the degradation model. Instead, samples of model parameters are generated from the posterior distribution, and the degradation model with the threshold in Eq. (2) is used to propagate these samples to calculate the samples of the end of life, and thus, the samples of the RUL in Eq. (3). Therefore, it is important to generate samples that follow the posterior distribution of parameters.
In general, the inverse cumulative distribution function (CDF) method is the easiest way of generating samples from a non-standard probability distribution, but it requires the functional expression of CDF, not PDF. For practical engineering applications, it is likely that the posterior PDF may be different from a standard probability distribution, or the posterior PDF is complicated due to the complex correlation structures between parameters. In such a case, sampling-based methods can be used to generate samples of parameters. There are many sampling methods, such as the grid approximation [15], rejection sampling [16], importance sampling [17], and the Markov Chain Monte Carlo (MCMC) method [18]. In this paper, the MCMC method using the Metropolis-Hastings (MH) algorithm is employed. MCMC is a simulation technique used to estimate quantities of interest by sampling consecutive random variables wherein the future state depends only on the current state [19].
The MCMC sampling method uses a Markov chain model in a random walk, where the distribution of the next sample depends only on the current sample (see Figure 3). As the algorithm generates more and more samples, the samples more closely approximate the posterior PDF. Specifically, Starting with an arbitrary initial sample (current sample), a new candidate sample is drawn from a proposal distribution centered at the current sample. In this paper, a uniformly distributed proposal distribution is used. Therefore, it is expected that the users provide the initial sample of parameters and the width of the proposal distribution. At i-th iteration, it is expected that the current sample θ iÀ1 ð Þ is available, and the new candidate sample θ * is drawn from the following proposal distribution that is uniformly distributed centered at θ iÀ1 ð Þ : where w is the user-provided width of the proposal distribution. It is noted that the proposal distribution is symmetric; that is g θ * ; jθ iÀ1 ð Þ Once the candidate sample is generated, it is either accepted as a new sample or rejected based on an acceptance criterion. When accepted, the candidate sample is added to a new sample and used in the next iteration. When rejected, the candidate sample is discarded, and the current sample is reused in the next iteration. In the original MH algorithm, it is suggested to use a function that is proportional to the posterior distribution for the acceptance/rejection test. In this paper, however, the posterior distribution is directly used as its evaluation is not computationally expensive. Since the proposal distribution is symmetric, the following acceptance ratio can be defined: The acceptance ratio compares the posterior probability of the new candidate sample against that of the current sample. If the candidate sample has a higher probability than that of the current sample; i.e., Q θ iÀ1 ð Þ ; θ * À Á >1, then it is always accepted as a new sample. When 0 < Q θ iÀ1 ð Þ ; θ * À Á < 1; that is, the probability of the candidate sample is lower than that of the current sample, the acceptance is determined based on the ratio. A high acceptance ratio has a more probability to be accepted, while a low ratio is occasionally accepted. This can be achieved by generating a sample from a uniform distribution, u $ U 0; 1 ½ , and the candidate sample is accepted if u < Q θ iÀ1 ð Þ ; θ * À Á ; otherwise, it is rejected. Intuitively, this is why this algorithm works, and returns samples that follow the desired distribution f Θ θjy À Á . In Figure 3, two dashed circles mean that these candidate samples are not selected according to the criterion. In such a case, the current sample is selected again. This process is repeated as many times as necessary until a sufficient number of samples are obtained. Figure 4 summarizes the MCMC sampling procedure using the MH algorithm.
The performance of MCMC sampling depends on the initial sample and the selection of the proposal distribution. A too-narrow proposal distribution can yield destabilization by not fully covering the posterior distribution, while a too-wide distribution can yield many duplications in sampling result by not accepting new samples. In addition, if the initial sample is located far away from the posterior distribution, many iterations (samples) will be required to converge to the posterior distribution. To prevent the effect of inaccurate initial samples, an initial portion of the samples can be discarded in estimating the posterior distribution, which is called the burn-in. In this paper, the first 20 percent of the samples are discarded as a burn-in.

Prognostics
Once the samples of parameters are obtained based on the posterior distribution, the future damage state and the RUL can be predicted by substituting the samples of parameters in the degradation model and estimating the RUL using Eqs. Instead, in this paper, the degradation model is evaluated at a set of discrete future times, and then, the end of life is calculated using a simple interpolation. More specifically, let the set of discrete times is defined as Then, the measurement data are available between t 0 and t CUR . Bayesian parameter estimation in the previous section uses the measurement data between t 0 and t CUR to estimate the posterior PDF of model parameters. Using the estimated model parameters, the degradations in the future times between t CUR and t end are calculated in the prediction stage. If two consecutive degradations cross the threshold; that is,ỹ then t EOL exists between t k and t kþ1 , which can be found by a simple linear interpolation. When the set of futures times do not include the end of life, it can extrapolate based on the trend of data. It is also possible that the degradation model never reaches the threshold; that is, the system has an infinite of life. In such a case, the sample is deleted from the calculation. Once the samples of the end of life are obtained, the samples of RUL can be calculated using Eq. (3).
Once the samples of RUL are available, the confidence interval and/or the prediction interval is used to evaluate the accuracy or precision of the RUL. The confidence interval represents how good the RUL is. Therefore, the confidence interval of 95% means that the true RUL will be within this interval with the probability of 95%. That is, the confidence interval tells us about the likely location of the true RUL. In the case of RUL samples, the 95% confidence interval can be calculated by taking the lower 2.5 percentile and the upper 2.5 percentile from the samples. On the other hand, the prediction interval shows the possible location of the next sample. Knowing that the next sample will have additional randomness from the predicted RUL, the prediction interval is calculated by adding additional randomness to the data. In practice, the RUL estimation is important for scheduling maintenance. Therefore, only the lower confidence/prediction bound is of interest in the practical application. Figure 5 shows a representative result of prognostics, which shows the statistical distribution and the confidence interval of the RUL.

MATLAB implementation
In this section, MATLAB implementation of prognostics using the Bayesian method is discussed. In the following explanation, 'line' or 'lines' in a parenthesis indicated the number of the line of the code in Appendix. The code is divided into three parts: (1) Problem Definition (lines 2-15, 65-67) (2) Bayesian parameter estimation and MCMC sampling (lines 16-29, 60-76) (3) Post-processing for displaying results (lines 40-57). Only the Problem Definition part needs to be changed for different applications. Detailed explanations are given in the subsequent sections with an example of battery degradation.
It is expected that the MATLAB script is saved as a file with the name of 'BM.m', which has two input arguments, para0 and weigh (line 1). The first argument, para0, is the initial sample of model parameters, and weigh is the width w of the proposal distribution in Eq. (12). The size of each array should be the same as the number of model parameters. The following is an example of calling the code in the command window of MATLAB: Since the convergence and accuracy depend on these two variables, it is suggested to try with different values. Since the users know the prior distribution of the model parameters, it is a good practice to start with the mean of the prior distribution as an initial sample. If the code ran successfully, it will return the samples of model parameters in the samplResul array and will generate two plots. The first plot is the trace of MCMC samples, and the second plot is the histogram of the RUL.

Problem definition (lines 2-15, 65-67)
The problem definition means defining the degradation equation using model parameters and time/cycle. All known parameters, as well as the initial estimate of unknown parameters, parameter names, and model data, need to be defined. The problem definition consists of two parts: parameter definition and model definition. For parameter definition (lines 2-15), 'Battery' is used as a WorkName (line 3). The capacity is measured every week, so TimeUnit is 'weeks' (line 4). In line 5, time is an array of discrete times in the units of TimeUnit. Measurements and predictions will be done at these times. The relative C/1 capacity data is stored as measuData (lines 6-7), which has 10 weeks of measurement; that is, N data ¼ 10 (k1 in line 18). These data correspond to the first 10 times in time array. Since time starts from 0, the 10th time corresponds to 9 weeks, which is the current time; that is, t CUR ¼ 9 week. The degradation will be predicted for future times that are from 10 to 50 weeks. The measurement data are generated using the MATLAB script in Section 2.1. It is noted that due to random noise, the users may experience different realizations of measurement data.
In line 8, the degradation threshold y threshold is defined using variable 'thres' with the value of 0.7. ParamName in line 9 is the name of model parameters: model parameter 'b' and standard deviation of noise 's'. Since the parameter name will actually be used in the model, it is important to use the actual name of variables here. The number of parameters is stored in the variable 'p' in line 17. It is noted that the last parameter should be the standard deviation of noise 's'. prioDisPar stores the information of the prior distribution of model parameters, as given in Eq. (9). When a uniform prior distribution is used, each row contains the lowerand upper-bounds of the distribution.
During MCMC sampling, the number of samples Ns is set to 5000 (line 12). Since the sampling process takes many iterations to converge, the initial 20% of the samples are discarded in calculating the posterior distribution by command burnIn = 0.2 (line 13). In order to keep 5000 samples, Ns/(1-burnIn) = 6250 samples (line 21) are generated first, and then, nBurn = 1250 samples (line 30) are discarded.
Since the RUL is represented by Ns samples, the confidence interval or the prediction interval is often used to support the decision-making process. signiLevel (line 14) is the significance level of this interval in percentage. When signiLevel = 5, the code will return the lower 5 percentile, median, and the upper 5 percentile. Following is the sample output from BM.m: MCMC sampling using the MH algorithm starts with the initial sample that is provided by the users (line 19) and the value of the posterior PDF (line 20). In the loop of MCMC sampling (lines 21-29), a candidate sample is randomly generated from a uniform distribution, centered at the current sample and the width of AEw (line 22). The value of posterior PDF for the candidate sample is also calculated (line 23). If the acceptance ratio in Eq. (13) is greater than a randomly generated number u, then the candidate sample is accepted as a new sample (lines 25-26). Otherwise, the current sample and its posterior PDF are kept as a new sample and PDF. Once the MCMC sampling loop is over, the first 20% of the samples are discarded as a burn-in process (line 31). At the end of Bayesian parameter estimation, samplResul array contains Ns samples of model parameters.

Remaining useful life prediction (lines 32-39)
Once the samples of model parameters are obtained based on the posterior distribution, they can be used to find the RUL, which is the time when the degradation prediction reaches the threshold. First, for all samples in samplResul, the degradation is predicted in the future times between t CUR ¼ k1 and t end . The option 'pchip' in interp1 uses a shape-preserving piecewise cubic interpolation, which preserves C 1 -continuity.

Postprocessing (lines 40-58)
In the postprocessing stage, the results given in samples are interpreted in terms of statistical quantities or in the form of graphs. First, in the RUL array, those components that have an infinite life should be removed (line 41). Then, the confidence intervals of [5%, median, 95%] are calculated from the RUL array and stored in rulPerce (line 42-43).
The MATLAB code plots two figures. The first figure plots the trace of MCMC samples (lines 45-50) as shown in Figure 6(a). This trace shows the quality of MCMC samples. If samples are distributed randomly and symmetrically around the mean with a constant bound, then it means that the samples are stabilized and well represent the posterior distribution. If the trace of samples shows an irregular behavior as shown in Figure 6(b), the samples may not represent the posterior PDF properly. This can happen when the initial sample was far away from the mean and when the width of the proposal distribution is too narrow or too wide.
The second plot is the histogram of RUL (lines 51-53) as shown in Figure 7. At the end of the code, the confidence intervals of [5%, median, 95%] are printed on the command window (lines 54-56). All variables are saved in the computer file so that they can be loaded to the memory for further analysis (line 57). The name of the saved database is "WorkName at t CUR .mat". For example, in the battery case, the saved database name is "Battery at 9.mat".
Although the MATLAB code plots two figures, it is possible that the users can plot different figures using the saved database. After calling the BM.m function, the saved database has to be loaded to the memory using the following commands: clear; clc; load('Battery at 9.mat')  In addition to the trace of samples shown in Figure 6, it is possible to plot the histogram of model parameters using the same samples. The following commands plot the histograms of all model parameters.
figure; % histogram of parameters for j=1:p; subplot(1,p,j); hist(samplResul(j,:),30); end When the true degradation model is known, it is possible to compare the predicted degradation with the true one. The following MATLAB script plots the median and confidence intervals of the predicted degradation along with the true degradation and the threshold. The degradation curves up to 50 weeks are shown in Figure 8 for the battery example. The true degradation with b ¼ 0:012 is shown with the black curve. The red curves show 5, 50 (median) and 95 percentiles of the predicted degradation, which are caused by signiLevel = 5 (line 14). The plot also shows the threshold (green line) and measurement data (blue asterisk marks). Based on the true model, the end of life of the battery is t EOL ¼ 29:72 weeks, and thus, the true RUL should be t RUL ¼ 20:72 weeks. The prediction shows that the median of RUL is 20.38 weeks, which is close to the true RUL. In addition, the 90% confidence interval is about 3.4 weeks; that is, the uncertainty in the prediction is about 17%.

Application to crack growth prognostics
The code can be modified easily by the users for various applications. In this section, an example of crack growth is used to explain how the MATLAB code BM.m can be modified.

Model definition: crack growth
In fatigue crack growth, the failure criterion is given in terms of the crack size. Therefore, it would be appropriate to use the size of crack as a degradation feature. In this case, the degradation feature monotonically increases, while it was monotonically decreased for the battery example. Assuming that a through-the-thickness center crack exists in an infinite plate under mode-I loading condition, the rate of fatigue crack growth can be expressed using the Paris-Erdogan model as where a is the half crack size, N is the number of cycles, m and C are model parameters, ΔK ¼ Δσ ffiffiffiffiffi ffi πa p is the range of stress intensity factor, and Δσ is the stress range. It is assumed that time is the number of fatigue loading cycles. For the consistent notation, the crack size and cycles are replaced withỹ ¼ a and t ¼ N in the following explanation. Since the degradation model requires the crack size as a function of time and model parameters, Eq. (16) can be integrated to obtain the following degradation model: The system is under fatigue loading with the range of stress being Δσ ¼ 75MPa at each cycle. It is assumed that the health monitoring is performed every 50 cycles to measure the crack size y k until the current time t CUR ¼ 1, 200 cycles, and the initial size of the crack is a 0 ¼ 0:01m. Similar to the battery degradation example, the measurement data are simulated by adding random noise to the true crack size. First, the true crack size data are generated at every 50 cycles using Eq. (17) with m true ¼ 3:8 and C true ¼ 1:5 Â 10 À10 . The measured crack size data are then generated by adding Gaussian noise ε $ N 0; s 2 ð Þ, s ¼ 0:0005 m to the true crack sizes. The measured crack size data are used to identify three model parameters, θ ¼ m; ln C ð Þ; s f g . In the Paris-Erdogan model, the y-intercept C is very small but changes its magnitude by several orders. Therefore, it would be better to identify logarithm of C. For RUL calculation, the critical crack size is determined as 0.043 m.
For the Bayesian method, it is necessary to define the prior distribution and the likelihood function. In the battery example, it was assumed that noise in data follows a normal distribution. However, when the distribution type of measurement noise is unknown, it is possible that the likelihood function might be different from the true noise distribution. The same is true for the prior/initial distribution. Therefore, it would be a good exercise to study the effect of different distribution types by changing the MATLAB codes. In this example, the lognormal distribution is employed for the likelihood function as: Initially, a fatigue crack grows slowly and then grows rapidly just before becoming unstable. The crack growth model in Eq. (17) is only valid when the crack growth is stable. Normally the threshold y threshold is set before the crack becomes unstable. When the crack becomes unstable, Eq. (17) yield a complex number. Therefore, the last line of the model definition code identifies if a prediction yields a complex number and converts it to a real number by ignoring the complex part.

Results
Similar to the battery example, the MATLAB code can be used to plot the trace of MCMC sampling and the histogram of model parameters and RUL. Using a similar code provided in Section 3.4, the degradation trend could also be plotted. For example, Figure 5 shows the predicted degradation with the true degradation. Even if the median (1553 cycles) has a relatively large error with the true RUL (1709 cycles), the 95% confidence interval covers the true RUL.
An important information that was not discussed before is the correlation between model parameters. It is well known that the Paris-Erdogan model parameters, m and C, are strongly correlated [20]. Therefore, it would be beneficial to plot the MCMC samples in the parameter space. The following MATLAB script plots the MCMC samples of the parameters.  Figure 9 shows the MCMC samples in the parameter space along with the exact value of the parameters (star marker). The figure also shows the contour of the joint posterior PDF based on the grid method. It can be observed that the MCMC samples represent the joint posterior PDF well, and the joint PDF covers the true parameter values. However, due to a strong correlation between the two Paris-Erdogan model parameters, the joint PDF shows a narrow but long tail. Any combination of model parameters along the correlation line may yield a similar damage growth trend.