labplan 0.2.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.2.0/src/labplan.egg-info → labplan-0.4.0}/PKG-INFO +582 -35
- {labplan-0.2.0 → labplan-0.4.0}/README.md +580 -32
- {labplan-0.2.0 → labplan-0.4.0}/pyproject.toml +4 -4
- {labplan-0.2.0 → labplan-0.4.0}/src/labplan/__init__.py +16 -6
- {labplan-0.2.0 → labplan-0.4.0}/src/labplan/conformal.py +8 -4
- {labplan-0.2.0 → labplan-0.4.0}/src/labplan/fit.py +110 -6
- labplan-0.4.0/src/labplan/inverse.py +309 -0
- {labplan-0.2.0 → labplan-0.4.0}/src/labplan/model.py +55 -6
- {labplan-0.2.0 → labplan-0.4.0}/src/labplan/plan.py +57 -14
- {labplan-0.2.0 → labplan-0.4.0}/src/labplan/propagate.py +31 -1
- labplan-0.4.0/src/labplan/records.py +275 -0
- {labplan-0.2.0 → labplan-0.4.0}/src/labplan/report.py +3 -2
- labplan-0.4.0/src/labplan/runs.py +155 -0
- {labplan-0.2.0 → labplan-0.4.0}/src/labplan/stats.py +74 -1
- {labplan-0.2.0 → labplan-0.4.0/src/labplan.egg-info}/PKG-INFO +582 -35
- {labplan-0.2.0 → labplan-0.4.0}/src/labplan.egg-info/SOURCES.txt +8 -1
- labplan-0.4.0/tests/test_fixes_030.py +158 -0
- 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_lab_data.py +262 -0
- labplan-0.4.0/tests/test_tutorials.py +77 -0
- labplan-0.2.0/src/labplan/records.py +0 -90
- {labplan-0.2.0 → labplan-0.4.0}/LICENSE +0 -0
- {labplan-0.2.0 → labplan-0.4.0}/setup.cfg +0 -0
- {labplan-0.2.0 → labplan-0.4.0}/src/labplan.egg-info/dependency_links.txt +0 -0
- {labplan-0.2.0 → labplan-0.4.0}/src/labplan.egg-info/requires.txt +0 -0
- {labplan-0.2.0 → labplan-0.4.0}/src/labplan.egg-info/top_level.txt +0 -0
- {labplan-0.2.0 → labplan-0.4.0}/tests/test_conformal_report.py +0 -0
- {labplan-0.2.0 → labplan-0.4.0}/tests/test_derivatives_design.py +0 -0
- {labplan-0.2.0 → labplan-0.4.0}/tests/test_documented_claims.py +0 -0
- {labplan-0.2.0 → labplan-0.4.0}/tests/test_fit_plan.py +0 -0
- {labplan-0.2.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
|
|
@@ -42,8 +41,12 @@ questions:
|
|
|
42
41
|
settings are worth the instrument time, and how many repeats do I
|
|
43
42
|
need for a target error bar?
|
|
44
43
|
- **After measuring:** what are the fitted numbers, and their error
|
|
45
|
-
bars?
|
|
46
|
-
|
|
44
|
+
bars? Are the readings consistent with the model and their stated
|
|
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.
|
|
47
50
|
- **If the model may be wrong:** how wide a band around the
|
|
48
51
|
prediction still covers new measurements at a chosen rate?
|
|
49
52
|
- **Later:** can anyone trace what was fitted, to exactly which data,
|
|
@@ -59,6 +62,7 @@ explains why.
|
|
|
59
62
|
- [A short guide to the words used here](#a-short-guide-to-the-words-used-here)
|
|
60
63
|
- [Install](#install)
|
|
61
64
|
- [Examples](#examples) (each with the output it prints)
|
|
65
|
+
- [Tutorials](#tutorials) (complete scripts, from data to result)
|
|
62
66
|
- [What is in the package](#what-is-in-the-package)
|
|
63
67
|
- [When it refuses, and why](#when-it-refuses-and-why)
|
|
64
68
|
- [How the results are checked](#how-the-results-are-checked)
|
|
@@ -112,6 +116,17 @@ explains why.
|
|
|
112
116
|
of freedom** (number of readings
|
|
113
117
|
minus number of parameters) it should be near 1 when the model and
|
|
114
118
|
the stated reading errors agree.
|
|
119
|
+
- **p-value**: here, the probability that chi2 would come out at
|
|
120
|
+
least as large as it did if the model and the stated reading errors
|
|
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.
|
|
115
130
|
- **Conformal prediction** -- a way to put a band around predictions
|
|
116
131
|
from held-out data alone, without trusting the model. The
|
|
117
132
|
**calibration scores** are the absolute errors
|
|
@@ -143,12 +158,13 @@ an error bar of 5 Hz on the corner frequency), and
|
|
|
143
158
|
`02_conformal_certificate.py` wraps a conformal band around a
|
|
144
159
|
deliberately wrong straight-line model and measures its coverage
|
|
145
160
|
over 300 simulated rounds (0.899, against the exact 0.9000 for its
|
|
146
|
-
79 calibration points).
|
|
161
|
+
79 calibration points). Two longer tutorials, also in `examples/`,
|
|
162
|
+
are described in [Tutorials](#tutorials) below.
|
|
147
163
|
|
|
148
164
|
## Examples
|
|
149
165
|
|
|
150
166
|
Each example below runs as written, and the output shown is what it
|
|
151
|
-
printed with labplan 0.
|
|
167
|
+
printed with labplan 0.4.0. The model, the numbers and the noise are
|
|
152
168
|
illustrative; the random numbers come from fixed seeds, so the
|
|
153
169
|
output is repeatable.
|
|
154
170
|
|
|
@@ -467,7 +483,7 @@ passed straight to `repeats_for`.
|
|
|
467
483
|
import numpy as np
|
|
468
484
|
from labplan import Model, information
|
|
469
485
|
|
|
470
|
-
# A Gaussian pulse, with times written in seconds:
|
|
486
|
+
# A Gaussian pulse, with times written in seconds: center t0, width w.
|
|
471
487
|
def pulse(th, x):
|
|
472
488
|
return th[0] * np.exp(-0.5 * ((x[:, 0] - th[1]) / th[2]) ** 2)
|
|
473
489
|
|
|
@@ -483,7 +499,7 @@ exact = Model("pulse", pulse, ("A", "t0", "w"),
|
|
|
483
499
|
reference="illustrative Gaussian pulse", jac=pulse_slopes)
|
|
484
500
|
|
|
485
501
|
t = np.linspace(-3e-9, 3e-9, 25) # 25 samples from -3 ns to 3 ns
|
|
486
|
-
guess = [1.0, 0.0, 1e-9] #
|
|
502
|
+
guess = [1.0, 0.0, 1e-9] # center exactly zero, width 1 ns
|
|
487
503
|
diff = np.abs(exact.jacobian(guess, t) - numeric.numeric_jacobian(guess, t))
|
|
488
504
|
print("largest slope difference / largest slope:",
|
|
489
505
|
f"{diff.max() / np.abs(exact.jacobian(guess, t)).max():.1e}")
|
|
@@ -502,10 +518,258 @@ planned error bar on t0 (exact slopes): 5.311777e-12 s
|
|
|
502
518
|
and `fit` then use; `numeric_jacobian` always computes the numerical
|
|
503
519
|
ones, so you can compare the two once, as here, to catch a mistake
|
|
504
520
|
in the hand-written slopes. The numerical slopes are also good here,
|
|
505
|
-
although the
|
|
521
|
+
although the center `t0` is exactly zero and there is no natural
|
|
506
522
|
step size for it (see "Corrections" below: version 0.1.1 printed
|
|
507
523
|
6.686976e-12 s, 26 % too large, for the numeric line).
|
|
508
524
|
|
|
525
|
+
### 11. Are the readings consistent with the model?
|
|
526
|
+
|
|
527
|
+
```python
|
|
528
|
+
import numpy as np
|
|
529
|
+
from labplan import Model, fit, goodness_of_fit
|
|
530
|
+
|
|
531
|
+
line = Model("sensor line",
|
|
532
|
+
lambda th, x: th[0] * x[:, 0] + th[1],
|
|
533
|
+
param_names=("gain", "offset"),
|
|
534
|
+
reference="illustrative straight-line sensor model")
|
|
535
|
+
x = np.linspace(0.5, 5.0, 12)
|
|
536
|
+
rng = np.random.default_rng(1)
|
|
537
|
+
y = 2.0 * x + 0.1 + 0.05 * rng.standard_normal(x.size) # true error 0.05
|
|
538
|
+
|
|
539
|
+
for stated in (0.05, 0.02): # the second is understated
|
|
540
|
+
res = fit(line, x, y, theta0=[1.0, 0.0], sigmas=stated)
|
|
541
|
+
g = goodness_of_fit(res)
|
|
542
|
+
print(f"sigmas={stated}: chi2 = {g['chi2']:.2f} for {g['dof']} "
|
|
543
|
+
f"degrees of freedom, p = {g['p_value']:.3g}, "
|
|
544
|
+
f"Birge ratio = {g['birge_ratio']:.2f}")
|
|
545
|
+
|
|
546
|
+
# A curved response fitted with a straight line, with the right errors:
|
|
547
|
+
y_curved = 2.0 * x + 0.1 + 0.05 * x ** 2 + 0.05 * rng.standard_normal(x.size)
|
|
548
|
+
g = goodness_of_fit(fit(line, x, y_curved, [1.0, 0.0], sigmas=0.05))
|
|
549
|
+
print(f"curved data: chi2 = {g['chi2']:.1f}, p = {g['p_value']:.2g}")
|
|
550
|
+
```
|
|
551
|
+
|
|
552
|
+
```
|
|
553
|
+
sigmas=0.05: chi2 = 4.10 for 10 degrees of freedom, p = 0.943, Birge ratio = 0.64
|
|
554
|
+
sigmas=0.02: chi2 = 25.62 for 10 degrees of freedom, p = 0.00429, Birge ratio = 1.60
|
|
555
|
+
curved data: chi2 = 49.0, p = 4e-07
|
|
556
|
+
```
|
|
557
|
+
|
|
558
|
+
`goodness_of_fit` needs a fit made with `sigmas`. Its `p_value` is
|
|
559
|
+
the probability of a chi2 at least this large if the model and the
|
|
560
|
+
stated errors are both right. With the true reading error (0.05) the
|
|
561
|
+
fit is unremarkable. With an error stated 2.5 times too small, chi2 is
|
|
562
|
+
6.25 times larger and p falls to 0.4 %. A straight line fitted to
|
|
563
|
+
readings that bend gives p = 4e-07 although the errors are right. The
|
|
564
|
+
test cannot say which of the two is wrong, only that something is.
|
|
565
|
+
`p_value_low` (also returned) is the opposite tail: a tiny value
|
|
566
|
+
means the readings agree better than their errors allow, so the
|
|
567
|
+
errors are probably overstated. The Birge ratio `sqrt(chi2 / dof)` is
|
|
568
|
+
the factor by which the stated errors would have to change for chi2
|
|
569
|
+
to equal its degrees of freedom. You can also call
|
|
570
|
+
`goodness_of_fit(chi2=..., dof=...)` with numbers from any other fit.
|
|
571
|
+
|
|
572
|
+
### 12. Read an instrument's exported table
|
|
573
|
+
|
|
574
|
+
```python
|
|
575
|
+
import os, tempfile
|
|
576
|
+
from labplan import Model, fit, load_columns
|
|
577
|
+
|
|
578
|
+
# A sweep as an instrument might export it (illustrative numbers):
|
|
579
|
+
# a comment line, a header, semicolons, decimal commas, extra columns.
|
|
580
|
+
export = """# leakage sweep, bench 2
|
|
581
|
+
Time (s);Bias (V);Current (A);Current error (A);Status
|
|
582
|
+
0;0,5;1,04E-12;5E-14;OK
|
|
583
|
+
2;1,0;2,01E-12;5E-14;OK
|
|
584
|
+
4;1,5;3,05E-12;5E-14;OK
|
|
585
|
+
6;2,0;3,98E-12;5E-14;OK
|
|
586
|
+
8;2,5;5,02E-12;5E-14;OK
|
|
587
|
+
"""
|
|
588
|
+
path = os.path.join(tempfile.mkdtemp(), "sweep.csv")
|
|
589
|
+
with open(path, "w") as fh:
|
|
590
|
+
fh.write(export)
|
|
591
|
+
|
|
592
|
+
x, y, s = load_columns(path, x="Bias (V)", y="Current (A)",
|
|
593
|
+
sigma="Current error (A)", decimal=",")
|
|
594
|
+
print("bias:", x[:, 0], " current:", y)
|
|
595
|
+
|
|
596
|
+
leak = Model("leakage", lambda th, x: th[0] * x[:, 0] + th[1],
|
|
597
|
+
param_names=("G", "I0"),
|
|
598
|
+
reference="illustrative ohmic leakage with an offset current",
|
|
599
|
+
units={"G": "S", "I0": "A"})
|
|
600
|
+
res = fit(leak, x, y, theta0=[0.0, 0.0], sigmas=s)
|
|
601
|
+
print(f"G = {res.values['G']:.4e} +/- {res.sigma['G']:.1e} S")
|
|
602
|
+
|
|
603
|
+
# A cell the instrument marked as overload is refused, not skipped:
|
|
604
|
+
with open(path, "a") as fh:
|
|
605
|
+
fh.write("10;3,0;OVLD;5E-14;OVERLOAD\n")
|
|
606
|
+
try:
|
|
607
|
+
load_columns(path, x="Bias (V)", y="Current (A)", decimal=",")
|
|
608
|
+
except ValueError as err:
|
|
609
|
+
print("refused:", err)
|
|
610
|
+
```
|
|
611
|
+
|
|
612
|
+
```
|
|
613
|
+
bias: [0.5 1. 1.5 2. 2.5] current: [1.04e-12 2.01e-12 3.05e-12 3.98e-12 5.02e-12]
|
|
614
|
+
G = 1.9860e-12 +/- 3.2e-14 S
|
|
615
|
+
refused: line 8, column 'Current (A)': 'OVLD' is not a number
|
|
616
|
+
```
|
|
617
|
+
|
|
618
|
+
`load_columns` reads the columns you name from a delimited text file
|
|
619
|
+
with a header row: comma, tab, semicolon or space separated (found
|
|
620
|
+
from the header, or given with `delimiter=`), with `.` or `,` as the
|
|
621
|
+
decimal mark. Comment lines (starting with `#`) and blank lines are
|
|
622
|
+
skipped, and `skip_rows=` skips a preamble that is not marked as
|
|
623
|
+
comment. Other columns may hold anything and are not read. Every
|
|
624
|
+
value it returns was read as a finite number from the cell you named.
|
|
625
|
+
An empty cell, text such as `OVLD`, `nan` or `inf`, or a row with
|
|
626
|
+
the wrong number of fields stops it with the line number, because
|
|
627
|
+
skipping such rows silently would change the data you fit. The
|
|
628
|
+
result `(x, y, sigmas)` goes straight into `fit`.
|
|
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
|
+
|
|
509
773
|
## What is in the package
|
|
510
774
|
|
|
511
775
|
**The model**
|
|
@@ -534,7 +798,12 @@ step size for it (see "Corrections" below: version 0.1.1 printed
|
|
|
534
798
|
fallback step is checked against a step 10 times smaller; while
|
|
535
799
|
the two disagree by more than 1e-6 of the largest slope (plus an
|
|
536
800
|
allowance for rounding), the smaller step is taken, at most 8
|
|
537
|
-
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.
|
|
538
807
|
|
|
539
808
|
**Planning**
|
|
540
809
|
|
|
@@ -555,7 +824,10 @@ step size for it (see "Corrections" below: version 0.1.1 printed
|
|
|
555
824
|
a swapped-in setting taking the place of the one it replaced), the
|
|
556
825
|
information matrix, `identifiable`, the condition number, the error
|
|
557
826
|
bars and the covariance; the result can be passed to
|
|
558
|
-
`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).
|
|
559
831
|
- `repeats_for(target_sigma, plan)` -- the number of repeats `r` of a
|
|
560
832
|
planned design needed to bring every error bar down to a target,
|
|
561
833
|
from the rule that `r` repeats divide the error bars by `sqrt(r)`.
|
|
@@ -571,7 +843,10 @@ step size for it (see "Corrections" below: version 0.1.1 printed
|
|
|
571
843
|
`J C J^T`, where `C` is the parameter covariance and `J` holds the
|
|
572
844
|
numerical slopes of `func`. Use the covariance of a fit, or of a
|
|
573
845
|
plan for the error bars you should expect. Values and error bars
|
|
574
|
-
come back as plain numbers when `func` returns one number.
|
|
846
|
+
come back as plain numbers when `func` returns one number. Since
|
|
847
|
+
0.3.0 a covariance that is not positive semidefinite (one that
|
|
848
|
+
would give some combination of parameters a negative variance) is
|
|
849
|
+
refused.
|
|
575
850
|
|
|
576
851
|
**Fitting**
|
|
577
852
|
|
|
@@ -579,16 +854,53 @@ step size for it (see "Corrections" below: version 0.1.1 printed
|
|
|
579
854
|
weighted least squares by the Levenberg-Marquardt method (a
|
|
580
855
|
standard step-by-step search that adjusts the parameters until the
|
|
581
856
|
weighted sum of squared differences between readings and
|
|
582
|
-
predictions stops decreasing), written in NumPy. With `sigmas`, the
|
|
583
|
-
matrix and chi2 is reported;
|
|
584
|
-
|
|
585
|
-
least one more reading
|
|
857
|
+
predictions stops decreasing), written in NumPy. With `sigmas`, the
|
|
858
|
+
covariance is the inverse information matrix and chi2 is reported;
|
|
859
|
+
without them, the covariance is scaled by the residual scatter
|
|
860
|
+
(chi2 / degrees of freedom), which needs at least one more reading
|
|
861
|
+
than parameters. The search stops when a step changes the weighted
|
|
862
|
+
sum of squares by at most `tol` times (one unit + the sum) and
|
|
863
|
+
moves no parameter by more than 1e-10 times (1 + the largest
|
|
864
|
+
parameter). With `sigmas` the unit is 1; without them, since 0.3.0,
|
|
865
|
+
it is the residual variance (at most 1), so the result no longer
|
|
866
|
+
depends on the units of the readings (see Corrections).
|
|
586
867
|
- `FitResult` -- what `fit` returns: `values` and `sigma`
|
|
587
868
|
(dictionaries by parameter name), `theta` and `cov` (arrays),
|
|
588
869
|
`chi2`, `chi2_dof`, `n_points`, `condition_number`, `n_iter`,
|
|
589
870
|
`model_name`, `reference`, `units` (the model's note) and
|
|
590
871
|
`data_digest` (the sha256 of the settings, readings and reading
|
|
591
872
|
errors).
|
|
873
|
+
- `goodness_of_fit(result)` or `goodness_of_fit(chi2=..., dof=...)`
|
|
874
|
+
(new in 0.3.0): the chi-squared test. It returns `chi2`, `dof`,
|
|
875
|
+
`reduced_chi2` (chi2 / dof), `p_value` (probability of a chi2 at
|
|
876
|
+
least this large if the model and the stated errors are right),
|
|
877
|
+
`p_value_low` (probability of one at most this large) and
|
|
878
|
+
`birge_ratio` (`sqrt(chi2 / dof)`; R. T. Birge, Phys. Rev. 40, 207
|
|
879
|
+
(1932)). The probabilities are the regularized incomplete gamma
|
|
880
|
+
functions `Q(dof/2, chi2/2)` and `P(dof/2, chi2/2)`, computed by
|
|
881
|
+
the series and continued fraction of Press et al., Numerical
|
|
882
|
+
Recipes, 3rd ed. (2007), section 6.2, so that small values keep
|
|
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.
|
|
592
904
|
|
|
593
905
|
**When the model may be wrong** (split conformal prediction)
|
|
594
906
|
|
|
@@ -613,6 +925,13 @@ step size for it (see "Corrections" below: version 0.1.1 printed
|
|
|
613
925
|
`load_measurements_csv(path)` -- a plain CSV file of measurements;
|
|
614
926
|
loading returns `(x, y, sigmas)`, with `sigmas` `None` when the file
|
|
615
927
|
has no sigma column.
|
|
928
|
+
- `load_columns(path, x, y, sigma=None, delimiter=None, decimal=".",
|
|
929
|
+
comment="#", skip_rows=0, header=True)` (new in 0.3.0): reads
|
|
930
|
+
the named columns (header names, or positions counted from 0) of
|
|
931
|
+
any delimited text table, such as an instrument's or a
|
|
932
|
+
spreadsheet's export, and returns `(x, y, sigmas)` like
|
|
933
|
+
`load_measurements_csv`. `x` can be one column or a list of
|
|
934
|
+
columns. See example 12.
|
|
616
935
|
- `__version__` -- the package version.
|
|
617
936
|
|
|
618
937
|
Each function's docstring (`help(labplan.fit)`, for example) gives
|
|
@@ -642,30 +961,63 @@ its inputs and conventions.
|
|
|
642
961
|
there are fewer settings than parameters;
|
|
643
962
|
- `fit` gets fewer readings than parameters, or, without `sigmas`,
|
|
644
963
|
no spare reading to estimate the scatter from;
|
|
645
|
-
- `fit` does not converge within `max_iter` steps (a `RuntimeError`)
|
|
964
|
+
- `fit` does not converge within `max_iter` steps (a `RuntimeError`),
|
|
965
|
+
or gets a `max_iter` below 1 or a negative `tol` (since 0.3.0);
|
|
646
966
|
- `design` is asked for fewer picks than parameters or more picks
|
|
647
|
-
than candidates
|
|
967
|
+
than candidates, or (since 0.3.0) for a number of picks that is not
|
|
968
|
+
a whole number, such as 2.5, which 0.2.0 silently cut to 2;
|
|
648
969
|
- `repeats_for` gets a plan that is not identifiable (no number of
|
|
649
|
-
repeats can fix that), an unknown parameter name,
|
|
650
|
-
is not positive
|
|
970
|
+
repeats can fix that), an unknown parameter name, a target that
|
|
971
|
+
is not positive, or (since 0.3.0, with a clear message) an empty
|
|
972
|
+
dictionary of targets;
|
|
651
973
|
- `conformal_quantile` gets negative or non-finite scores, an alpha
|
|
652
974
|
outside 0 to 1, or too few scores for the level (the message names
|
|
653
975
|
the minimum); `coverage_exact` refuses the same levels;
|
|
654
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);
|
|
655
994
|
- `propagate` gets no covariance (`None`, as from a plan that is
|
|
656
995
|
not identifiable), one of the wrong size, one that is not
|
|
657
|
-
symmetric or has a negative diagonal,
|
|
996
|
+
symmetric or has a negative diagonal, one that is not positive
|
|
997
|
+
semidefinite (since 0.3.0), or a `func` that cannot be
|
|
658
998
|
called, returns non-finite values, or returns an array with more
|
|
659
999
|
than one dimension (a table instead of a list of numbers);
|
|
660
1000
|
- `audit_record` gets an operator or note that is not text, or
|
|
661
1001
|
`report_text` gets something that is not an audit record;
|
|
1002
|
+
- `goodness_of_fit` gets a fit made without `sigmas` (it has no
|
|
1003
|
+
chi2), zero degrees of freedom, a negative or non-finite chi2, a
|
|
1004
|
+
`dof` that is not a whole number, or both a fit and numbers;
|
|
662
1005
|
- a measurement file is empty, has an unexpected header, has no data
|
|
663
1006
|
rows, has a row with the wrong number of values, or has a value that
|
|
664
|
-
is not a number
|
|
1007
|
+
is not a number;
|
|
1008
|
+
- `load_columns` is asked for a column name that is not in the header
|
|
1009
|
+
(the message lists the names that are), a name that appears twice,
|
|
1010
|
+
a position outside the table, or the same column twice; or finds a
|
|
1011
|
+
row with a different number of fields than the header, an empty
|
|
1012
|
+
cell, text, NaN or infinity in a requested column, a reading error
|
|
1013
|
+
that is not positive, a header in which more than one of comma, tab
|
|
1014
|
+
and semicolon occurs (pass `delimiter=`), a `.` in a number when
|
|
1015
|
+
`decimal=","` (a thousands separator?), or no data rows. Each
|
|
1016
|
+
message names the line.
|
|
665
1017
|
|
|
666
1018
|
## How the results are checked
|
|
667
1019
|
|
|
668
|
-
|
|
1020
|
+
96 automated tests run on every push and pull request, on Python 3.9,
|
|
669
1021
|
3.10, 3.11, 3.12, 3.13 and 3.14, and once more on Python 3.9 with the
|
|
670
1022
|
oldest NumPy the package allows (1.22.0, with pytest 7.0.0). The
|
|
671
1023
|
numerical checks compare against a formula, an identity or seeded
|
|
@@ -717,7 +1069,7 @@ from an earlier run. What the tests assert:
|
|
|
717
1069
|
|
|
718
1070
|
**Slopes: parameters at zero and exact slopes** (added in 0.2.0)
|
|
719
1071
|
|
|
720
|
-
- A Gaussian pulse written in seconds with
|
|
1072
|
+
- A Gaussian pulse written in seconds with center `t0 = 0` and width
|
|
721
1073
|
1 ns: every column of the numerical slopes matches the exact
|
|
722
1074
|
derivative to 1e-5 of the column's largest value; the planned error
|
|
723
1075
|
bars match the exact `(J^T W J)^-1` to relative 1e-5, and equal
|
|
@@ -815,6 +1167,98 @@ from an earlier run. What the tests assert:
|
|
|
815
1167
|
- The version in the package matches `pyproject.toml` and
|
|
816
1168
|
`CITATION.cff`, and every name in `__all__` exists.
|
|
817
1169
|
|
|
1170
|
+
**Added in 0.3.0**
|
|
1171
|
+
|
|
1172
|
+
- A straight line fitted without `sigmas` to readings scaled by 1,
|
|
1173
|
+
1e-3, 1e-6, 1e-9, 1e-12 and 1e-15 gives the values of
|
|
1174
|
+
`np.linalg.lstsq` to 1e-6 of their standard errors and the error
|
|
1175
|
+
bars of `s^2 (X^T X)^-1` to relative 1e-6, at every scale (0.2.0
|
|
1176
|
+
was 1.3 standard errors off at 1e-12; this test fails on 0.2.0).
|
|
1177
|
+
Noiseless lines and exponential decays at scales 1, 1e-6 and 1e-12
|
|
1178
|
+
still converge to the truth (relative 1e-12 and 1e-9). A weighted
|
|
1179
|
+
fit of the same small readings equals weighted least squares from
|
|
1180
|
+
`np.linalg.lstsq`.
|
|
1181
|
+
- `propagate` accepts a fitted covariance and gives the closed form
|
|
1182
|
+
`a C a^T` (relative 1e-6), but refuses the same covariance copied to
|
|
1183
|
+
3 significant digits (it has a negative eigenvalue; 0.2.0 returned
|
|
1184
|
+
an error bar of 0) and a covariance with a correlation of 2. It
|
|
1185
|
+
accepts a parameter held fixed (zero variance and covariances) and
|
|
1186
|
+
a planned covariance with condition number 4e9, just inside the
|
|
1187
|
+
limit.
|
|
1188
|
+
- `goodness_of_fit`: for every number of degrees of freedom from 1 to
|
|
1189
|
+
80 and twelve values of chi2 from 1e-8 to 1200, `p_value` equals
|
|
1190
|
+
the closed form computed in the test with the standard library (the
|
|
1191
|
+
finite Poisson sum for even degrees of freedom; `erfc` plus the
|
|
1192
|
+
recurrence `Q(a + 1, x) = Q(a, x) + x^a e^-x / Gamma(a + 1)` for odd
|
|
1193
|
+
ones) to relative 1e-12, and `p_value + p_value_low = 1`. Small
|
|
1194
|
+
lower tails match `-expm1(-chi2/2)` (2 degrees of freedom) and
|
|
1195
|
+
`erf(sqrt(chi2/2))` (1 degree of freedom) to relative 1e-13.
|
|
1196
|
+
`p_value_low` equals Simpson's-rule integration of the chi-squared
|
|
1197
|
+
density to 1e-10 for 20 cases, and `p_value` matches the fraction
|
|
1198
|
+
of 20000 seeded chi-squared draws above three quantiles within 4.5
|
|
1199
|
+
binomial standard errors. For 400 seeded straight-line fits with
|
|
1200
|
+
correct Gaussian errors the p-values are uniform: each tenth of
|
|
1201
|
+
(0, 1) holds 40 of them within 4.5 binomial standard deviations,
|
|
1202
|
+
and their mean is 1/2 within 4.5 standard errors.
|
|
1203
|
+
- `load_columns` reads a commented, comma-separated export with a
|
|
1204
|
+
quoted text column and a blank line back to the numbers typed into
|
|
1205
|
+
it (`np.array_equal`), and agrees exactly with `np.genfromtxt` on
|
|
1206
|
+
the same file; tab, semicolon and space separated tables agree with
|
|
1207
|
+
the written numbers and with `np.genfromtxt`; a semicolon file with
|
|
1208
|
+
decimal commas and a byte-order mark is read correctly; a file
|
|
1209
|
+
written by `save_measurements_csv` reads back identically; a
|
|
1210
|
+
preamble is skipped with `skip_rows`, and a file without a header
|
|
1211
|
+
is read by column positions. Each refusal in the list above fires
|
|
1212
|
+
with the expected message.
|
|
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
|
+
|
|
818
1262
|
Every refusal listed in "When it refuses, and why" is checked to
|
|
819
1263
|
fire, including a fit that does not converge within `max_iter` steps
|
|
820
1264
|
(a `RuntimeError`) and a parameter that no reading responds to (an
|
|
@@ -825,15 +1269,76 @@ equal models hash equally.
|
|
|
825
1269
|
|
|
826
1270
|
## Corrections in earlier versions
|
|
827
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
|
+
|
|
1295
|
+
**0.3.0 fixed two wrong answers.**
|
|
1296
|
+
|
|
1297
|
+
- *A fit without `sigmas` could stop too early when the readings are
|
|
1298
|
+
small numbers in their own units.* Without `sigmas` the quantity
|
|
1299
|
+
being minimized is the sum of squared residuals, in the readings'
|
|
1300
|
+
units squared. The search stopped once a step changed it by less
|
|
1301
|
+
than `tol * (1 + sum)`, and that fixed 1 is enormous for readings
|
|
1302
|
+
of order 1e-12 (a current of a few picoamperes written in
|
|
1303
|
+
amperes): every change was below it, and the fit stopped after its
|
|
1304
|
+
first, still damped, step. For a straight line through 12 such
|
|
1305
|
+
readings (gain 2e-12 A/V, noise 5e-14 A) 0.2.0 returned gain
|
|
1306
|
+
1.99116e-12 and offset 1.35943e-13 after 1 step, 1.30 and 1.15
|
|
1307
|
+
standard errors away from the least-squares answer (2.00051e-12
|
|
1308
|
+
and 1.10355e-13), with error bars about 10 % too large (7.18e-15
|
|
1309
|
+
instead of 6.54e-15 on the gain). With readings of order 1e-9 and
|
|
1310
|
+
relative noise 1e-5 it stopped 0.60 standard errors off. 0.3.0
|
|
1311
|
+
measures the change in units of the residual variance (at most 1)
|
|
1312
|
+
and gives the least-squares answer to 1e-6 of a standard error at
|
|
1313
|
+
every scale tested. Fits with `sigmas` are unchanged bit for bit;
|
|
1314
|
+
fits without them whose residual variance is below 1 may take a
|
|
1315
|
+
step or two more and then change far below their error bars (in
|
|
1316
|
+
300 random straight-line fits with readings from 1e-3 to 1e3, 253
|
|
1317
|
+
gave identical results and the largest change was 3.4e-6 of a
|
|
1318
|
+
standard error, the size of the rounding differences between
|
|
1319
|
+
`fit` and `np.linalg.lstsq` themselves). If you used 0.2.0
|
|
1320
|
+
or earlier without `sigmas` on readings much smaller than 1, please
|
|
1321
|
+
re-run those fits.
|
|
1322
|
+
- *`propagate` returned an error bar of 0 for an invalid covariance.*
|
|
1323
|
+
A covariance that is not positive semidefinite gives some
|
|
1324
|
+
combination of parameters a negative variance. That happens, for
|
|
1325
|
+
example, when the covariance of a strongly correlated fit is copied
|
|
1326
|
+
to a few digits: a line measured at settings 100 to 101 has slope
|
|
1327
|
+
and offset correlated at -0.999995, and its covariance rounded to
|
|
1328
|
+
3 significant digits has a negative eigenvalue. With it, the error
|
|
1329
|
+
bar of the predicted reading at 100.5 came out as 0.0 (the right
|
|
1330
|
+
value is 0.0144), with a negative variance in the returned `cov`.
|
|
1331
|
+
Such a matrix is now refused with an explanation.
|
|
1332
|
+
|
|
828
1333
|
**0.2.0 fixed three wrong answers.**
|
|
829
1334
|
|
|
830
1335
|
- *Slopes for a parameter at zero.* When a parameter was exactly zero
|
|
831
1336
|
(or too small for its relative step to show), 0.1.1 used a fixed
|
|
832
1337
|
step of 1e-9 in the parameter's own units, which can be far too
|
|
833
|
-
large. For a Gaussian pulse written in seconds, with
|
|
1338
|
+
large. For a Gaussian pulse written in seconds, with center 0 and
|
|
834
1339
|
width 1 ns, that step equals the width: the slope with respect to
|
|
835
|
-
the
|
|
836
|
-
error bar on the
|
|
1340
|
+
the center was 31 % wrong, and both the planned and the fitted
|
|
1341
|
+
error bar on the center came out as 6.687e-12 s instead of the
|
|
837
1342
|
exact 5.312e-12 s (26 % too large). The step is now checked
|
|
838
1343
|
against a 10 times smaller one and reduced while they disagree;
|
|
839
1344
|
0.2.0 gives 5.312e-12 s. Where the fixed step was already accurate
|
|
@@ -901,14 +1406,39 @@ The full history is in [CHANGELOG.md](CHANGELOG.md).
|
|
|
901
1406
|
- `propagate` is first order: exact for straight-line functions of
|
|
902
1407
|
the parameters, an approximation otherwise that is good when the
|
|
903
1408
|
function is close to a straight line within about one error bar.
|
|
1409
|
+
It refuses a covariance that is not positive semidefinite, but it
|
|
1410
|
+
cannot tell when a valid one was rounded: copy covariances at full
|
|
1411
|
+
precision.
|
|
1412
|
+
- `goodness_of_fit` assumes independent Gaussian reading errors of the
|
|
1413
|
+
stated size. Its chi-squared distribution is exact for models that
|
|
1414
|
+
are straight-line functions of their parameters and approximate
|
|
1415
|
+
otherwise. A small p-value says that the model or the stated errors
|
|
1416
|
+
are wrong, not which one, and a large one does not prove the model
|
|
1417
|
+
right: with few readings or large errors the test has little power.
|
|
904
1418
|
- `design` is a greedy heuristic: it adds the best single setting at
|
|
905
1419
|
each step and does not prove that the chosen set is the best
|
|
906
1420
|
possible. `exchange=True` improves it until no single swap helps,
|
|
907
1421
|
which in the tests reached the best subset every time, but that is
|
|
908
1422
|
still not a proof. Each swap pass tries every chosen setting
|
|
909
|
-
against every unused one
|
|
910
|
-
|
|
911
|
-
|
|
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.
|
|
912
1442
|
- The conformal guarantee is an average over calibration sets and new
|
|
913
1443
|
measurements, not a promise at each setting, and it needs the
|
|
914
1444
|
calibration data to be exchangeable with the new measurement:
|
|
@@ -917,14 +1447,19 @@ The full history is in [CHANGELOG.md](CHANGELOG.md).
|
|
|
917
1447
|
assumes scores without ties. `alpha` is read to about 9
|
|
918
1448
|
significant digits: an alpha that differs from a "round" value by
|
|
919
1449
|
less than 1e-9 relative is treated as that value.
|
|
920
|
-
-
|
|
921
|
-
|
|
1450
|
+
- `save_measurements_csv` and `load_measurements_csv` use `labplan`'s
|
|
1451
|
+
own CSV layout (`x1, ..., y, sigma`). Other delimited text tables
|
|
1452
|
+
are read with `load_columns` (since 0.3.0), which needs one header
|
|
1453
|
+
line (or none), one row per line (no line breaks inside quoted
|
|
1454
|
+
fields) and names given exactly as in the header. It does not read
|
|
1455
|
+
binary instrument formats or spreadsheet files (`.xlsx`); export
|
|
1456
|
+
those as text first.
|
|
922
1457
|
- No physics ships with the package. Your model and its `reference`
|
|
923
1458
|
carry the physics.
|
|
924
1459
|
|
|
925
1460
|
## Where it comes from
|
|
926
1461
|
|
|
927
|
-
Nine research packages in this organization -- covering
|
|
1462
|
+
Nine research packages in this organization -- covering color-center
|
|
928
1463
|
spins, squeezed light, spin-squeezed clocks, Raman maps, single-photon
|
|
929
1464
|
emitters, superconducting detectors, band structure, semiconductor
|
|
930
1465
|
heterostructures and photonic fabrication -- each grew the same
|
|
@@ -938,7 +1473,19 @@ Theoretical Statistics (1974); any statistics text under
|
|
|
938
1473
|
"Cramer-Rao bound"). D-optimal design follows F. Pukelsheim, Optimal
|
|
939
1474
|
Design of Experiments, SIAM (2006), and the swap refinement the
|
|
940
1475
|
exchange idea of V. V. Fedorov, Theory of Optimal Experiments,
|
|
941
|
-
Academic Press (1972).
|
|
1476
|
+
Academic Press (1972). The chi-squared test computes the regularized
|
|
1477
|
+
incomplete gamma functions (NIST Digital Library of Mathematical
|
|
1478
|
+
Functions, chapter 8) by the series and continued fraction of W. H.
|
|
1479
|
+
Press, S. A. Teukolsky, W. T. Vetterling and B. P. Flannery,
|
|
1480
|
+
Numerical Recipes, 3rd ed., Cambridge University Press (2007),
|
|
1481
|
+
section 6.2; the Birge ratio is from R. T. Birge, Phys. Rev. 40, 207
|
|
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
|
|
942
1489
|
covariances is the "law of propagation of uncertainty" of JCGM
|
|
943
1490
|
100:2008, Evaluation of measurement data -- Guide to the expression
|
|
944
1491
|
of uncertainty in measurement (sections 5.1.2 and 5.2). Split
|