-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Delete src/UQpy/surrogates/kriging directory"
This reverts commit a79da94.
- Loading branch information
Showing
17 changed files
with
681 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from UQpy.surrogates.kriging.Kriging import Kriging | ||
|
||
from UQpy.surrogates.kriging.regression_models import * | ||
from UQpy.surrogates.kriging.correlation_models import * |
28 changes: 28 additions & 0 deletions
28
src/UQpy/surrogates/kriging/correlation_models/CubicCorrelation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from UQpy.surrogates.kriging.correlation_models.baseclass.Correlation import * | ||
|
||
|
||
class CubicCorrelation(Correlation): | ||
def c(self, x, s, params, dt=False, dx=False): | ||
zeta_matrix, dtheta_derivs, dx_derivs = Correlation.derivatives( | ||
x_=x, s_=s, params=params | ||
) | ||
# Initial matrices containing derivates for all values in array. Note since | ||
# dtheta_s and dx_s already accounted for where derivative should be zero, all | ||
# that must be done is multiplying the |dij| or thetaj matrix on top of a | ||
# matrix of derivates w.r.t zeta (in this case, dzeta = -6zeta+6zeta**2) | ||
drdt = (-6 * zeta_matrix + 6 * zeta_matrix ** 2) * dtheta_derivs | ||
drdx = (-6 * zeta_matrix + 6 * zeta_matrix ** 2) * dx_derivs | ||
# Also, create matrix for values of equation, 1 - 3zeta**2 + 2zeta**3, for loop | ||
zeta_function_cubic = 1 - 3 * zeta_matrix ** 2 + 2 * zeta_matrix ** 3 | ||
rx = np.prod(zeta_function_cubic, 2) | ||
if dt: | ||
# Same as previous example, loop over zeta matrix by shifting index | ||
for i in range(len(params) - 1): | ||
drdt = drdt * np.roll(zeta_function_cubic, i + 1, axis=2) | ||
return rx, drdt | ||
if dx: | ||
# Same as previous example, loop over zeta matrix by shifting index | ||
for i in range(len(params) - 1): | ||
drdx = drdx * np.roll(zeta_function_cubic, i + 1, axis=2) | ||
return rx, drdx | ||
return rx |
20 changes: 20 additions & 0 deletions
20
src/UQpy/surrogates/kriging/correlation_models/ExponentialCorrelation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from UQpy.surrogates.kriging.correlation_models.baseclass.Correlation import * | ||
|
||
|
||
class ExponentialCorrelation(Correlation): | ||
def c(self, x, s, params, dt=False, dx=False): | ||
stack = Correlation.check_samples_and_return_stack(x, s) | ||
rx = np.exp(np.sum(-params * abs(stack), axis=2)) | ||
if dt: | ||
drdt = -abs(stack) * np.transpose( | ||
np.tile(rx, (np.size(x, 1), 1, 1)), (1, 2, 0) | ||
) | ||
return rx, drdt | ||
if dx: | ||
drdx = ( | ||
-params | ||
* np.sign(stack) | ||
* np.transpose(np.tile(rx, (np.size(x, 1), 1, 1)), (1, 2, 0)) | ||
) | ||
return rx, drdx | ||
return rx |
21 changes: 21 additions & 0 deletions
21
src/UQpy/surrogates/kriging/correlation_models/GaussianCorrelation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from UQpy.surrogates.kriging.correlation_models.baseclass.Correlation import * | ||
|
||
|
||
class GaussianCorrelation(Correlation): | ||
def c(self, x, s, params, dt=False, dx=False): | ||
stack = Correlation.check_samples_and_return_stack(x, s) | ||
rx = np.exp(np.sum(-params * (stack ** 2), axis=2)) | ||
if dt: | ||
drdt = -(stack ** 2) * np.transpose( | ||
np.tile(rx, (np.size(x, 1), 1, 1)), (1, 2, 0) | ||
) | ||
return rx, drdt | ||
if dx: | ||
drdx = ( | ||
-2 | ||
* params | ||
* stack | ||
* np.transpose(np.tile(rx, (np.size(x, 1), 1, 1)), (1, 2, 0)) | ||
) | ||
return rx, drdx | ||
return rx |
35 changes: 35 additions & 0 deletions
35
src/UQpy/surrogates/kriging/correlation_models/LinearCorrelation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from UQpy.surrogates.kriging.correlation_models.baseclass.Correlation import * | ||
|
||
|
||
class LinearCorrelation(Correlation): | ||
def c(self, x, s, params, dt=False, dx=False): | ||
stack = Correlation.check_samples_and_return_stack(x, s) | ||
# Taking stack and turning each d value into 1-theta*dij | ||
after_parameters = 1 - params * abs(stack) | ||
# Define matrix of zeros to compare against (not necessary to be defined separately, | ||
# but the line is bulky if this isn't defined first, and it is used more than once) | ||
comp_zero = np.zeros((np.size(x, 0), np.size(s, 0), np.size(s, 1))) | ||
# Compute matrix of max{0,1-theta*d} | ||
max_matrix = np.maximum(after_parameters, comp_zero) | ||
rx = np.prod(max_matrix, 2) | ||
# Create matrix that has 1s where max_matrix is nonzero | ||
# -Essentially, this acts as a way to store the indices of where the values are nonzero | ||
ones_and_zeros = max_matrix.astype(bool).astype(int) | ||
# Set initial derivatives as if all were positive | ||
first_dtheta = -abs(stack) | ||
first_dx = np.negative(params) * np.sign(stack) | ||
# Multiply derivs by ones_and_zeros...this will set the values where the | ||
# derivative should be zero to zero, and keep all other values the same | ||
drdt = np.multiply(first_dtheta, ones_and_zeros) | ||
drdx = np.multiply(first_dx, ones_and_zeros) | ||
if dt: | ||
# Loop over parameters, shifting max_matrix and multiplying over derivative matrix with each iter | ||
for i in range(len(params) - 1): | ||
drdt = drdt * np.roll(max_matrix, i + 1, axis=2) | ||
return rx, drdt | ||
if dx: | ||
# Loop over parameters, shifting max_matrix and multiplying over derivative matrix with each iter | ||
for i in range(len(params) - 1): | ||
drdx = drdx * np.roll(max_matrix, i + 1, axis=2) | ||
return rx, drdx | ||
return rx |
28 changes: 28 additions & 0 deletions
28
src/UQpy/surrogates/kriging/correlation_models/SphericalCorrelation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from UQpy.surrogates.kriging.correlation_models.baseclass.Correlation import * | ||
|
||
|
||
class SphericalCorrelation(Correlation): | ||
def c(self, x, s, params, dt=False, dx=False): | ||
zeta_matrix, dtheta_derivs, dx_derivs = Correlation.derivatives( | ||
x_=x, s_=s, params=params | ||
) | ||
# Initial matrices containing derivates for all values in array. Note since | ||
# dtheta_s and dx_s already accounted for where derivative should be zero, all | ||
# that must be done is multiplying the |dij| or thetaj matrix on top of a | ||
# matrix of derivates w.r.t zeta (in this case, dzeta = -1.5+1.5zeta**2) | ||
drdt = (-1.5 + 1.5 * zeta_matrix ** 2) * dtheta_derivs | ||
drdx = (-1.5 + 1.5 * zeta_matrix ** 2) * dx_derivs | ||
# Also, create matrix for values of equation, 1 - 1.5zeta + 0.5zeta**3, for loop | ||
zeta_function = 1 - 1.5 * zeta_matrix + 0.5 * zeta_matrix ** 3 | ||
rx = np.prod(zeta_function, 2) | ||
if dt: | ||
# Same as previous example, loop over zeta matrix by shifting index | ||
for i in range(len(params) - 1): | ||
drdt = drdt * np.roll(zeta_function, i + 1, axis=2) | ||
return rx, drdt | ||
if dx: | ||
# Same as previous example, loop over zeta matrix by shifting index | ||
for i in range(len(params) - 1): | ||
drdx = drdx * np.roll(zeta_function, i + 1, axis=2) | ||
return rx, drdx | ||
return rx |
58 changes: 58 additions & 0 deletions
58
src/UQpy/surrogates/kriging/correlation_models/SplineCorrelation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from UQpy.surrogates.kriging.correlation_models.baseclass.Correlation import * | ||
|
||
|
||
class SplineCorrelation(Correlation): | ||
def c(self, x, s, params, dt=False, dx=False): | ||
# x_, s_ = np.atleast_2d(x_), np.atleast_2d(s_) | ||
# # Create stack matrix, where each block is x_i with all s | ||
# stack = np.tile(np.swapaxes(np.atleast_3d(x_), 1, 2), (1, np.size(s_, 0), 1)) - np.tile(s_, ( | ||
# np.size(x_, 0), | ||
# 1, 1)) | ||
stack = Correlation.check_samples_and_return_stack(x, s) | ||
# In this case, the zeta value is just abs(stack)*parameters, no comparison | ||
zeta_matrix = abs(stack) * params | ||
# So, dtheta and dx are just |dj| and theta*sgn(dj), respectively | ||
dtheta_derivs = abs(stack) | ||
# dx_derivs = np.ones((np.size(x,0),np.size(s,0),np.size(s,1)))*parameters | ||
dx_derivs = np.sign(stack) * params | ||
|
||
# Initialize empty sigma and dsigma matrices | ||
sigma = np.ones( | ||
(zeta_matrix.shape[0], zeta_matrix.shape[1], zeta_matrix.shape[2]) | ||
) | ||
dsigma = np.ones( | ||
(zeta_matrix.shape[0], zeta_matrix.shape[1], zeta_matrix.shape[2]) | ||
) | ||
|
||
# Loop over cases to create zeta_matrix and subsequent dR matrices | ||
for i in range(zeta_matrix.shape[0]): | ||
for j in range(zeta_matrix.shape[1]): | ||
for k in range(zeta_matrix.shape[2]): | ||
y = zeta_matrix[i, j, k] | ||
if 0 <= y <= 0.2: | ||
sigma[i, j, k] = 1 - 15 * y ** 2 + 30 * y ** 3 | ||
dsigma[i, j, k] = -30 * y + 90 * y ** 2 | ||
elif 0.2 < y < 1.0: | ||
sigma[i, j, k] = 1.25 * (1 - y) ** 3 | ||
dsigma[i, j, k] = 3.75 * (1 - y) ** 2 * -1 | ||
elif y >= 1: | ||
sigma[i, j, k] = 0 | ||
dsigma[i, j, k] = 0 | ||
|
||
rx = np.prod(sigma, 2) | ||
|
||
if dt: | ||
# Initialize derivative matrices incorporating chain rule | ||
drdt = dsigma * dtheta_derivs | ||
# Loop over to create proper matrices | ||
for i in range(len(params) - 1): | ||
drdt = drdt * np.roll(sigma, i + 1, axis=2) | ||
return rx, drdt | ||
if dx: | ||
# Initialize derivative matrices incorporating chain rule | ||
drdx = dsigma * dx_derivs | ||
# Loop over to create proper matrices | ||
for i in range(len(params) - 1): | ||
drdx = drdx * np.roll(sigma, i + 1, axis=2) | ||
return rx, drdx | ||
return rx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from UQpy.surrogates.kriging.correlation_models.baseclass import * | ||
from UQpy.surrogates.kriging.correlation_models.CubicCorrelation import CubicCorrelation | ||
from UQpy.surrogates.kriging.correlation_models.ExponentialCorrelation import ExponentialCorrelation | ||
from UQpy.surrogates.kriging.correlation_models.GaussianCorrelation import GaussianCorrelation | ||
from UQpy.surrogates.kriging.correlation_models.LinearCorrelation import LinearCorrelation | ||
from UQpy.surrogates.kriging.correlation_models.SphericalCorrelation import SphericalCorrelation | ||
from UQpy.surrogates.kriging.correlation_models.SplineCorrelation import SplineCorrelation |
47 changes: 47 additions & 0 deletions
47
src/UQpy/surrogates/kriging/correlation_models/baseclass/Correlation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from abc import ABC, abstractmethod | ||
import numpy as np | ||
|
||
|
||
class Correlation(ABC): | ||
""" | ||
Abstract base class of all Correlations. Serves as a template for creating new Kriging correlation | ||
functions. | ||
""" | ||
|
||
@abstractmethod | ||
def c(self, x, s, params, dt=False, dx=False): | ||
""" | ||
Abstract method that needs to be implemented by the user when creating a new Correlation function. | ||
""" | ||
pass | ||
|
||
@staticmethod | ||
def check_samples_and_return_stack(x, s): | ||
x_, s_ = np.atleast_2d(x), np.atleast_2d(s) | ||
# Create stack matrix, where each block is x_i with all s | ||
stack = np.tile( | ||
np.swapaxes(np.atleast_3d(x_), 1, 2), (1, np.size(s_, 0), 1) | ||
) - np.tile(s_, (np.size(x_, 0), 1, 1)) | ||
return stack | ||
|
||
@staticmethod | ||
def derivatives(x_, s_, params): | ||
stack = Correlation.check_samples_and_return_stack(x_, s_) | ||
# Taking stack and creating array of all thetaj*dij | ||
after_parameters = params * abs(stack) | ||
# Create matrix of all ones to compare | ||
comp_ones = np.ones((np.size(x_, 0), np.size(s_, 0), np.size(s_, 1))) | ||
# zeta_matrix has all values min{1,theta*dij} | ||
zeta_matrix_ = np.minimum(after_parameters, comp_ones) | ||
# Copy zeta_matrix to another matrix that will used to find where derivative should be zero | ||
indices = zeta_matrix_.copy() | ||
# If value of min{1,theta*dij} is 1, the derivative should be 0. | ||
# So, replace all values of 1 with 0, then perform the .astype(bool).astype(int) | ||
# operation like in the linear example, so you end up with an array of 1's where | ||
# the derivative should be caluclated and 0 where it should be zero | ||
indices[indices == 1] = 0 | ||
# Create matrix of all |dij| (where non zero) to be used in calculation of dR/dtheta | ||
dtheta_derivs_ = indices.astype(bool).astype(int) * abs(stack) | ||
# Same as above, but for matrix of all thetaj where non-zero | ||
dx_derivs_ = indices.astype(bool).astype(int) * params * np.sign(stack) | ||
return zeta_matrix_, dtheta_derivs_, dx_derivs_ |
1 change: 1 addition & 0 deletions
1
src/UQpy/surrogates/kriging/correlation_models/baseclass/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from UQpy.surrogates.kriging.correlation_models.baseclass.Correlation import Correlation |
10 changes: 10 additions & 0 deletions
10
src/UQpy/surrogates/kriging/regression_models/ConstantRegression.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import numpy as np | ||
from UQpy.surrogates.kriging.regression_models.baseclass.Regression import Regression | ||
|
||
|
||
class ConstantRegression(Regression): | ||
def r(self, s): | ||
s = np.atleast_2d(s) | ||
fx = np.ones([np.size(s, 0), 1]) | ||
jf = np.zeros([np.size(s, 0), np.size(s, 1), 1]) | ||
return fx, jf |
12 changes: 12 additions & 0 deletions
12
src/UQpy/surrogates/kriging/regression_models/LinearRegression.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import numpy as np | ||
from UQpy.surrogates.kriging.regression_models.baseclass.Regression import Regression | ||
|
||
|
||
class LinearRegression(Regression): | ||
def r(self, s): | ||
s = np.atleast_2d(s) | ||
fx = np.concatenate((np.ones([np.size(s, 0), 1]), s), 1) | ||
jf_b = np.zeros([np.size(s, 0), np.size(s, 1), np.size(s, 1)]) | ||
np.einsum("jii->ji", jf_b)[:] = 1 | ||
jf = np.concatenate((np.zeros([np.size(s, 0), np.size(s, 1), 1]), jf_b), 2) | ||
return fx, jf |
37 changes: 37 additions & 0 deletions
37
src/UQpy/surrogates/kriging/regression_models/QuadraticRegression.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import numpy as np | ||
from UQpy.surrogates.kriging.regression_models.baseclass.Regression import Regression | ||
|
||
|
||
class QuadraticRegression(Regression): | ||
def r(self, s): | ||
s = np.atleast_2d(s) | ||
fx = np.zeros( | ||
[np.size(s, 0), int((np.size(s, 1) + 1) * (np.size(s, 1) + 2) / 2)] | ||
) | ||
jf = np.zeros( | ||
[ | ||
np.size(s, 0), | ||
np.size(s, 1), | ||
int((np.size(s, 1) + 1) * (np.size(s, 1) + 2) / 2), | ||
] | ||
) | ||
for i in range(np.size(s, 0)): | ||
temp = np.hstack((1, s[i, :])) | ||
for j in range(np.size(s, 1)): | ||
temp = np.hstack((temp, s[i, j] * s[i, j::])) | ||
fx[i, :] = temp | ||
# definie H matrix | ||
h_ = 0 | ||
for j in range(np.size(s, 1)): | ||
tmp_ = s[i, j] * np.eye(np.size(s, 1)) | ||
t1 = np.zeros([np.size(s, 1), np.size(s, 1)]) | ||
t1[j, :] = s[i, :] | ||
tmp = tmp_ + t1 | ||
if j == 0: | ||
h_ = tmp[:, j::] | ||
else: | ||
h_ = np.hstack((h_, tmp[:, j::])) | ||
jf[i, :, :] = np.hstack( | ||
(np.zeros([np.size(s, 1), 1]), np.eye(np.size(s, 1)), h_) | ||
) | ||
return fx, jf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from UQpy.surrogates.kriging.regression_models.baseclass import * | ||
from UQpy.surrogates.kriging.regression_models.ConstantRegression import ConstantRegression | ||
from UQpy.surrogates.kriging.regression_models.LinearRegression import LinearRegression | ||
from UQpy.surrogates.kriging.regression_models.QuadraticRegression import QuadraticRegression |
14 changes: 14 additions & 0 deletions
14
src/UQpy/surrogates/kriging/regression_models/baseclass/Regression.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
|
||
class Regression(ABC): | ||
""" | ||
Abstract base class of all Regressions. Serves as a template for creating new Kriging regression | ||
functions. | ||
""" | ||
@abstractmethod | ||
def r(self, s): | ||
""" | ||
Abstract method that needs to be implemented by the user when creating a new Regression function. | ||
""" | ||
pass |
1 change: 1 addition & 0 deletions
1
src/UQpy/surrogates/kriging/regression_models/baseclass/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from UQpy.surrogates.kriging.regression_models.baseclass.Regression import Regression |