Skip to content

Commit c323442

Browse files
authored
fix jac+OLS (#3)
1 parent 2ad1538 commit c323442

8 files changed

Lines changed: 136 additions & 125 deletions

File tree

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@
6868
"typeinfo": "cpp",
6969
"valarray": "cpp",
7070
"variant": "cpp",
71-
"ranges": "cpp"
71+
"ranges": "cpp",
72+
"strstream": "cpp",
73+
"bitset": "cpp"
7274
},
7375
"autopep8.args": [
7476
"--max-line-length=90"

docs/examples/explicit-model.ipynb

Lines changed: 11 additions & 13 deletions
Large diffs are not rendered by default.

docs/examples/implicit-model.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"Estimate the parameters of an ellipse from a set of coordinates.\n",
1919
"\n",
2020
"$$\\begin{align*}\n",
21-
"f(\\bm{\\beta}, \\bm{X}) & = \\frac{\\left[(x-x_0)\\cos\\theta + (y-y_0)\\sin\\theta\\right]^2}{a^2} \\\\\n",
21+
"f(\\bm{X}, \\bm{\\beta}) & = \\frac{\\left[(x-x_0)\\cos\\theta + (y-y_0)\\sin\\theta\\right]^2}{a^2} \\\\\n",
2222
"& + \\frac{\\left[(y-y_0)\\cos\\theta -(x-x_0)\\sin\\theta\\right]^2}{b^2} - 1 = 0\n",
2323
"\\end{align*}$$\n",
2424
"\n",

mkdocs.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ plugins:
5252
handlers:
5353
python:
5454
paths: [src]
55-
import:
56-
- https://docs.python.org/3/objects.inv
57-
- https://numpy.org/doc/stable/objects.inv
55+
# import:
56+
# - https://docs.python.org/3/objects.inv
57+
# - https://numpy.org/doc/stable/objects.inv
5858
options:
5959
modernize_annotations: true
6060
docstring_section_style: spacy

src/odrpack/odr_fortran.py

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,25 @@
77
from odrpack.__odrpack import loc_iwork, loc_rwork
88
from odrpack.__odrpack import odr as _odr
99
from odrpack.__odrpack import workspace_dimensions
10-
from odrpack.result import OdrResult
10+
from odrpack.result import OdrResult, I32Array, F64Array
1111

1212
__all__ = ['odr']
1313

1414

15-
def odr(f: Callable[[NDArray[np.float64], NDArray[np.float64]], NDArray[np.float64]],
16-
beta0: NDArray[np.float64],
17-
y: NDArray[np.float64],
18-
x: NDArray[np.float64],
15+
def odr(f: Callable[[F64Array, F64Array], F64Array],
16+
beta0: F64Array,
17+
y: F64Array,
18+
x: F64Array,
1919
*,
20-
we: float | NDArray[np.float64] | None = None,
21-
wd: float | NDArray[np.float64] | None = None,
22-
fjacb: Callable[[NDArray[np.float64], NDArray[np.float64]],
23-
NDArray[np.float64]] | None = None,
24-
fjacd: Callable[[NDArray[np.float64], NDArray[np.float64]],
25-
NDArray[np.float64]] | None = None,
26-
ifixb: NDArray[np.int32] | None = None,
27-
ifixx: NDArray[np.int32] | None = None,
28-
delta0: NDArray[np.float64] | None = None,
29-
lower: NDArray[np.float64] | None = None,
30-
upper: NDArray[np.float64] | None = None,
20+
we: float | F64Array | None = None,
21+
wd: float | F64Array | None = None,
22+
fjacb: Callable[[F64Array, F64Array], F64Array] | None = None,
23+
fjacd: Callable[[F64Array, F64Array], F64Array] | None = None,
24+
ifixb: I32Array | None = None,
25+
ifixx: I32Array | None = None,
26+
delta0: F64Array | None = None,
27+
lower: F64Array | None = None,
28+
upper: F64Array | None = None,
3129
job: int = 0,
3230
iprint: int | None = 0,
3331
rptfile: str | None = None,
@@ -37,37 +35,37 @@ def odr(f: Callable[[NDArray[np.float64], NDArray[np.float64]], NDArray[np.float
3735
sstol: float | None = None,
3836
partol: float | None = None,
3937
maxit: int | None = None,
40-
stpb: NDArray[np.float64] | None = None,
41-
stpd: NDArray[np.float64] | None = None,
42-
sclb: NDArray[np.float64] | None = None,
43-
scld: NDArray[np.float64] | None = None,
44-
rwork: NDArray[np.float64] | None = None,
45-
iwork: NDArray[np.int32] | None = None,
38+
stpb: F64Array | None = None,
39+
stpd: F64Array | None = None,
40+
sclb: F64Array | None = None,
41+
scld: F64Array | None = None,
42+
rwork: F64Array | None = None,
43+
iwork: I32Array | None = None,
4644
) -> OdrResult:
4745
r"""Solve a weighted orthogonal distance regression (ODR) problem, also
4846
known as errors-in-variables regression.
4947
5048
Parameters
5149
----------
52-
f : Callable[[NDArray[np.float64], NDArray[np.float64]], NDArray[np.float64]]
50+
f : Callable[[F64Array, F64Array], F64Array]
5351
Function to be fitted, with the signature `f(beta, x)`. It must return
5452
an array with the same shape as `y`.
55-
beta0 : NDArray[np.float64]
53+
beta0 : F64Array
5654
Array of shape `(npar,)` with the initial guesses of the model parameters,
5755
within the bounds specified by arguments `lower` and `upper` (if they are
5856
specified).
59-
y : NDArray[np.float64]
57+
y : F64Array
6058
Array of shape `(n,)` or `(q, n)` containing the values of the response
6159
variable(s). When the model is explicit, the user must specify a value
6260
for each element of `y`. If some responses of some observations are
6361
actually missing, then the user can set the corresponding weight in
6462
argument `we` to zero in order to remove the effect of the missing
6563
observation from the analysis. When the model is implicit, `y` is not
6664
referenced.
67-
x : NDArray[np.float64]
65+
x : F64Array
6866
Array of shape `(n,)` or `(m, n)` containing the values of the explanatory
6967
variable(s).
70-
we : float | NDArray[np.float64] | None
68+
we : float | F64Array | None
7169
Scalar or array specifying how the errors on `y` are to be weighted.
7270
If `we` is a scalar, then it is used for all data points. If `we` is
7371
an array of shape `(n,)` and `q==1`, then `we[i]` represents the weight
@@ -81,7 +79,7 @@ def odr(f: Callable[[NDArray[np.float64], NDArray[np.float64]], NDArray[np.float
8179
`y[:, i]`. For a comprehensive description of the options, refer to page
8280
25 of the ODRPACK95 guide. By default, `we` is set to one for all `y`
8381
data points.
84-
wd : float | NDArray[np.float64] | None
82+
wd : float | F64Array | None
8583
Scalar or array specifying how the errors on `x` are to be weighted.
8684
If `wd` is a scalar, then it is used for all data points. If `wd` is
8785
an array of shape `(n,)` and `m==1`, then `wd[i]` represents the weight
@@ -95,41 +93,41 @@ def odr(f: Callable[[NDArray[np.float64], NDArray[np.float64]], NDArray[np.float
9593
`x[:, i]`. For a comprehensive description of the options, refer to page
9694
26 of the ODRPACK95 guide. By default, `wd` is set to one for all `x`
9795
data points.
98-
fjacb : Callable[[NDArray[np.float64], NDArray[np.float64]], NDArray[np.float64]] | None
96+
fjacb : Callable[[F64Array, F64Array], F64Array] | None
9997
Jacobian of the function to be fitted with respect to `beta`, with the
10098
signature `fjacb(beta, x)`. It must return an array with shape
10199
`(n, npar, q)` or compatible. To activate this option, `job` must be
102100
set accordingly. By default, the Jacobian is evaluated numerically
103101
according to the finite difference scheme defined in `job`.
104-
fjacd : Callable[[NDArray[np.float64], NDArray[np.float64]], NDArray[np.float64]] | None
102+
fjacd : Callable[[F64Array, F64Array], F64Array] | None
105103
Jacobian of the function to be fitted with respect to `delta`, with the
106104
signature `fjacd(beta, x)`. It must return an array with shape
107105
`(n, m, q)` or compatible. To activate this option, `job` must be
108106
set accordingly. By default, the Jacobian is evaluated numerically
109107
according to the finite difference scheme defined in `job`.
110-
ifixb : NDArray[np.int32] | None
108+
ifixb : I32Array | None
111109
Array with the same shape as `beta0`, containing the values designating
112110
which elements of `beta` are to be held fixed. Zero means the parameter
113111
is held fixed, and one means it is adjustable. By default, `ifixb` is
114112
set to one for all elements of `beta`.
115-
ifixx : NDArray[np.int32] | None
113+
ifixx : I32Array | None
116114
Array with the same shape as `x`, containing the values designating
117115
which elements of `x` are to be held fixed. Alternatively, it can be a
118116
rank-1 array of shape `(m,)` or `(n,)`, in which case it will be broadcast
119117
along the other axis. Zero means the element is held fixed and one means
120118
it is free. By default, in orthogonal distance regression mode, `ifixx`
121119
is set to one for all elements of `x`. In ordinary least squares mode,
122120
the `x` values are intrinsically fixed.
123-
delta0 : NDArray[np.float64] | None
121+
delta0 : F64Array | None
124122
Array with the same shape as `x`, containing the initial guesses of the
125123
errors in the explanatory variable. To activate this option, `job` must
126124
be set accordingly. By default, `delta0` is set to zero for all elements
127125
of `x`.
128-
lower : NDArray[np.float64] | None
126+
lower : F64Array | None
129127
Array with the same shape as `beta0`, containing the lower bounds of the
130128
model parameters. By default, `lower` is set to negative infinity for
131129
all elements of `beta`.
132-
upper : NDArray[np.float64] | None
130+
upper : F64Array | None
133131
Array with the same shape as `beta0`, containing the upper bounds of the
134132
model parameters. By default, `upper` is set to positive infinity for
135133
all elements of `beta`.
@@ -182,29 +180,29 @@ def odr(f: Callable[[NDArray[np.float64], NDArray[np.float64]], NDArray[np.float
182180
maxit : int | None
183181
Maximum number of allowed iterations. The default value is 50 for a
184182
first run and 10 for a restart (see `job`).
185-
stpb : NDArray[np.float64] | None
183+
stpb : F64Array | None
186184
Array with the same shape as `beta0` containing the _relative_ step
187185
sizes used to compute the finite difference derivatives with respect
188186
to the model parameters. By default, `stpb` is set internally based on
189187
the value of `ndigit` and the type of finite differences used. For
190188
additional details, refer to pages 31 and 78 of the ODRPACK95 guide.
191-
stpd : NDArray[np.float64] | None
189+
stpd : F64Array | None
192190
Array with the same shape as `x`, containing the _relative_ step sizes
193191
used to compute the finite difference derivatives with respect to the
194192
errors in the explanatory variable. Alternatively, it can be a rank-1
195193
array of shape `(m,)` or `(n,)`, in which case it will be broadcast along
196194
the other axis. By default, `stpd` is set internally based on the value
197195
of `ndigit` and the type of finite differences used. For additional
198196
details, refer to pages 31 and 78 of the ODRPACK95 guide.
199-
sclb : NDArray[np.float64] | None
197+
sclb : F64Array | None
200198
Array with the same shape as `beta0` containing the scale values of the
201199
model parameters. Scaling is used to improve the numerical stability
202200
of the regression, but does not affect the problem specification. Scaling
203201
should not be confused with the weighting matrices `we` and `wd`. By
204202
default, `sclb` is set internally based on the relative magnitudes of
205203
`beta`. For further details, refer to pages 32 and 84 of the ODRPACK95
206204
guide.
207-
scld : NDArray[np.float64] | None
205+
scld : F64Array | None
208206
Array with the same shape as `x`, containing the scale values of the
209207
errors in the explanatory variable. Alternatively, it can be a rank-1
210208
array of shape `(m,)` or `(n,)`, in which case it will be broadcast along
@@ -213,11 +211,11 @@ def odr(f: Callable[[NDArray[np.float64], NDArray[np.float64]], NDArray[np.float
213211
should not be confused with the weighting matrices `we` and `wd`. By
214212
default, `scld` is set internally based on the relative magnitudes of
215213
`x`. For further details, refer to pages 32 and 85 of the ODRPACK95 guide.
216-
rwork : NDArray[np.float64] | None
214+
rwork : F64Array | None
217215
Array containing the real-valued internal state of the odrpack solver.
218216
It is only required for a restart (see `job`), in which case it must be
219217
set to the state of the previous run.
220-
iwork : NDArray[np.int32] | None
218+
iwork : I32Array | None
221219
Array containing the integer-valued internal state of the odrpack solver.
222220
It is only required for a restart (see `job`), in which case it must be
223221
set to the state of the previous run.

0 commit comments

Comments
 (0)