Use of Multi-Fidelity Kriging Partial Least SquaresΒΆ

from smt.applications import MFKPLS,NestedLHS
import numpy as np
import otsmt
import openturns as ot
Definition of Initial data
# Construction of the DOE
# low fidelity model
def lf_function(x):

    return (
        0.5 * ((x * 6 - 2) ** 2) * np.sin((x * 6 - 2) * 2)
        + (x - 0.5) * 10.0
        - 5
    )

# high fidelity model
def hf_function(x):

    return ((x * 6 - 2) ** 2) * np.sin((x * 6 - 2) * 2)

# Problem set up
xlimits = np.array([[0.0, 1.0]])
xdoes = NestedLHS(nlevel=2, xlimits=xlimits, random_state=0)
xt_c, xt_e = xdoes(7)

# Evaluate the HF and LF functions
yt_e = hf_function(xt_e)
yt_c = lf_function(xt_c)

xv_e = ot.Sample([[0.1],[0.5]])

Out:

/usr/share/miniconda3/envs/test/lib/python3.9/site-packages/numpy/lib/function_base.py:2845: RuntimeWarning: Degrees of freedom <= 0 for slice
  c = cov(x, y, rowvar, dtype=dtype)
/usr/share/miniconda3/envs/test/lib/python3.9/site-packages/numpy/lib/function_base.py:2704: RuntimeWarning: divide by zero encountered in divide
  c *= np.true_divide(1, fact)
/usr/share/miniconda3/envs/test/lib/python3.9/site-packages/numpy/lib/function_base.py:2704: RuntimeWarning: invalid value encountered in multiply
  c *= np.true_divide(1, fact)
Training of smt model for MFKPLS
ncomp = 1
sm_mfkpls = MFKPLS(n_comp=ncomp, theta0=ncomp * [1.0])

# low-fidelity dataset names being integers from 0 to level-1
sm_mfkpls.set_training_values(xt_c, yt_c, name=0)
# high-fidelity dataset without name
sm_mfkpls.set_training_values(xt_e, yt_e)

# train the model
sm_mfkpls.train()

Out:

___________________________________________________________________________

                                  MFKPLS
___________________________________________________________________________

 Problem size

      # training points.        : 7

___________________________________________________________________________

 Training

   Training ...
   Training - done. Time (sec):  0.0541923
Creation of OpenTurns PythonFunction for prediction
otmfkpls = otsmt.smt2ot(sm_mfkpls)
otmfkplsprediction = otmfkpls.getPredictionFunction()
otmfkplspvariances = otmfkpls.getConditionalVarianceFunction()
otmfkplsgradient= otmfkpls.getPredictionDerivativesFunction()

print('Predicted values by MFKPLS:',otmfkplsprediction(xv_e))
print('Predicted variances values by MFKPLS:',otmfkplspvariances(xv_e))
print('Prediction derivatives by MFKPLS:',otmfkplsgradient(xv_e))

Out:

___________________________________________________________________________

 Evaluation

      # eval points. : 2

   Predicting ...
   Predicting - done. Time (sec):  0.0003593

   Prediction time/pt. (sec) :  0.0001796

Predicted values by MFKPLS:     [ y0        ]
0 : [ -0.656621 ]
1 : [  0.909277 ]
Predicted variances values by MFKPLS:     [ y0          ]
0 : [ 2.70638e-08 ]
1 : [ 1.99051e-09 ]
___________________________________________________________________________

 Evaluation

      # eval points. : 2

   Predicting ...
   Predicting - done. Time (sec):  0.0003285

   Prediction time/pt. (sec) :  0.0001643

Prediction derivatives by MFKPLS:     [ y0        ]
0 : [ -16.5312  ]
1 : [   5.91771 ]

Total running time of the script: ( 0 minutes 0.115 seconds)

Gallery generated by Sphinx-Gallery