Skip to content

bssputils module

Contains scipy utility programs:

  • describe_array: report descriptive statistics on a vectorized array
  • spline_reg: spline interpolation in one dimension.

describe_array(v, name='v')

descriptive statistics on an array interpreted as a vector

Parameters:

Name Type Description Default
v ndarray

the array

required
name str | None

its name

'v'

Returns:

Type Description
Any

the scipy.stats.describe object

Source code in bs_python_utils/bssputils.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def describe_array(v: np.ndarray, name: str | None = "v") -> Any:
    """
    descriptive statistics on an array interpreted as a vector

    Args:
        v: the array
        name: its name

    Returns:
        the `scipy.stats.describe` object
    """
    print_stars(f"{name} has:")
    d = sts.describe(v, None)
    print(f"Number of elements: {d.nobs}")
    print(f"Minimum: {d.minmax[0]}")
    print(f"Maximum: {d.minmax[1]}")
    print(f"Mean: {d.mean}")
    print(f"Stderr: {sqrt(d.variance)}")
    return d

spline_reg(y, x, x_new=None, is_sorted=False, smooth=True)

one-dimensional spline interpolation of vector y on vector x

Parameters:

Name Type Description Default
y ndarray

vector of y-values

required
x ndarray

vector of x-values

required
x_new ndarray | None

where we evaluate (at the points in x by default)

None
is_sorted bool | None

True if x is sorted in increasing order

False
smooth bool | None

True if we want a smoother; otherwise we go through all points provided

True

Returns:

Type Description
ndarray

values interpolated at x_new.

Source code in bs_python_utils/bssputils.py
39
40
41
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
def spline_reg(
    y: np.ndarray,
    x: np.ndarray,
    x_new: np.ndarray | None = None,
    is_sorted: bool | None = False,
    smooth: bool | None = True,
) -> np.ndarray:
    """
    one-dimensional spline interpolation of vector `y` on vector `x`

    Args:
        y: vector of y-values
        x: vector of x-values
        x_new: where we evaluate (at the points in `x` by default)
        is_sorted: True if `x` is sorted in increasing order
        smooth: True if we want a smoother; otherwise we go through all points provided

    Returns:
        values interpolated at `x_new`.
    """
    n = check_vector(x)
    ny = check_vector(y)
    if ny != n:
        bs_error_abort("x and y should have the same size")

    if not is_sorted:
        # need to sort by increasing value of x
        order_rhs = np.argsort(x)
        rhs = x[order_rhs]
        lhs = y[order_rhs]
    else:
        rhs, lhs = x, y

    if smooth:
        # we compute a local estimator of the stderr of (y | x) and we use it to enter weights
        sigyx = rice_stderr(lhs, rhs)
        w = 1 / sigyx
        spl = UnivariateSpline(rhs, lhs, w=w)
    else:
        spl = UnivariateSpline(rhs, lhs)

    xeval = x if x_new is None else x_new
    y_pred = spl(xeval)
    return cast(np.ndarray, y_pred)