kriging
Kriging
Bases: surrogates
Source code in spotPython/build/kriging.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 |
|
__init__(noise=False, cod_type='norm', var_type=['num'], use_cod_y=False, name='kriging', seed=124, model_optimizer=None, model_fun_evals=None, min_theta=-3, max_theta=2, n_theta=1, n_p=1, optim_p=False, log_level=50, **kwargs)
Kriging surrogate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
noise |
bool
|
use regression instead of interpolation kriging. Defaults to "False". |
False
|
cod_type |
bool
|
normalize or standardize X and values. Can be None, "norm", or "std". Defaults to "norm". |
'norm'
|
var_type |
str
|
variable type. Can be either |
['num']
|
use_cod_y |
bool
|
use coded y values (instead of natural one). Defaults to |
False
|
name |
str
|
Surrogate name. Defaults to |
'kriging'
|
seed |
int
|
Random seed. Defaults to |
124
|
model_optimizer |
object
|
Optimizer on the surrogate. If |
None
|
model_fun_evals |
int
|
Number of iterations used by the optimizer on the surrogate. |
None
|
min_theta |
float
|
min log10 theta value. Defaults to |
-3
|
max_theta |
float
|
max log10 theta value. Defaults to |
2
|
n_theta |
int
|
number of theta values. Defaults to |
1
|
n_p |
int
|
number of p values. Defaults to |
1
|
optim_p |
bool
|
Determines whether |
False
|
log_level |
int
|
logging level, e.g., |
50
|
Attributes:
Name | Type | Description |
---|---|---|
nat_range_X |
list
|
List of X natural ranges. |
nat_range_y |
list
|
List of y nat ranges. |
noise |
bool
|
noisy objective function. Default: False. If |
var_type |
str
|
variable type. Can be either |
num_mask |
array
|
array of bool variables. |
factor_mask |
array
|
array of factor variables. |
int_mask |
array
|
array of integer variables. |
ordered_mask |
array
|
array of ordered variables. |
name |
str
|
Surrogate name |
seed |
int
|
Random seed. |
use_cod_y |
bool
|
Use coded y values. |
sigma |
float
|
Kriging sigma. |
gen |
method
|
Design generator, e.g., spotPython.design.spacefilling.spacefilling. |
min_theta |
float
|
min log10 theta value. Defaults: -6. |
max_theta |
float
|
max log10 theta value. Defaults: 3. |
min_p |
float
|
min p value. Default: 1. |
max_p |
float
|
max p value. Default: 2. |
Examples:
Surrogate of the x*sin(x) function. See: scikit-learn
>>> from spotPython.build.kriging import Kriging
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> rng = np.random.RandomState(1)
>>> X = linspace(start=0, stop=10, num=1_000).reshape(-1, 1)
>>> y = np.squeeze(X * np.sin(X))
>>> training_indices = rng.choice(arange(y.size), size=6, replace=False)
>>> X_train, y_train = X[training_indices], y[training_indices]
>>> S = Kriging(name='kriging', seed=124)
>>> S.fit(X_train, y_train)
>>> mean_prediction, std_prediction = S.predict(X)
>>> plt.plot(X, y, label=r"$f(x)$", linestyle="dotted")
>>> plt.scatter(X_train, y_train, label="Observations")
>>> plt.plot(X, mean_prediction, label="Mean prediction")
>>> plt.fill_between(
X.ravel(),
mean_prediction - 1.96 * std_prediction,
mean_prediction + 1.96 * std_prediction,
alpha=0.5,
label=r"95% confidence interval",
)
>>> plt.legend()
>>> plt.xlabel("$x$")
>>> plt.ylabel("$f(x)$")
>>> _ = plt.title("Gaussian process regression on noise-free dataset")
Source code in spotPython/build/kriging.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
|
build_Psi()
New construction (rebuild to reflect new data or a change in hyperparameters) of the (nxn) correlation matrix Psi as described in [Forr08a, p.57].
Note
Method uses theta
, p
, and coded X
values.
Source code in spotPython/build/kriging.py
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
|
build_U(scipy=True)
Cholesky factorization of Psi as U as described in [Forr08a, p.57].
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scipy |
bool
|
Use |
True
|
Source code in spotPython/build/kriging.py
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 |
|
build_psi_vec(cod_x)
Build the psi vector. Needed by predict_cod
, predict_err_coded
,
regression_predict_coded
. Modifies self.psi
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cod_x |
array
|
point to calculate psi |
required |
Source code in spotPython/build/kriging.py
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 |
|
calculate_mean_MSE(n_samples=200, points=None)
This function calculates the mean MSE metric of the model by evaluating MSE at a number of points.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_samples |
integer
|
Number of points to sample the mean squared error at. Ignored if the points argument is specified. |
200
|
points |
array
|
an array of points to sample the model at. |
None
|
Returns:
Type | Description |
---|---|
float
|
the mean value of MSE and the standard deviation of the MSE points |
Source code in spotPython/build/kriging.py
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 |
|
cod_to_nat_x(cod_X)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cod_X |
array
|
An array representing one point (self.k long) in normalized (coded) units. |
required |
Returns:
Type | Description |
---|---|
array
|
An array of natural (physical or real world) units. |
Source code in spotPython/build/kriging.py
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 |
|
cod_to_nat_y(cod_y)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cod_y |
array
|
A normalized array of coded (model) units in the range of [0,1]. |
required |
Returns:
Type | Description |
---|---|
array
|
An array of observed values in real-world units. |
Source code in spotPython/build/kriging.py
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 |
|
exp_imp(y0, s0)
Returns the expected improvement for y0 and error s0 (in coded units).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y0 |
float
|
function value (in coded units) |
required |
s0 |
float
|
error |
required |
Returns:
Type | Description |
---|---|
float
|
The expected improvement value. |
Source code in spotPython/build/kriging.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
|
extract_from_bounds(new_theta_p_Lambda)
Extract theta
, p
, and Lambda
from bounds. The kriging object stores
theta
as an array, p
as an array, and Lambda
as a float.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_theta_p_Lambda |
numpy.array
|
1d-array with theta, p, and Lambda values. Order is important. |
required |
Source code in spotPython/build/kriging.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
|
fit(nat_X, nat_y)
The function fits the hyperparameters (theta
, p
, Lambda
) of the Kriging model, i.e.,
the following internal values are computed:
theta
,p
, andLambda
values via optimization of the functionfun_likelihood()
.- Correlation matrix
Psi
viarebuildPsi()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nat_X |
array
|
sample points |
required |
nat_y |
array
|
function values |
required |
Returns:
Name | Type | Description |
---|---|---|
surrogate |
object
|
Fitted estimator. |
Attributes:
Name | Type | Description |
---|---|---|
theta |
numpy.ndarray
|
Kriging theta values. Shape (k,). |
p |
numpy.ndarray
|
Kriging p values. Shape (k,). |
LnDetPsi |
numpy.float64
|
Determinant Psi matrix. |
Psi |
numpy.matrix
|
Correlation matrix Psi. Shape (n,n). |
psi |
numpy.ndarray
|
psi vector. Shape (n,). |
one |
numpy.ndarray
|
vector of ones. Shape (n,). |
mu |
numpy.float64
|
Kriging expected mean value mu. |
U |
numpy.matrix
|
Kriging U matrix, Cholesky decomposition. Shape (n,n). |
SigmaSqr |
numpy.float64
|
Sigma squared value. |
Lambda |
float
|
lambda noise value. |
Source code in spotPython/build/kriging.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
|
fun_likelihood(new_theta_p_Lambda)
Compute log likelihood for a set of hyperparameters (theta, p, Lambda). Performs the following steps:
- Build Psi via
build_Psi()
andbuild_U()
. - Compute negLnLikelihood via `likelihood()
- If successful, the return
negLnLike
value, otherwise a penalty value (pen_val
).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_theta_p_Lambda |
array
|
|
required |
Returns:
Type | Description |
---|---|
float
|
negLnLike, th negative log likelihood of the surface at the hyperparameters specified. |
Source code in spotPython/build/kriging.py
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
|
likelihood()
Calculates the negative of the concentrated log-likelihood. Implementation of (2.32) in [Forr08a]. See also function krigingLikelihood() in spot.
Note
build_Psi
and build_U
should be called first.
Modifies
mu
,
SigmaSqr
,
LnDetPsi
, and
negLnLike
, concentrated log-likelihood *-1 for minimizing
Source code in spotPython/build/kriging.py
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 |
|
nat_to_cod_init()
Determine max and min of each dimension and normalize that axis to a range of [0,1].
Called when 1) surrogate is initialized and 2) new points arrive, i.e., suggested
by the surrogate as infill points.
This method calls nat_to_cod_x
and nat_to_cod_y
and updates the ranges nat_range_X
and
nat_range_y
.
Source code in spotPython/build/kriging.py
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 |
|
nat_to_cod_x(nat_X)
Normalize one point (row) of nat_X array to [0,1]. The internal nat_range_X values are not updated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nat_X |
array
|
An array representing one points (self.k long) in natural (physical or real world) units. |
required |
Returns:
Type | Description |
---|---|
array
|
An array of coded values in the range of [0,1] for each dimension. |
Source code in spotPython/build/kriging.py
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 |
|
nat_to_cod_y(nat_y)
Normalize natural y values to [0,1].
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nat_y |
array
|
An array of observed values in natural (real-world) units. |
required |
Returns:
Type | Description |
---|---|
array
|
A normalized array of coded (model) units in the range of [0,1]. |
Source code in spotPython/build/kriging.py
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 |
|
plot(show=True)
This function plots 1d and 2d surrogates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
show |
boolean
|
If |
True
|
Source code in spotPython/build/kriging.py
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 |
|
predict(nat_X, nat=True, return_val='y')
This function returns the prediction (in natural units) of the surrogate at the natural coordinates of X.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nat_X |
array
|
Design variable to evaluate in natural units. |
required |
nat |
bool
|
argument |
True
|
return_val |
string
|
whether |
'y'
|
Returns:
Type | Description |
---|---|
float
|
The predicted value in natural units. |
float
|
predicted error |
float
|
expected improvement |
Source code in spotPython/build/kriging.py
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 |
|
predict_coded(cod_x)
Kriging prediction of one point in the coded units as described in (2.20) in [Forr08a]. The error is returned as well. See also [Forr08a, p.60].
Note
self.mu
and self.SigmaSqr
are computed in likelihood
, not here.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cod_x |
array
|
point in coded units to make prediction at |
required |
Returns:
Type | Description |
---|---|
float
|
predicted value in coded units. |
float
|
predicted error. |
Source code in spotPython/build/kriging.py
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 |
|
set_de_bounds()
Determine search bounds for model_optimizer, e.g., differential evolution.
Source code in spotPython/build/kriging.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
|
weighted_exp_imp(cod_x, w)
Weighted expected improvement.
References
[Sobester et al. 2005].
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cod_x |
array
|
A coded design vector. |
required |
w |
float
|
weight |
required |
Returns:
Type | Description |
---|---|
float
|
weighted expected improvement. |
Source code in spotPython/build/kriging.py
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 |
|