labplan 0.3.0__tar.gz → 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {labplan-0.3.0/src/labplan.egg-info → labplan-0.4.0}/PKG-INFO +307 -15
- {labplan-0.3.0 → labplan-0.4.0}/README.md +305 -12
- {labplan-0.3.0 → labplan-0.4.0}/pyproject.toml +4 -4
- {labplan-0.3.0 → labplan-0.4.0}/src/labplan/__init__.py +9 -2
- {labplan-0.3.0 → labplan-0.4.0}/src/labplan/conformal.py +5 -1
- labplan-0.4.0/src/labplan/inverse.py +309 -0
- {labplan-0.3.0 → labplan-0.4.0}/src/labplan/model.py +50 -2
- {labplan-0.3.0 → labplan-0.4.0}/src/labplan/plan.py +46 -10
- labplan-0.4.0/src/labplan/runs.py +155 -0
- {labplan-0.3.0 → labplan-0.4.0/src/labplan.egg-info}/PKG-INFO +307 -15
- {labplan-0.3.0 → labplan-0.4.0}/src/labplan.egg-info/SOURCES.txt +6 -1
- labplan-0.4.0/tests/test_fixes_040.py +234 -0
- labplan-0.4.0/tests/test_inverse_runs.py +382 -0
- labplan-0.4.0/tests/test_tutorials.py +77 -0
- {labplan-0.3.0 → labplan-0.4.0}/LICENSE +0 -0
- {labplan-0.3.0 → labplan-0.4.0}/setup.cfg +0 -0
- {labplan-0.3.0 → labplan-0.4.0}/src/labplan/fit.py +0 -0
- {labplan-0.3.0 → labplan-0.4.0}/src/labplan/propagate.py +0 -0
- {labplan-0.3.0 → labplan-0.4.0}/src/labplan/records.py +0 -0
- {labplan-0.3.0 → labplan-0.4.0}/src/labplan/report.py +0 -0
- {labplan-0.3.0 → labplan-0.4.0}/src/labplan/stats.py +0 -0
- {labplan-0.3.0 → labplan-0.4.0}/src/labplan.egg-info/dependency_links.txt +0 -0
- {labplan-0.3.0 → labplan-0.4.0}/src/labplan.egg-info/requires.txt +0 -0
- {labplan-0.3.0 → labplan-0.4.0}/src/labplan.egg-info/top_level.txt +0 -0
- {labplan-0.3.0 → labplan-0.4.0}/tests/test_conformal_report.py +0 -0
- {labplan-0.3.0 → labplan-0.4.0}/tests/test_derivatives_design.py +0 -0
- {labplan-0.3.0 → labplan-0.4.0}/tests/test_documented_claims.py +0 -0
- {labplan-0.3.0 → labplan-0.4.0}/tests/test_fit_plan.py +0 -0
- {labplan-0.3.0 → labplan-0.4.0}/tests/test_fixes_030.py +0 -0
- {labplan-0.3.0 → labplan-0.4.0}/tests/test_lab_data.py +0 -0
- {labplan-0.3.0 → labplan-0.4.0}/tests/test_propagate_records.py +0 -0
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: labplan
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: Plan the measurement, fit the model, keep the record - exact measurement planning, calibration and audit trails for any lab model
|
|
5
5
|
Author: Tanvir Mahmud Mahim
|
|
6
|
-
License: Apache-2.0
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
7
|
Project-URL: Homepage, https://github.com/TaN-MM-Org/labplan
|
|
8
8
|
Project-URL: Issues, https://github.com/TaN-MM-Org/labplan/issues
|
|
9
9
|
Classifier: Development Status :: 4 - Beta
|
|
10
10
|
Classifier: Intended Audience :: Science/Research
|
|
11
|
-
Classifier: License :: OSI Approved :: Apache Software License
|
|
12
11
|
Classifier: Programming Language :: Python :: 3
|
|
13
12
|
Classifier: Programming Language :: Python :: 3.9
|
|
14
13
|
Classifier: Programming Language :: Python :: 3.10
|
|
@@ -43,9 +42,11 @@ questions:
|
|
|
43
42
|
need for a target error bar?
|
|
44
43
|
- **After measuring:** what are the fitted numbers, and their error
|
|
45
44
|
bars? Are the readings consistent with the model and their stated
|
|
46
|
-
errors?
|
|
47
|
-
|
|
48
|
-
|
|
45
|
+
errors? Do the residuals wander in a pattern the model misses? What
|
|
46
|
+
is the error bar of anything computed from them, such as a predicted
|
|
47
|
+
reading? And, reading the calibration backwards, which setting
|
|
48
|
+
produced a new reading, and how sure is that? The readings can come
|
|
49
|
+
straight from an instrument's exported text table.
|
|
49
50
|
- **If the model may be wrong:** how wide a band around the
|
|
50
51
|
prediction still covers new measurements at a chosen rate?
|
|
51
52
|
- **Later:** can anyone trace what was fitted, to exactly which data,
|
|
@@ -61,6 +62,7 @@ explains why.
|
|
|
61
62
|
- [A short guide to the words used here](#a-short-guide-to-the-words-used-here)
|
|
62
63
|
- [Install](#install)
|
|
63
64
|
- [Examples](#examples) (each with the output it prints)
|
|
65
|
+
- [Tutorials](#tutorials) (complete scripts, from data to result)
|
|
64
66
|
- [What is in the package](#what-is-in-the-package)
|
|
65
67
|
- [When it refuses, and why](#when-it-refuses-and-why)
|
|
66
68
|
- [How the results are checked](#how-the-results-are-checked)
|
|
@@ -117,6 +119,14 @@ explains why.
|
|
|
117
119
|
- **p-value**: here, the probability that chi2 would come out at
|
|
118
120
|
least as large as it did if the model and the stated reading errors
|
|
119
121
|
were right. A very small p-value means one of them is wrong.
|
|
122
|
+
- **Residuals and runs**: the residuals are the differences
|
|
123
|
+
`reading - prediction`. Ordered by the setting, a **run** is a block
|
|
124
|
+
of residuals with the same sign. A model that misses a bend or a
|
|
125
|
+
drift leaves long runs, so too few runs is a warning sign.
|
|
126
|
+
- **Inverse prediction**: reading a calibration curve backwards. A
|
|
127
|
+
new reading `y0` is turned into the setting `x0` at which the model
|
|
128
|
+
predicts `y0`; its error bar counts both the error of the new
|
|
129
|
+
reading and the uncertainty of the calibration.
|
|
120
130
|
- **Conformal prediction** -- a way to put a band around predictions
|
|
121
131
|
from held-out data alone, without trusting the model. The
|
|
122
132
|
**calibration scores** are the absolute errors
|
|
@@ -148,12 +158,13 @@ an error bar of 5 Hz on the corner frequency), and
|
|
|
148
158
|
`02_conformal_certificate.py` wraps a conformal band around a
|
|
149
159
|
deliberately wrong straight-line model and measures its coverage
|
|
150
160
|
over 300 simulated rounds (0.899, against the exact 0.9000 for its
|
|
151
|
-
79 calibration points).
|
|
161
|
+
79 calibration points). Two longer tutorials, also in `examples/`,
|
|
162
|
+
are described in [Tutorials](#tutorials) below.
|
|
152
163
|
|
|
153
164
|
## Examples
|
|
154
165
|
|
|
155
166
|
Each example below runs as written, and the output shown is what it
|
|
156
|
-
printed with labplan 0.
|
|
167
|
+
printed with labplan 0.4.0. The model, the numbers and the noise are
|
|
157
168
|
illustrative; the random numbers come from fixed seeds, so the
|
|
158
169
|
output is repeatable.
|
|
159
170
|
|
|
@@ -616,6 +627,149 @@ the wrong number of fields stops it with the line number, because
|
|
|
616
627
|
skipping such rows silently would change the data you fit. The
|
|
617
628
|
result `(x, y, sigmas)` goes straight into `fit`.
|
|
618
629
|
|
|
630
|
+
### 13. Read the calibration backwards: which setting gave this reading?
|
|
631
|
+
|
|
632
|
+
```python
|
|
633
|
+
import numpy as np
|
|
634
|
+
from labplan import Model, fit, inverse_predict
|
|
635
|
+
|
|
636
|
+
line = Model("sensor line",
|
|
637
|
+
lambda th, x: th[0] * x[:, 0] + th[1],
|
|
638
|
+
param_names=("gain", "offset"),
|
|
639
|
+
reference="illustrative straight-line sensor model")
|
|
640
|
+
x = np.linspace(0.5, 5.0, 12)
|
|
641
|
+
rng = np.random.default_rng(1)
|
|
642
|
+
y = 2.0 * x + 0.1 + 0.05 * rng.standard_normal(x.size)
|
|
643
|
+
res = fit(line, x, y, theta0=[1.0, 0.0], sigmas=0.05)
|
|
644
|
+
|
|
645
|
+
# An unknown gives the reading 5.0 (error bar 0.05): which setting?
|
|
646
|
+
out = inverse_predict(line, res, reading=5.0, bracket=(0.5, 5.0),
|
|
647
|
+
reading_sigma=0.05)
|
|
648
|
+
print(f"setting = {out['value']:.4f} +/- {out['sigma']:.4f}")
|
|
649
|
+
print(f" from the reading: {out['sigma_from_reading']:.4f}, "
|
|
650
|
+
f"from the calibration: {out['sigma_from_parameters']:.4f}")
|
|
651
|
+
|
|
652
|
+
# A reading the calibrated range does not reach is refused:
|
|
653
|
+
try:
|
|
654
|
+
inverse_predict(line, res, reading=12.0, bracket=(0.5, 5.0))
|
|
655
|
+
except ValueError as err:
|
|
656
|
+
print("refused:", err)
|
|
657
|
+
```
|
|
658
|
+
|
|
659
|
+
```
|
|
660
|
+
setting = 2.4442 +/- 0.0261
|
|
661
|
+
from the reading: 0.0250, from the calibration: 0.0074
|
|
662
|
+
refused: the model does not reach the reading 12 for settings between 0.5 and 5 (there it runs from 1.11061 to 10.1129); widen the bracket, or check the reading
|
|
663
|
+
```
|
|
664
|
+
|
|
665
|
+
`inverse_predict` (new in 0.4.0) solves `f(theta, x0) = reading` for
|
|
666
|
+
`x0` inside the `bracket` you give, normally the calibrated range. The
|
|
667
|
+
error bar is first order: `u(x0)^2 = (u(y0)^2 + a^T C a) / s^2`, with
|
|
668
|
+
`s` the slope of the curve at `x0` and `a` the slopes with respect to
|
|
669
|
+
the parameters. For a straight line this is the propagation-of-error
|
|
670
|
+
formula of the NIST/SEMATECH e-Handbook of Statistical Methods,
|
|
671
|
+
section 2.3.6.7.3. The two parts show what limits the result: here the
|
|
672
|
+
reading (0.0250) far more than the calibration (0.0074), so repeating
|
|
673
|
+
the reading of the unknown would help, and recalibrating would not.
|
|
674
|
+
The value is the setting example 8 found with `propagate`; the error
|
|
675
|
+
bar is larger because it now includes the reading's own error. Pass
|
|
676
|
+
an array of readings to get the covariance of several unknowns (they
|
|
677
|
+
share the calibration, so their errors are correlated), or
|
|
678
|
+
`theta=` and `cov=` from `information` to plan before calibrating. A
|
|
679
|
+
reading that the model reaches twice in the bracket (a peak), a curve
|
|
680
|
+
that is flat there, and a curve that bends so much within one error
|
|
681
|
+
bar that the first-order error bar is unreliable are refused.
|
|
682
|
+
|
|
683
|
+
### 14. Do the residuals wander? The runs test
|
|
684
|
+
|
|
685
|
+
```python
|
|
686
|
+
import numpy as np
|
|
687
|
+
from labplan import Model, fit, goodness_of_fit, runs_test
|
|
688
|
+
|
|
689
|
+
line = Model("sensor line",
|
|
690
|
+
lambda th, x: th[0] * x[:, 0] + th[1],
|
|
691
|
+
param_names=("gain", "offset"),
|
|
692
|
+
reference="illustrative straight-line sensor model")
|
|
693
|
+
x = np.linspace(0.5, 5.0, 24)
|
|
694
|
+
rng = np.random.default_rng(2)
|
|
695
|
+
# Readings that bend a little, with errors stated 3 times too large:
|
|
696
|
+
y = 2.0 * x + 0.1 + 0.05 * x ** 2 + 0.05 * rng.standard_normal(x.size)
|
|
697
|
+
res = fit(line, x, y, theta0=[1.0, 0.0], sigmas=0.15)
|
|
698
|
+
print(f"chi2 test: p = {goodness_of_fit(res)['p_value']:.3f}")
|
|
699
|
+
|
|
700
|
+
r = runs_test(y - line.predict(res.theta, x), order=x)
|
|
701
|
+
print(f"signs: {r['n_positive']} +, {r['n_negative']} -, "
|
|
702
|
+
f"{r['runs']} runs (expected {r['expected_runs']:.2f} "
|
|
703
|
+
f"+/- {r['sigma_runs']:.2f}), p = {r['p_value']:.4f}")
|
|
704
|
+
```
|
|
705
|
+
|
|
706
|
+
```
|
|
707
|
+
chi2 test: p = 0.994
|
|
708
|
+
signs: 9 +, 15 -, 5 runs (expected 12.25 +/- 2.24), p = 0.0010
|
|
709
|
+
```
|
|
710
|
+
|
|
711
|
+
The readings bend a little, and their stated errors are 3 times too
|
|
712
|
+
large, so chi2 is small and its test sees nothing wrong (p = 0.994).
|
|
713
|
+
The residuals of the straight line, ordered by `x`, tell another
|
|
714
|
+
story: positive at both ends and negative in the middle, they form
|
|
715
|
+
only 5 runs where about 12 are expected. `runs_test` (new in 0.4.0)
|
|
716
|
+
gives the probability of so few runs if the signs were in random order
|
|
717
|
+
(the runs test of A. Wald and J. Wolfowitz, Ann. Math. Statist. 11,
|
|
718
|
+
147 (1940)), computed exactly. `p_value_high` is the opposite tail
|
|
719
|
+
(signs that alternate too often). For residuals of a fit the signs are
|
|
720
|
+
not exactly independent; in the tests this made the test more
|
|
721
|
+
cautious, not less: on 4000 seeded correct straight-line fits each
|
|
722
|
+
with 12 and with 30 readings it raised a false alarm at the 5 % level
|
|
723
|
+
in 0.7 % and 2.8 % of them (3.6 % for 30 independent random signs,
|
|
724
|
+
where the exact test, whose runs come in whole numbers, stays at or
|
|
725
|
+
below 5 %).
|
|
726
|
+
|
|
727
|
+
## Tutorials
|
|
728
|
+
|
|
729
|
+
Two complete scripts in [`examples/`](examples/) go from raw data
|
|
730
|
+
(simulated with a fixed seed, so the output is repeatable) to a final
|
|
731
|
+
result with error bars. Each runs in well under a second, and a test
|
|
732
|
+
checks that it still prints exactly what is shown here.
|
|
733
|
+
|
|
734
|
+
[`tutorial_calibration_curve.py`](examples/tutorial_calibration_curve.py)
|
|
735
|
+
calibrates an illustrative pressure sensor with a curved response
|
|
736
|
+
(`V = c0 + c1 P + c2 P^2`, 11 reference pressures from 0 to 100 kPa),
|
|
737
|
+
plans how well an unknown will be read before calibrating, checks the
|
|
738
|
+
calibration with chi-squared and the runs test, and then reads an
|
|
739
|
+
unknown (5 readings, true pressure 50 kPa) off the curve:
|
|
740
|
+
|
|
741
|
+
```
|
|
742
|
+
planned error bar at 50 kPa from one reading: 0.0161 kPa
|
|
743
|
+
fitted c0 = 0.09988 +/- 0.00046 V
|
|
744
|
+
fitted c1 = 0.039960 +/- 2.1e-05 V/kPa
|
|
745
|
+
fitted c2 = 9.9567e-06 +/- 2e-07 V/kPa^2
|
|
746
|
+
chi2 = 6.93 for 8 degrees of freedom, p = 0.544
|
|
747
|
+
runs of residual signs: 8 (expected 6.5), p = 0.911
|
|
748
|
+
unknown: mean reading 2.12274 V +/- 0.00027 V (5 readings)
|
|
749
|
+
pressure = 49.9988 +/- 0.0094 kPa
|
|
750
|
+
from the reading noise: 0.0066 kPa
|
|
751
|
+
from the calibration curve: 0.0067 kPa
|
|
752
|
+
```
|
|
753
|
+
|
|
754
|
+
[`tutorial_decay_on_background.py`](examples/tutorial_decay_on_background.py)
|
|
755
|
+
measures a 10 mV decay with a time constant near 1 ns on a steady 1 V
|
|
756
|
+
level, in seconds and volts. It plans how many traces to average for
|
|
757
|
+
an error bar of 10 ps on the time constant, fits the averaged trace,
|
|
758
|
+
checks the fit and reports the decay rate `1/tau` with its error bar.
|
|
759
|
+
labplan 0.3.0 refused this model at the natural first guess
|
|
760
|
+
`tau = 1e-9` s (see Corrections):
|
|
761
|
+
|
|
762
|
+
```
|
|
763
|
+
planned error bar on tau from one trace: 3.57e-11 s
|
|
764
|
+
traces to average for tau +/- 1e-11 s: 13
|
|
765
|
+
A = 0.0101 +/- 5.9e-05 V
|
|
766
|
+
tau = 9.9383e-10 +/- 9.8e-12 s
|
|
767
|
+
B = 0.999993 +/- 1.3e-05 V
|
|
768
|
+
chi2 = 150.0 for 197 degrees of freedom, p = 0.995
|
|
769
|
+
runs of residual signs: 105 (expected 101.0), p = 0.740
|
|
770
|
+
decay rate 1/tau = 1.006e+09 +/- 9.9e+06 1/s
|
|
771
|
+
```
|
|
772
|
+
|
|
619
773
|
## What is in the package
|
|
620
774
|
|
|
621
775
|
**The model**
|
|
@@ -644,7 +798,12 @@ result `(x, y, sigmas)` goes straight into `fit`.
|
|
|
644
798
|
fallback step is checked against a step 10 times smaller; while
|
|
645
799
|
the two disagree by more than 1e-6 of the largest slope (plus an
|
|
646
800
|
allowance for rounding), the smaller step is taken, at most 8
|
|
647
|
-
times.
|
|
801
|
+
times. Since 0.4.0, when that fallback step reaches or crosses zero
|
|
802
|
+
and the model returns values that are not finite there (a time
|
|
803
|
+
constant of 1e-9 s, say), it is divided by 10 until they are finite,
|
|
804
|
+
at most 8 times, and then checked the same way. `rel_step` (default
|
|
805
|
+
1e-6) must be a positive number. `Model.n_params` counts the
|
|
806
|
+
parameters.
|
|
648
807
|
|
|
649
808
|
**Planning**
|
|
650
809
|
|
|
@@ -665,7 +824,10 @@ result `(x, y, sigmas)` goes straight into `fit`.
|
|
|
665
824
|
a swapped-in setting taking the place of the one it replaced), the
|
|
666
825
|
information matrix, `identifiable`, the condition number, the error
|
|
667
826
|
bars and the covariance; the result can be passed to
|
|
668
|
-
`repeats_for`.
|
|
827
|
+
`repeats_for`. Since 0.4.0 the candidates are scored in batches,
|
|
828
|
+
which gives the same picks, bit for bit, 9 to 24 times faster for
|
|
829
|
+
400 to 3000 candidates (medians of 7 runs on a shared 2-CPU
|
|
830
|
+
machine).
|
|
669
831
|
- `repeats_for(target_sigma, plan)` -- the number of repeats `r` of a
|
|
670
832
|
planned design needed to bring every error bar down to a target,
|
|
671
833
|
from the rule that `r` repeats divide the error bars by `sqrt(r)`.
|
|
@@ -719,6 +881,26 @@ result `(x, y, sigmas)` goes straight into `fit`.
|
|
|
719
881
|
the series and continued fraction of Press et al., Numerical
|
|
720
882
|
Recipes, 3rd ed. (2007), section 6.2, so that small values keep
|
|
721
883
|
their full relative precision. See example 11.
|
|
884
|
+
- `runs_test(residuals, order=None)` (new in 0.4.0): the runs test of
|
|
885
|
+
Wald and Wolfowitz on the signs of the residuals, sorted by `order`
|
|
886
|
+
(normally the settings). Returns `runs`, `n_positive`, `n_negative`,
|
|
887
|
+
`n_zero` (zero residuals have no sign and are left out),
|
|
888
|
+
`expected_runs`, `sigma_runs`, `p_value` (probability of this few
|
|
889
|
+
runs or fewer when the signs are in random order) and `p_value_high`
|
|
890
|
+
(this many or more). See example 14.
|
|
891
|
+
|
|
892
|
+
**Reading a calibration backwards**
|
|
893
|
+
|
|
894
|
+
- `inverse_predict(model, result, reading, bracket, reading_sigma=0.0,
|
|
895
|
+
theta=None, cov=None, n_scan=1001, max_slope_change=0.1)` (new in
|
|
896
|
+
0.4.0): the setting `x0` at which the model predicts `reading`,
|
|
897
|
+
searched in `bracket = (low, high)` (scanned at `n_scan` points, then
|
|
898
|
+
refined by bisection), with its error bar from the reading's own
|
|
899
|
+
error `reading_sigma` and from the parameter covariance (of `result`,
|
|
900
|
+
or `theta=` and `cov=`). Returns `value`, `sigma`, `cov` (for several
|
|
901
|
+
readings), `slope` (`df/dx` at `x0`), `sigma_from_reading`,
|
|
902
|
+
`sigma_from_parameters` and `slope_change`. The settings must be one
|
|
903
|
+
number per measurement. See example 13.
|
|
722
904
|
|
|
723
905
|
**When the model may be wrong** (split conformal prediction)
|
|
724
906
|
|
|
@@ -792,6 +974,23 @@ its inputs and conventions.
|
|
|
792
974
|
outside 0 to 1, or too few scores for the level (the message names
|
|
793
975
|
the minimum); `coverage_exact` refuses the same levels;
|
|
794
976
|
`conformal_interval` refuses a negative or non-finite `q`;
|
|
977
|
+
- `inverse_predict` finds no setting in the bracket at which the
|
|
978
|
+
model reaches the reading (the message gives the range the model
|
|
979
|
+
covers there), finds more than one (narrow the bracket to one
|
|
980
|
+
branch), finds the model flat at the solution or equal to the
|
|
981
|
+
reading over a whole range, or finds that the slope `df/dx` changes
|
|
982
|
+
by more than `max_slope_change` (10 %) within one error bar of the
|
|
983
|
+
solution; or gets a bracket that is not two finite numbers with
|
|
984
|
+
low < high, a reading that is not finite, a negative
|
|
985
|
+
`reading_sigma`, a `FitResult` of another model, both a result and
|
|
986
|
+
`theta=`/`cov=`, or a covariance that `propagate` would refuse;
|
|
987
|
+
- `runs_test` gets residuals that are not finite or not a 1-D array,
|
|
988
|
+
an `order` of the wrong length, fewer than 2 nonzero residuals, or
|
|
989
|
+
residuals that all have the same sign (no runs to count);
|
|
990
|
+
- `Model.numeric_jacobian` gets a `rel_step` that is not a finite
|
|
991
|
+
positive number (since 0.4.0; 0.3.0 failed with an
|
|
992
|
+
`UnboundLocalError`), and `coverage_exact` gets an `n` that is not
|
|
993
|
+
a whole number (since 0.4.0; 0.3.0 cut 29.9 to 29);
|
|
795
994
|
- `propagate` gets no covariance (`None`, as from a plan that is
|
|
796
995
|
not identifiable), one of the wrong size, one that is not
|
|
797
996
|
symmetric or has a negative diagonal, one that is not positive
|
|
@@ -818,7 +1017,7 @@ its inputs and conventions.
|
|
|
818
1017
|
|
|
819
1018
|
## How the results are checked
|
|
820
1019
|
|
|
821
|
-
|
|
1020
|
+
96 automated tests run on every push and pull request, on Python 3.9,
|
|
822
1021
|
3.10, 3.11, 3.12, 3.13 and 3.14, and once more on Python 3.9 with the
|
|
823
1022
|
oldest NumPy the package allows (1.22.0, with pytest 7.0.0). The
|
|
824
1023
|
numerical checks compare against a formula, an identity or seeded
|
|
@@ -1012,6 +1211,54 @@ from an earlier run. What the tests assert:
|
|
|
1012
1211
|
is read by column positions. Each refusal in the list above fires
|
|
1013
1212
|
with the expected message.
|
|
1014
1213
|
|
|
1214
|
+
**Added in 0.4.0**
|
|
1215
|
+
|
|
1216
|
+
- A decay of 10 on a background of 1000, with its time constant
|
|
1217
|
+
`tau = 1e-9` written in seconds (samples from `t = 0`): the slopes
|
|
1218
|
+
match the exact derivatives to 1e-5 of each column's largest value,
|
|
1219
|
+
the planned error bars match the exact `(J^T W J)^-1` to relative
|
|
1220
|
+
1e-5, and a fit from that guess equals the fit with exact slopes to
|
|
1221
|
+
1e-4 of an error bar (0.3.0 refused all three; this test fails on
|
|
1222
|
+
0.3.0). Where the fallback step already gave finite values the
|
|
1223
|
+
slopes are unchanged bit for bit. A `rel_step` of 0, a negative,
|
|
1224
|
+
NaN, infinite or non-numeric one, and a fractional `n` in
|
|
1225
|
+
`coverage_exact` are refused.
|
|
1226
|
+
- `design` (greedy, and with `exchange=True`) returns the same
|
|
1227
|
+
indices and the same information matrix, compared with `==`, as the
|
|
1228
|
+
0.3.0 loops, kept in the test as the reference, on 21 problems (a
|
|
1229
|
+
decay, a Gaussian peak with a sloped background and a plane in two
|
|
1230
|
+
settings, with 2 to 8 picks from 9 to 60 candidates, including
|
|
1231
|
+
exact ties from repeated candidates), also when the batches are cut
|
|
1232
|
+
to 7 candidates.
|
|
1233
|
+
- `inverse_predict` on a straight line equals the NIST/SEMATECH
|
|
1234
|
+
e-Handbook formula (section 2.3.6.7.3), typed into the test, for
|
|
1235
|
+
the value (relative 1e-12) and the error bar (relative 1e-7), and
|
|
1236
|
+
the covariance of three readings equals the closed form. On an
|
|
1237
|
+
exponential decay with background it equals the closed-form inverse
|
|
1238
|
+
`-tau ln((y - B) / A)` and the error bar from its partial
|
|
1239
|
+
derivatives (relative 1e-6). For 20000 seeded replicate
|
|
1240
|
+
calibrations (least squares done in the test with NumPy) followed
|
|
1241
|
+
by a new reading, the scatter of the inverse values matches the
|
|
1242
|
+
planned error bar within 3 %. On a saturating response the
|
|
1243
|
+
first-order error bar matches seeded simulation within 2 % where it
|
|
1244
|
+
is accepted; where it is refused (the slope changes by 65 % within
|
|
1245
|
+
one error bar) the simulated scatter is more than 25 % larger. Each
|
|
1246
|
+
refusal fires.
|
|
1247
|
+
- `runs_test`: for every n1 and n2 from 1 to 7 the tail probabilities,
|
|
1248
|
+
the mean and the variance of the number of runs equal the counts
|
|
1249
|
+
over all arrangements of the signs (relative 1e-12); for n1, n2 up
|
|
1250
|
+
to 900 and 700 the tails equal exact integer arithmetic (relative
|
|
1251
|
+
1e-9). On 4000 seeded sets of random signs and of residuals of
|
|
1252
|
+
correct straight-line fits the false-alarm rate at the 5 % level is
|
|
1253
|
+
at most 5 % plus 4.5 binomial standard errors; on curved readings
|
|
1254
|
+
fitted with a line and errors stated 3 times too large, chi-squared
|
|
1255
|
+
gives p = 0.994 while the runs test gives p = 0.001.
|
|
1256
|
+
- The two tutorials run and print exactly the output shown in their
|
|
1257
|
+
docstrings and in this README; their results agree with the values
|
|
1258
|
+
used to simulate the data within 3 error bars, and their printed
|
|
1259
|
+
numbers agree with each other (error bar parts adding in
|
|
1260
|
+
quadrature, the repeat count, the error bar of `1/tau`).
|
|
1261
|
+
|
|
1015
1262
|
Every refusal listed in "When it refuses, and why" is checked to
|
|
1016
1263
|
fire, including a fit that does not converge within `max_iter` steps
|
|
1017
1264
|
(a `RuntimeError`) and a parameter that no reading responds to (an
|
|
@@ -1022,6 +1269,29 @@ equal models hash equally.
|
|
|
1022
1269
|
|
|
1023
1270
|
## Corrections in earlier versions
|
|
1024
1271
|
|
|
1272
|
+
**0.4.0 fixed one wrong refusal.**
|
|
1273
|
+
|
|
1274
|
+
- *A small parameter whose effect is small next to the largest
|
|
1275
|
+
reading could be refused as "non-finite".* For a parameter whose
|
|
1276
|
+
relative step is too small to show next to the largest prediction,
|
|
1277
|
+
the slopes fall back to the 0.1.0 step `1e-6 * max(|theta|, 1e-3)`,
|
|
1278
|
+
which is 1e-9 for any parameter below 1e-3. For a time constant of
|
|
1279
|
+
1e-9 s that step is the parameter itself: `tau - h = 0`. A decay of
|
|
1280
|
+
10 on a background of 1000 (any amplitude below about 1/73 of the
|
|
1281
|
+
background) with `tau = 1e-9` and samples from `t = 0` then divided
|
|
1282
|
+
0 by 0, and `information`, `design` and `fit` stopped with "the
|
|
1283
|
+
forward function returned non-finite predictions; check theta and
|
|
1284
|
+
x", although the model is finite at that `tau`. 0.4.0 divides such
|
|
1285
|
+
a step by 10 until the model is finite and then checks it as
|
|
1286
|
+
before: the planned error bar on `tau` is 1.4189183e-10 s, equal to
|
|
1287
|
+
the exact value from the analytic slopes to 6e-9 relative. Results
|
|
1288
|
+
that 0.3.0 could compute are unchanged.
|
|
1289
|
+
|
|
1290
|
+
Also in 0.4.0: `Model.numeric_jacobian(rel_step=0)` (or a negative
|
|
1291
|
+
step) failed with an `UnboundLocalError` and now explains the
|
|
1292
|
+
problem; `coverage_exact(29.9, 0.1)` returned the answer for n = 29
|
|
1293
|
+
and now refuses.
|
|
1294
|
+
|
|
1025
1295
|
**0.3.0 fixed two wrong answers.**
|
|
1026
1296
|
|
|
1027
1297
|
- *A fit without `sigmas` could stop too early when the readings are
|
|
@@ -1150,9 +1420,25 @@ The full history is in [CHANGELOG.md](CHANGELOG.md).
|
|
|
1150
1420
|
possible. `exchange=True` improves it until no single swap helps,
|
|
1151
1421
|
which in the tests reached the best subset every time, but that is
|
|
1152
1422
|
still not a proof. Each swap pass tries every chosen setting
|
|
1153
|
-
against every unused one
|
|
1154
|
-
|
|
1155
|
-
|
|
1423
|
+
against every unused one; since 0.4.0 these are scored in batches
|
|
1424
|
+
(3000 candidates take a fraction of a second), but the work still
|
|
1425
|
+
grows with the number of picks times the number of candidates for
|
|
1426
|
+
every pass. It picks each candidate at most once; use `repeats_for`
|
|
1427
|
+
for repeats.
|
|
1428
|
+
- `inverse_predict` needs settings that are one number per
|
|
1429
|
+
measurement and a bracket in which the model reaches the reading
|
|
1430
|
+
exactly once. It scans the bracket at `n_scan` points, so two
|
|
1431
|
+
crossings closer together than the scan spacing are not seen. Its
|
|
1432
|
+
error bar is first order, like `propagate`; it refuses when the
|
|
1433
|
+
slope changes by more than 10 % within one error bar, and below that
|
|
1434
|
+
the error bar can still be a few percent too small where the curve
|
|
1435
|
+
bends (in the tests the simulated scatter was 1.014 times the error
|
|
1436
|
+
bar at a slope change of 10.5 %).
|
|
1437
|
+
- `runs_test` looks only at the signs, so it needs enough residuals
|
|
1438
|
+
to have power (with 10 residuals, 5 of each sign, the smallest
|
|
1439
|
+
possible `p_value` is 2/252 = 0.008). For
|
|
1440
|
+
residuals of a fit it is approximate (the residuals are not exactly
|
|
1441
|
+
independent); in the tests that made it more cautious, not less.
|
|
1156
1442
|
- The conformal guarantee is an average over calibration sets and new
|
|
1157
1443
|
measurements, not a promise at each setting, and it needs the
|
|
1158
1444
|
calibration data to be exchangeable with the new measurement:
|
|
@@ -1193,7 +1479,13 @@ Functions, chapter 8) by the series and continued fraction of W. H.
|
|
|
1193
1479
|
Press, S. A. Teukolsky, W. T. Vetterling and B. P. Flannery,
|
|
1194
1480
|
Numerical Recipes, 3rd ed., Cambridge University Press (2007),
|
|
1195
1481
|
section 6.2; the Birge ratio is from R. T. Birge, Phys. Rev. 40, 207
|
|
1196
|
-
(1932).
|
|
1482
|
+
(1932). The runs test is A. Wald and J. Wolfowitz, "On a test whether
|
|
1483
|
+
two samples are from the same population", Ann. Math. Statist. 11,
|
|
1484
|
+
147-162 (1940), doi:10.1214/aoms/1177731909. The straight-line case of
|
|
1485
|
+
`inverse_predict` is the propagation-of-error formula of the
|
|
1486
|
+
NIST/SEMATECH e-Handbook of Statistical Methods, section 2.3.6.7.3
|
|
1487
|
+
(https://itl.nist.gov/div898/handbook/mpc/section3/mpc3673.htm).
|
|
1488
|
+
First-order propagation of uncertainty with
|
|
1197
1489
|
covariances is the "law of propagation of uncertainty" of JCGM
|
|
1198
1490
|
100:2008, Evaluation of measurement data -- Guide to the expression
|
|
1199
1491
|
of uncertainty in measurement (sections 5.1.2 and 5.2). Split
|