# Calibration of the deflection of a tube¶

In this example, we calibrate the deflection of a tube as described in the use cases section. More precisely, we calibrate the mechanical parameters of a physical model which computes the vertical deflection of a tube and two deflection angles. This example shows how to calibrate a computer code which has several outputs.

In this example, we use Gaussian calibration method to calibrate the parametric model. Please read Gaussian calibration for more details on Bayesian Gaussian calibration. This study is relatively complicated: please read the calibration of the Chaboche mechanical model first if this is not already done.

## Variables¶

In this this model, the following list presents which variables are observed input variables, input calibrated parameters and observed output variables.

• F, E: Input. Observed.

• L, a, D, d: Input. Calibrated.

• , , : Output. Observed.

```from openturns.usecases import deflection_tube
import openturns as ot
import openturns.viewer as otv
from matplotlib import pylab as plt

ot.Log.Show(ot.Log.NONE)
```

## Create a calibration problem¶

We load the model from the use case module :

```dt = deflection_tube.DeflectionTube()
print("Inputs:", dt.model.getInputDescription())
print("Outputs:", dt.model.getOutputDescription())
```
```Inputs: [F,L,a,De,di,E]
Outputs: [Deflection,Left angle,Right angle]
```

We see that there are 6 inputs: F, L, a, De, di, E and 3 outputs: Deflection, Left angle, Right angle. In this calibration example, the variables F and E are observed inputs and the input parameters L, a, De, di are calibrated.

We create a sample out of our input distribution :

```sampleSize = 100
inputSample = dt.inputDistribution.getSample(sampleSize)
inputSample[0:5]
```
Force Length Location External diameter Internal diameter Young Modulus 0.9905769 1.5 1 0.8 0.1 195443.3 1.05256 1.5 1 0.8 0.1 198526 0.8960331 1.5 1 0.8 0.1 197851.2 1.057398 1.5 1 0.8 0.1 200036.7 1.008869 1.5 1 0.8 0.1 197215.1

We take the image of our input sample by the model :

```outputDeflection = dt.model(inputSample)
outputDeflection[0:5]
```
Deflection Left angle Right angle -7.003919e-06 -1.400784e-05 1.75098e-05 -7.326614e-06 -1.465323e-05 1.831653e-05 -6.258339e-06 -1.251668e-05 1.564585e-05 -7.304704e-06 -1.460941e-05 1.826176e-05 -7.069166e-06 -1.413833e-05 1.767292e-05

We now define the observed noise of the output. Since there are three observed outputs, there are three different standard deviations to define. Then we define a 3x3 covariance matrix of the observed outputs.

```observationNoiseSigma = [0.1e-6, 0.5e-6, 0.5e-6]
observationNoiseCovariance = ot.CovarianceMatrix(3)
for i in range(3):
observationNoiseCovariance[i, i] = observationNoiseSigma[i] ** 2
```

Finally, we define a dimension 3 normal distribution of the observed output and get a sample from it. We add this noise to the output of the model, which defines the observed output.

```noiseSigma = ot.Normal([0.0, 0.0, 0.0], observationNoiseCovariance)
sampleObservationNoise = noiseSigma.getSample(sampleSize)
observedOutput = outputDeflection + sampleObservationNoise
observedOutput[0:5]
```
Deflection Left angle Right angle -7.163228e-06 -1.410771e-05 1.753855e-05 -7.452763e-06 -1.449212e-05 1.838678e-05 -6.33502e-06 -1.256392e-05 1.626712e-05 -7.265429e-06 -1.522145e-05 1.823709e-05 -7.065494e-06 -1.457829e-05 1.76351e-05

We now extract the observed inputs from the input sample.

```observedInput = ot.Sample(sampleSize, 2)
observedInput[:, 0] = inputSample[:, 0]  # F
observedInput[:, 1] = inputSample[:, 5]  # E
observedInput.setDescription(["Force", "Young Modulus"])
observedInput[0:5]
```
Force Young Modulus 0.9905769 195443.3 1.05256 198526 0.8960331 197851.2 1.057398 200036.7 1.008869 197215.1

We would like to see how the observed output depend on the observed inputs. To do this, we use the `DrawPairs()` method.

```fullSample = ot.Sample(sampleSize, 5)
fullSample[:, 0:2] = observedInput
fullSample[:, 2:5] = observedOutput
fullSample.setDescription(["Force", "Young", "Deflection", "Left Angle", "Right Angle"])
fullSample[0:5]
```
Force Young Deflection Left Angle Right Angle 0.9905769 195443.3 -7.163228e-06 -1.410771e-05 1.753855e-05 1.05256 198526 -7.452763e-06 -1.449212e-05 1.838678e-05 0.8960331 197851.2 -6.33502e-06 -1.256392e-05 1.626712e-05 1.057398 200036.7 -7.265429e-06 -1.522145e-05 1.823709e-05 1.008869 197215.1 -7.065494e-06 -1.457829e-05 1.76351e-05

```graph = ot.VisualTest.DrawPairs(fullSample)
view = otv.View(graph, figure_kw={"figsize": (10.0, 8.0)})
```

## Setting up the calibration¶

Please consider that the input parameters L, a, De, di are calibrated. We define the initial point of the calibrated parameters: these values will be used as the mean of the prior normal distribution of the parameters. We set it with a slight difference with the true values, because, in general, we do not know the exact value (otherwise we would not use a calibration method).

```XL = 1.4  # Exact : 1.5
Xa = 1.2  # Exact : 1.0
XD = 0.7  # Exact : 0.8
Xd = 0.2  # Exact : 0.1
thetaPrior = [XL, Xa, XD, Xd]
```

We now define the standard deviation of the prior distribution of the parameters. We compute the standard deviation as 10% of the value of the parameter. This ensures that the prior normal distribution has a spread which scales correctly with the mean of the Gaussian distribution. In other words, this setting corresponds to a 10% coefficient of variation.

```sigmaXL = 0.1 * XL
sigmaXa = 0.1 * Xa
sigmaXD = 0.1 * XD
sigmaXd = 0.1 * Xd
parameterCovariance = ot.CovarianceMatrix(4)
parameterCovariance[0, 0] = sigmaXL ** 2
parameterCovariance[1, 1] = sigmaXa ** 2
parameterCovariance[2, 2] = sigmaXD ** 2
parameterCovariance[3, 3] = sigmaXd ** 2
print(parameterCovariance)
```
```[[ 0.0196 0      0      0      ]
[ 0      0.0144 0      0      ]
[ 0      0      0.0049 0      ]
[ 0      0      0      0.0004 ]]
```

In the physical model, the inputs and parameters are ordered as presented in the next table. Notice that there are no parameters in the physical model.

Index

Input variable

0

F

1

L

2

a

3

De

4

di

5

E

Index

Parameter

Table 1. Indices and names of the inputs and parameters of the physical model.

```print("Physical Model Inputs:", dt.model.getInputDescription())
print("Physical Model Parameters:", dt.model.getParameterDescription())
```
```Physical Model Inputs: [F,L,a,De,di,E]
Physical Model Parameters: []
```

In order to perform calibration, we have to define a parametric model, with observed inputs and parameters to calibrate. In order to do this, we create a `ParametricFunction` where the parameters are L, a, de, di, which have the indices 1, 2, 3 and 4 in the physical model.

Index

Input variable

0

F

1

E

Index

Parameter

0

L

1

a

2

De

3

di

Table 2. Indices and names of the inputs and parameters of the parametric model.

```calibratedIndices = [1, 2, 3, 4]  # [L, a, De, di]
calibrationFunction = ot.ParametricFunction(dt.model, calibratedIndices, thetaPrior)
print("Parametric Model Inputs:", calibrationFunction.getInputDescription())
print("Parametric Model Parameters:", calibrationFunction.getParameterDescription())
```
```Parametric Model Inputs: [F,E]
Parametric Model Parameters: [L,a,De,di]
```

In the next cells, we are going to perform a Bayesian calibration, based on Gaussian assumptions. In the Bayesian framework, we define the standard deviation of the prior Gaussian distribution of the observation errors. Although the exact value of the standard deviation of the observation errors is equal to 0.1e-6, we set slightly different values because the value of the true standard deviation of the observation error is not known in general. We will be able to check if these hypotheses are correct after calibration, using the `drawResiduals()` method.

```sigmaObservation = [0.2e-6, 0.3e-6, 0.3e-6]  # Exact : [0.1e-6, 0.5e-6, 0.5e-6]
# Set the diagonal of the covariance matrix.
errorCovariance = ot.CovarianceMatrix(3)
errorCovariance[0, 0] = sigmaObservation[0] ** 2
errorCovariance[1, 1] = sigmaObservation[1] ** 2
errorCovariance[2, 2] = sigmaObservation[2] ** 2
```
```calibrationFunction.setParameter(thetaPrior)
predictedOutput = calibrationFunction(observedInput)
predictedOutput[0:5]
```
Deflection Left angle Right angle -2.968605e-06 -9.895348e-06 1.607994e-05 -3.105378e-06 -1.035126e-05 1.68208e-05 -2.652591e-06 -8.841971e-06 1.43682e-05 -3.096092e-06 -1.032031e-05 1.67705e-05 -2.996259e-06 -9.987531e-06 1.622974e-05

## Calibration with Gaussian non linear calibration¶

We are finally able to use the `GaussianNonLinearCalibration`.

```algo = ot.GaussianNonLinearCalibration(
calibrationFunction,
observedInput,
observedOutput,
thetaPrior,
parameterCovariance,
errorCovariance,
)
```

The `run()` method launches the optimization algorithm.

```algo.run()
calibrationResult = algo.getResult()
```

## Analysis of the results¶

The `getParameterMAP()` method returns the optimized parameters.

```thetaMAP = calibrationResult.getParameterMAP()
print("theta After = ", thetaMAP)
print("theta Before = ", thetaPrior)
```
```theta After =  [1.50671,1.0088,0.801516,0.199872]
theta Before =  [1.4, 1.2, 0.7, 0.2]
```

Compute a 95% credibility interval for each marginal.

```thetaPosterior = calibrationResult.getParameterPosterior()
alpha = 0.95
dim = thetaPosterior.getDimension()
for i in range(dim):
print(thetaPosterior.getMarginal(i).computeBilateralConfidenceInterval(alpha))
```
```[1.48811, 1.52898]
[0.985314, 1.03571]
[0.797819, 0.80569]
[0.199866, 0.199877]
```

We see that the parameters are accurately estimated in this case.

Increase the default number of points in the plots. This produces smoother spiky distributions.

```ot.ResourceMap.SetAsUnsignedInteger("Distribution-DefaultPointNumber", 300)
```

In order to see how the predicted outputs first to the observed outputs, we plot the outputs depending on the inputs.

```# sphinx_gallery_thumbnail_number = 2
graph = calibrationResult.drawObservationsVsInputs()
view = otv.View(
graph,
figure_kw={"figsize": (8.0, 6.0)},
legend_kw={"bbox_to_anchor": (1.0, 1.0), "loc": "upper left"},
)
```

For all outputs, the predicted outputs of the model correspond to the observed outputs.

The next cell plots the predicted outputs depending on the observed outputs.

```graph = calibrationResult.drawObservationsVsPredictions()
view = otv.View(
graph,
figure_kw={"figsize": (12.0, 4.0)},
legend_kw={"bbox_to_anchor": (1.0, 1.0), "loc": "upper left"},
)
```

We see that, after calibration, the points are on the first diagonal of the plot: this shows that the calibration performs correctly.

In the next cell, we analyse the residuals before and after calibration. In order to clearly see these results, the next table presents the true standard deviation of the output error of observation compared to the hypothesis used in Gaussian calibration. In a practical study, the true sigma is unknown.

Output

Sigma True

Sigma hypothesis

Deflection

0.1e-6

0.2e-6

Left angle

0.5e-6

0.3e-6

Right angle

0.5e-6

0.3e-6

Table 3. For each output, the true standard deviation of the output error of observation compared to the hypothesis used in Gaussian calibration.

One of the hypotheses of the Gaussian calibration is that the observation errors are Gaussian. In order to check this hypothesis, we plot the distribution of the residuals before and after calibration. Furthermore, we plot the distribution of the observation errors, which is an hypothesis of the Gaussian calibration. Since there are three output, there are three residuals to check.

```graph = calibrationResult.drawResiduals()
view = otv.View(
graph,
figure_kw={"figsize": (13.0, 4.0)},
legend_kw={"bbox_to_anchor": (1.0, 1.0), "loc": "upper left"},
)
```

We see that the distribution of the residuals after calibration is centered on zero: this is a good point, since this shows that we have roughly equal chances to over or under predict the output when we use the calibrated model. We see that the distribution of each the residuals after calibration is relatively close to the Gaussian assumption that we used. For the first output, i.e. the deflection, the calibrated distribution of the residuals has a distribution which has spread lower than the hypothesis. This corresponds to the parameters that we used, since the true standard deviation of the errors is equal to 0.1e-6 while we used the 0.2e-6 hypothesis. The opposite happens for the left and right angle residuals: the calibrated residuals are narrower than the normal hypothesis that we used. This is because we used 0.3e-6 as the hypothesis while the true value is 0.05e-5.

Check that the results are Gaussian, using a Normal-plot.

```graph = calibrationResult.drawResidualsNormalPlot()
view = otv.View(
graph,
figure_kw={"figsize": (10.0, 4.0)},
legend_kw={"bbox_to_anchor": (1.0, 1.0), "loc": "upper left"},
)
```

Finally, we observe the prior and posterior distribution of each parameter.

```graph = calibrationResult.drawParameterDistributions()
view = otv.View(
graph,
figure_kw={"figsize": (10.0, 4.0)},
legend_kw={"bbox_to_anchor": (1.0, 1.0), "loc": "upper left"},
)
```ot.ResourceMap.Reload()