Defining a Custom Function

This is a short tutorial on using the Parameter Space Dimension Reduction library on the (in)famous borehole test function. Although this function is provided as a demo (with analytic gradients), here we provide a complete example setting up a function in this library.

Standard imports

To begin, we import numpy and psdr.

[1]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
import psdr

Problem Setup

To define a problem for use with this library, there are two basic steps. First we need to define the domain on which we will be working. As engineering applications frequently have poorly scaled variables with different units, defining a domain allows us to correct this effect when evaluating the importance of a particular set of parameters. Second, we need to define the function, posed on this domain, that we are interested in studying. Depending on the technique we use, we need to either have access to the function itself or to samples of the function. Here we define the function and then sample it as appropreate for each technique.

Defining the domain

The borehole function has eight variables:

Parameter and Domain Description
\(r_w \in [0.05, 0.15]\) radius of borehole (m)
\(r \in [100, 50\times 10^3]\) radius of influence (m)
\(T_u \in [63700, 115600]\) transmissivity of upper aquifer (\(m^2\)/yr)
\(H_u \in [990, 1110]\) potentiometric head of upper aquifer (m)
$T_:nbsphinx-math:ell `:nbsphinx-math:in [63.1, 116] `$ transmissivity of lower aquifer (\(m^2\)/yr)
\(H_\ell \in [700, 820]\) potentiometric head of lower aquifer (m)
\(L \in [1120, 1680]\) length of borehole (m)
\(K_w \in [9855, 12045]\) hydraulic conductivity of borehole (m/yr)

To begin, we first setup a domain for each variable.

[2]:
rw_domain = psdr.BoxDomain(0.05, 0.15)
r_domain = psdr.BoxDomain(100, 50e3)
Tu_domain = psdr.BoxDomain(63700, 115600)
Hu_domain = psdr.BoxDomain(990, 1110)
Tell_domain = psdr.BoxDomain(63.1, 116)
Hell_domain = psdr.BoxDomain(700, 820)
L_domain = psdr.BoxDomain(1120, 1680)
Kw_domain = psdr.BoxDomain(9855, 12045)

Then, we combine these domains for each variable into a domain for the entire borehole function:

[3]:
borehole_domain = psdr.TensorProductDomain([
    rw_domain, r_domain, Tu_domain, Hu_domain,
    Tell_domain, Hell_domain, L_domain, Kw_domain])

This class instance provides many functions to interact with the domain. For example, we can sample a random point in this domain.

[4]:
borehole_domain.sample(1)
[4]:
array([6.87872077e-02, 2.88484670e+04, 1.08139517e+05, 1.06153228e+03,
       1.00115493e+02, 7.52056091e+02, 1.57284863e+03, 1.15785813e+04])

Defining the Function

The borehole function is:

\[f(r_w, r, T_u, H_u, T_\ell, H_\ell, L, K_w) := \frac{ 2\pi T_u (H_u - H_\ell)}{\log(r/r_w) ( 1 + \frac{2L T_u}{\log(r/r_w) r_w^2 K_w} + \frac{T_u}{T_\ell} )}.\]

So we simply define this function that takes an array-like input and returns the output.

[5]:
def borehole_func(x):
    r_w = x[0]
    r = x[1]
    T_u = x[2]
    H_u = x[3]
    T_l = x[4]
    H_l = x[5]
    L = x[6]
    K_w = x[7]
    return 2*np.pi*T_u*(H_u - H_l)/(np.log(r/r_w)*(1 + 2*L*T_u/(np.log(r/r_w)*r_w**2*K_w) + T_u/T_l))

Wrapping the Function

For convience, we can wrap a function in the Function environment. This does several important things. First, it automatically converts function on the application domain to a function on the normalized domain. Further, as illustrated here, it can automatically estimate gradients using a finite difference approximation. However, if gradients are availble, these can also be provided.

[6]:
borehole = psdr.Function(borehole_func, borehole_domain, fd_grad = True)

Active Subspace

[7]:
X = borehole.domain.sample(200)
grads = borehole.grad(X)
[8]:
active = psdr.ActiveSubspace()
active.fit(grads)
[9]:
fX = borehole(X)
active.shadow_plot(X, fX, dim = 1)
[9]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f118c5525c0>
_images/borehole_17_1.png
[10]:
active.shadow_plot(X, fX, dim = 2)
[10]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f118a3d0e10>
_images/borehole_18_1.png

Constructing a Ridge Approximation

[11]:
X = borehole.domain.sample(200)
fX = borehole(X)
[12]:
pra = psdr.PolynomialRidgeApproximation(degree = 5, subspace_dimension = 1, norm = 2, bound = 'upper')
pra.fit(X, fX)
[13]:
pra.shadow_plot(X, fX);
_images/borehole_22_0.png

Of course, we can also construct ridge approximations with higher dimensional subspaces.

[14]:
pra2 = psdr.PolynomialRidgeApproximation(degree = 5, subspace_dimension = 2)
pra2.fit(X, fX)
[15]:
pra2.U
[15]:
array([[ 0.92086577,  0.38751581],
       [ 0.0027626 , -0.05204254],
       [ 0.00280231,  0.00185545],
       [ 0.21640868, -0.54882712],
       [ 0.0032728 , -0.01363488],
       [-0.21473188,  0.5504805 ],
       [-0.21919035,  0.44707093],
       [ 0.10484816, -0.2069331 ]])
[16]:
pra2.shadow_plot(X, fX)
[16]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f118a267128>
_images/borehole_26_1.png
[ ]: