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')

Print a summary of descriptive statistics for a 1-D array.

Parameters:

Name Type Description Default
v ndarray

Array to summarise (flattened to one dimension).

required
name str | None

Optional label printed alongside the summary.

'v'

Returns:

Type Description
Any

The scipy.stats.describe result for v.

Source code in bs_python_utils/bssputils.py
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:
    """Print a summary of descriptive statistics for a 1-D array.

    Args:
        v: Array to summarise (flattened to one dimension).
        name: Optional label printed alongside the summary.

    Returns:
        The ``scipy.stats.describe`` result for ``v``.
    """
    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"Std deviation: {sqrt(d.variance)}")
    return d

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

Interpolate y as a function of x using univariate splines.

Parameters:

Name Type Description Default
y ndarray

Observed response values.

required
x ndarray

Covariate locations.

required
x_new ndarray | None

Evaluation points; defaults to x.

None
is_sorted bool | None

Set to True when x is already sorted in ascending order.

False
smooth bool | None

When True estimate weights via rice_stderr and fit a smoothing spline; otherwise enforce an interpolating spline with s=0.

True

Returns:

Type Description
ndarray

The interpolated values evaluated 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:
    """Interpolate ``y`` as a function of ``x`` using univariate splines.

    Args:
        y: Observed response values.
        x: Covariate locations.
        x_new: Evaluation points; defaults to ``x``.
        is_sorted: Set to ``True`` when ``x`` is already sorted in ascending order.
        smooth: When ``True`` estimate weights via ``rice_stderr`` and fit a smoothing spline;
            otherwise enforce an interpolating spline with ``s=0``.

    Returns:
        The interpolated values evaluated 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, s=0)

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