nca.terminal¶
Vectorized terminal phase regression.
The elimination rate constant lambda_z is the negative slope of the linear
regression of ln c on t over the points of the terminal phase
(Gabrielsson & Weiner 2016, ch. 2.8; Phoenix WinNonlin NCA). Which points form
the terminal phase is decided by TerminalPhase.method; BEST_FIT evaluates
every window of consecutive points that ends at the last measurable point and
takes the largest adjusted R², preferring more points within a tolerance,
which is the rule of Phoenix.
All windows of all rows are evaluated at once: with suffix sums of x, y,
x², xy and y² over the packed points the statistics of the window
starting at index s are closed-form expressions of the sums from s to the
end, so window_statistics returns (N, n) arrays without a loop over rows
or windows.
RESIDUAL_FLOOR
module-attribute
¶
Multiple of n * eps * syy below which a residual sum of squares is zero.
The residual of a window is syy_c - slope * sxy_c, a difference of two
quantities which are equal for a perfect fit, so what is left there is the
rounding of the sums themselves and its sign is arbitrary: clamping it at zero
reports either 0 or the square root of that noise, and which one depends on the
platform and on whether the python core or the rust kernel computed it.
The floor is taken against the uncentred syy and not against syy_c,
because every window of a row shares one origin, the row's first sample, so
window_statistics can evaluate them all from one set of suffix sums. For a
short window late in the curve that makes syy and sy * sy / n both large,
of the order of the whole drop of the curve, while their difference syy_c is
small - the cancellation, and therefore the noise, is set by the uncentred sums
and not by the centred one. A floor written against syy_c leaves the three
and four point windows of a noise free curve reporting a standard error of up
to 4e-08, which is what measurement showed.
The multiple is 16 rather than tighter or looser from measurement over 120 000
windows of 20 000 curves. The ratio |ss_res| / (n * eps * syy) reaches 1.20
for a noise free curve, at three points and falling with the window, while a
curve with 1 % noise stays above 1.9e+05 from four points up. The two regimes
therefore overlap only at three points, where a window has one degree of
freedom and three noisy samples can fall nearly collinear by chance: at this
floor that zeroes 2 windows in 120 000 of a noisy batch, whose residual really
is at the rounding floor, and leaves no exact fit reporting a standard error.
A multiple of 4 would do the same with a margin of only 3x over the worst
exact fit measured, so 16 buys robustness to another time grid for the same
cost.
TerminalFit
dataclass
¶
TerminalFit(
slope,
intercept,
r2,
r2_adj,
se_slope,
n_points,
t_first,
t_last,
start,
flags,
candidates=None,
)
Result of the terminal regression per row, NaN (and start = -1) without a fit.
Attributes:
| Name | Type | Description |
|---|---|---|
slope |
ndarray
|
slope of |
intercept |
ndarray
|
intercept of the regression, |
r2 |
ndarray
|
coefficient of determination |
r2_adj |
ndarray
|
adjusted coefficient of determination |
se_slope |
ndarray
|
standard error of the slope |
n_points |
ndarray
|
number of points of the regression |
t_first |
ndarray
|
time of the first point of the regression |
t_last |
ndarray
|
time of the last point of the regression |
start |
ndarray
|
packed index of the first point of the window |
flags |
ndarray
|
|
candidates |
DataFrame | None
|
every candidate window of every row with the columns |
window_statistics
¶
Regression statistics of every window from an index to the end of the row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
regressor |
required |
y
|
ndarray
|
response |
required |
valid
|
ndarray
|
which points enter the regression |
required |
Returns:
| Type | Description |
|---|---|
dict[str, ndarray]
|
Arrays |
dict[str, ndarray]
|
|
dict[str, ndarray]
|
fewer than 3 points are |
terminal_fit
¶
Terminal log-linear regression of every row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tp
|
ndarray
|
packed times |
required |
cp
|
ndarray
|
packed values |
required |
n_valid
|
ndarray
|
valid points per row |
required |
tmax_idx
|
ndarray
|
packed index of the maximum per row |
required |
phase
|
TerminalPhase
|
the selection rule and its parameters |
required |
manual_mask
|
ndarray | None
|
packed points of the regression for |
None
|
exclude
|
ndarray | None
|
packed points which may not enter the regression |
None
|
windows
|
ndarray | None
|
the terminal window of single rows |
None
|
Returns:
| Type | Description |
|---|---|
TerminalFit
|
The fit per row, with the table of every candidate window |
TerminalFit
|
( |
Raises:
| Type | Description |
|---|---|
ValueError
|
|
candidate_table
¶
Every window the selection of the terminal phase may choose from.
A candidate is a window which starts at a point of the regression, holds at
least phase.min_points regressable points and, with
phase.exclude_cmax, starts after the maximum: the windows BEST_FIT
ranks by the adjusted R², and the same set for the other rules, which pick
one of them by a different criterion. A window whose first point cannot be
regressed is left out, since its statistics are those of the window
starting at the next regressable point (_collect). The slope is reported
as it is, so a window of a still rising curve is in the table with a
positive slope, which BEST_FIT never chooses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tp
|
ndarray
|
packed times |
required |
y
|
ndarray
|
the logarithms of the values |
required |
regressable
|
ndarray
|
the points which may enter a regression |
required |
tmax_idx
|
ndarray
|
packed index of the maximum per row |
required |
phase
|
TerminalPhase
|
the selection rule and its parameters |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
One row per candidate window with the columns |
DataFrame
|
row of the batch), |
DataFrame
|
window), |
DataFrame
|
are ordered by row and by the start time within a row. |