pynumerics 2.3.0__py3-none-any.whl
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.
- pynumerics/__init__.py +73 -0
- pynumerics/_bin/Microsoft.CodeAnalysis.CSharp.dll +0 -0
- pynumerics/_bin/Microsoft.CodeAnalysis.LICENSE.txt +27 -0
- pynumerics/_bin/Microsoft.CodeAnalysis.dll +0 -0
- pynumerics/_bin/Numerics.dll +0 -0
- pynumerics/_bin/pynumerics.runtimeconfig.json +15 -0
- pynumerics/_coverage.py +3825 -0
- pynumerics/_util.py +237 -0
- pynumerics/_version.py +40 -0
- pynumerics/bootstrap.py +593 -0
- pynumerics/callbacks.py +791 -0
- pynumerics/copulas.py +910 -0
- pynumerics/data.py +2233 -0
- pynumerics/differentiation.py +348 -0
- pynumerics/distributions.py +4043 -0
- pynumerics/download.py +623 -0
- pynumerics/enums.py +119 -0
- pynumerics/errors.py +245 -0
- pynumerics/functions.py +1142 -0
- pynumerics/goodness_of_fit.py +699 -0
- pynumerics/graphs.py +556 -0
- pynumerics/hypothesis_tests.py +445 -0
- pynumerics/identity.py +144 -0
- pynumerics/integration.py +677 -0
- pynumerics/linalg.py +1408 -0
- pynumerics/machine_learning.py +1335 -0
- pynumerics/marshal.py +733 -0
- pynumerics/mathematics.py +1053 -0
- pynumerics/mcmc.py +1406 -0
- pynumerics/multivariate.py +1539 -0
- pynumerics/ode.py +156 -0
- pynumerics/optimization.py +1736 -0
- pynumerics/probability.py +966 -0
- pynumerics/py.typed +0 -0
- pynumerics/regional.py +522 -0
- pynumerics/roots.py +274 -0
- pynumerics/runtime.py +384 -0
- pynumerics/sampling.py +1758 -0
- pynumerics/special.py +1079 -0
- pynumerics/statistics.py +1958 -0
- pynumerics/timeseries.py +1326 -0
- pynumerics-2.3.0.dist-info/METADATA +142 -0
- pynumerics-2.3.0.dist-info/RECORD +46 -0
- pynumerics-2.3.0.dist-info/WHEEL +5 -0
- pynumerics-2.3.0.dist-info/licenses/LICENSE +10 -0
- pynumerics-2.3.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
"""Finite-difference derivatives from ``Numerics.Mathematics.NumericalDerivative``.
|
|
2
|
+
|
|
3
|
+
Functions are Python callables, C# source or .NET delegates (see :mod:`pynumerics.callbacks`).
|
|
4
|
+
A ``step_size`` of None lets Numerics choose the step (:func:`calculate_step_size`); the other
|
|
5
|
+
defaults are the Numerics defaults.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
import numpy as np
|
|
13
|
+
|
|
14
|
+
from pynumerics import callbacks
|
|
15
|
+
from pynumerics._util import namespace
|
|
16
|
+
from pynumerics.errors import translate_public
|
|
17
|
+
from pynumerics.marshal import _optional_doubles, to_net_doubles, to_numpy_doubles, to_numpy_matrix
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _derivative() -> Any:
|
|
21
|
+
"""Return ``Numerics.Mathematics.NumericalDerivative`` after starting the runtime."""
|
|
22
|
+
return namespace("Numerics.Mathematics").NumericalDerivative
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _step(step_size: float | None) -> float:
|
|
26
|
+
"""Return the Numerics step argument: -1 (choose the step) for None."""
|
|
27
|
+
return -1.0 if step_size is None else float(step_size)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _scalar(name: str, f: Any, point: float, step_size: float | None) -> float:
|
|
31
|
+
"""Call the single-variable member ``name`` of ``NumericalDerivative``."""
|
|
32
|
+
return float(getattr(_derivative(), name)(callbacks.scalar_function(f), float(point), _step(step_size)))
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def forward_difference(f: Any, point: float, step_size: float | None = None) -> float:
|
|
36
|
+
"""Return f'(point) by the forward difference [f(x + h) − f(x)] / h (``ForwardDifference``).
|
|
37
|
+
|
|
38
|
+
Parameters
|
|
39
|
+
----------
|
|
40
|
+
f : callable, str or System.Delegate
|
|
41
|
+
Function of one float.
|
|
42
|
+
point : float
|
|
43
|
+
Where to differentiate.
|
|
44
|
+
step_size : float, optional
|
|
45
|
+
The step h; None lets Numerics choose it.
|
|
46
|
+
|
|
47
|
+
Returns
|
|
48
|
+
-------
|
|
49
|
+
float
|
|
50
|
+
The derivative estimate.
|
|
51
|
+
"""
|
|
52
|
+
return _scalar("ForwardDifference", f, point, step_size)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def backward_difference(f: Any, point: float, step_size: float | None = None) -> float:
|
|
56
|
+
"""Return f'(point) by the backward difference [f(x) − f(x − h)] / h (``BackwardDifference``).
|
|
57
|
+
|
|
58
|
+
Parameters
|
|
59
|
+
----------
|
|
60
|
+
f : callable, str or System.Delegate
|
|
61
|
+
Function of one float.
|
|
62
|
+
point : float
|
|
63
|
+
Where to differentiate.
|
|
64
|
+
step_size : float, optional
|
|
65
|
+
The step h; None lets Numerics choose it.
|
|
66
|
+
|
|
67
|
+
Returns
|
|
68
|
+
-------
|
|
69
|
+
float
|
|
70
|
+
The derivative estimate.
|
|
71
|
+
"""
|
|
72
|
+
return _scalar("BackwardDifference", f, point, step_size)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def central_difference(f: Any, point: float, step_size: float | None = None) -> float:
|
|
76
|
+
"""Return f'(point) by the central difference [f(x + h) − f(x − h)] / 2h (``CentralDifference``).
|
|
77
|
+
|
|
78
|
+
Parameters
|
|
79
|
+
----------
|
|
80
|
+
f : callable, str or System.Delegate
|
|
81
|
+
Function of one float.
|
|
82
|
+
point : float
|
|
83
|
+
Where to differentiate.
|
|
84
|
+
step_size : float, optional
|
|
85
|
+
The step h; None lets Numerics choose it.
|
|
86
|
+
|
|
87
|
+
Returns
|
|
88
|
+
-------
|
|
89
|
+
float
|
|
90
|
+
The derivative estimate.
|
|
91
|
+
"""
|
|
92
|
+
return _scalar("CentralDifference", f, point, step_size)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def derivative(f: Any, point: float, step_size: float | None = None) -> float:
|
|
96
|
+
"""Return f'(point) by the Numerics default rule, the central difference (``Derivative``).
|
|
97
|
+
|
|
98
|
+
Parameters
|
|
99
|
+
----------
|
|
100
|
+
f : callable, str or System.Delegate
|
|
101
|
+
Function of one float.
|
|
102
|
+
point : float
|
|
103
|
+
Where to differentiate.
|
|
104
|
+
step_size : float, optional
|
|
105
|
+
The step h; None lets Numerics choose it.
|
|
106
|
+
|
|
107
|
+
Returns
|
|
108
|
+
-------
|
|
109
|
+
float
|
|
110
|
+
The derivative estimate.
|
|
111
|
+
"""
|
|
112
|
+
return _scalar("Derivative", f, point, step_size)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def ridders(f: Any, point: float, step_size: float | None = None) -> tuple[float, float]:
|
|
116
|
+
"""Return f'(point) and its error estimate by Ridders' extrapolation (``RiddersMethod``).
|
|
117
|
+
|
|
118
|
+
Parameters
|
|
119
|
+
----------
|
|
120
|
+
f : callable, str or System.Delegate
|
|
121
|
+
Function of one float.
|
|
122
|
+
point : float
|
|
123
|
+
Where to differentiate.
|
|
124
|
+
step_size : float, optional
|
|
125
|
+
The initial step; None uses the large step 0.1 * (1 + abs(point)) that Numerical Recipes
|
|
126
|
+
recommends, which the extrapolation refines to about 1e-15 relative for a smooth function.
|
|
127
|
+
Pass a smaller step when ``f`` is not smooth over that scale (near a singularity or the
|
|
128
|
+
edge of its domain). With the default step, a tableau that meets a NaN or infinite value
|
|
129
|
+
(a logarithm at 0.05 that returns NaN for a negative argument, say) is rebuilt from half
|
|
130
|
+
the step, up to 30 times, until it is finite.
|
|
131
|
+
|
|
132
|
+
Returns
|
|
133
|
+
-------
|
|
134
|
+
tuple of float
|
|
135
|
+
``(derivative, error)``.
|
|
136
|
+
"""
|
|
137
|
+
value, error = _derivative().RiddersMethod(callbacks.scalar_function(f), float(point), 0.0, _step(step_size))
|
|
138
|
+
return float(value), float(error)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def second_derivative(f: Any, point: float, step_size: float | None = None) -> float:
|
|
142
|
+
"""Return f''(point) by the central second difference (``SecondDerivative``).
|
|
143
|
+
|
|
144
|
+
Parameters
|
|
145
|
+
----------
|
|
146
|
+
f : callable, str or System.Delegate
|
|
147
|
+
Function of one float.
|
|
148
|
+
point : float
|
|
149
|
+
Where to differentiate.
|
|
150
|
+
step_size : float, optional
|
|
151
|
+
The step h; None lets Numerics choose it.
|
|
152
|
+
|
|
153
|
+
Returns
|
|
154
|
+
-------
|
|
155
|
+
float
|
|
156
|
+
The second-derivative estimate.
|
|
157
|
+
"""
|
|
158
|
+
return _scalar("SecondDerivative", f, point, step_size)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def second_derivative_forward(f: Any, point: float, step_size: float | None = None) -> float:
|
|
162
|
+
"""Return f''(point) by the forward second difference (``SecondDerivativeForward``).
|
|
163
|
+
|
|
164
|
+
Parameters
|
|
165
|
+
----------
|
|
166
|
+
f : callable, str or System.Delegate
|
|
167
|
+
Function of one float.
|
|
168
|
+
point : float
|
|
169
|
+
Where to differentiate.
|
|
170
|
+
step_size : float, optional
|
|
171
|
+
The step h; None lets Numerics choose it.
|
|
172
|
+
|
|
173
|
+
Returns
|
|
174
|
+
-------
|
|
175
|
+
float
|
|
176
|
+
The second-derivative estimate.
|
|
177
|
+
"""
|
|
178
|
+
return _scalar("SecondDerivativeForward", f, point, step_size)
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def second_derivative_backward(f: Any, point: float, step_size: float | None = None) -> float:
|
|
182
|
+
"""Return f''(point) by the backward second difference (``SecondDerivativeBackward``).
|
|
183
|
+
|
|
184
|
+
Parameters
|
|
185
|
+
----------
|
|
186
|
+
f : callable, str or System.Delegate
|
|
187
|
+
Function of one float.
|
|
188
|
+
point : float
|
|
189
|
+
Where to differentiate.
|
|
190
|
+
step_size : float, optional
|
|
191
|
+
The step h; None lets Numerics choose it.
|
|
192
|
+
|
|
193
|
+
Returns
|
|
194
|
+
-------
|
|
195
|
+
float
|
|
196
|
+
The second-derivative estimate.
|
|
197
|
+
"""
|
|
198
|
+
return _scalar("SecondDerivativeBackward", f, point, step_size)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def jacobian_at(f: Any, x: Any, point: Any, step_size: float | None = None) -> np.ndarray:
|
|
202
|
+
"""Return J[i, j] = ∂f(x_i, θ)/∂θ_j at θ = point by central differences (``Jacobian(f, x, point)``).
|
|
203
|
+
|
|
204
|
+
Parameters
|
|
205
|
+
----------
|
|
206
|
+
f : callable, str or System.Delegate
|
|
207
|
+
Function ``f(x_i, theta)`` of a float and a float64 parameter array.
|
|
208
|
+
x : array_like
|
|
209
|
+
The fixed locations, one Jacobian row each.
|
|
210
|
+
point : array_like
|
|
211
|
+
The parameters θ, one Jacobian column each.
|
|
212
|
+
step_size : float, optional
|
|
213
|
+
The step h; None lets Numerics choose one per parameter.
|
|
214
|
+
|
|
215
|
+
Returns
|
|
216
|
+
-------
|
|
217
|
+
numpy.ndarray
|
|
218
|
+
The ``len(x)`` by ``len(point)`` Jacobian.
|
|
219
|
+
"""
|
|
220
|
+
return to_numpy_matrix(_derivative().Jacobian(callbacks.to_delegate(f, "Func<double,double[],double>"),
|
|
221
|
+
to_net_doubles(x), to_net_doubles(point), _step(step_size)))
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def jacobian(g: Any, theta: Any, lower_bounds: Any = None, upper_bounds: Any = None, rel_step: float = 1e-5,
|
|
225
|
+
abs_step: float = 1e-7, max_backtrack: int = 5) -> np.ndarray:
|
|
226
|
+
"""Return the Jacobian of a vector function with bound-aware adaptive steps (``Jacobian(g, theta, ...)``).
|
|
227
|
+
|
|
228
|
+
Central differences are used where both sides fit inside the bounds and one-sided differences
|
|
229
|
+
near a bound; the step shrinks when an evaluation is not finite.
|
|
230
|
+
|
|
231
|
+
Parameters
|
|
232
|
+
----------
|
|
233
|
+
g : callable, str or System.Delegate
|
|
234
|
+
Function of a float64 array returning a float64 array (``Func<double[], double[]>``).
|
|
235
|
+
theta : array_like
|
|
236
|
+
Where to differentiate.
|
|
237
|
+
lower_bounds, upper_bounds : array_like, optional
|
|
238
|
+
Parameter bounds; None for unbounded.
|
|
239
|
+
rel_step : float, optional
|
|
240
|
+
Relative step; default 1e-5.
|
|
241
|
+
abs_step : float, optional
|
|
242
|
+
Minimum absolute step; default 1e-7.
|
|
243
|
+
max_backtrack : int, optional
|
|
244
|
+
Step reductions allowed after a failed evaluation; default 5.
|
|
245
|
+
|
|
246
|
+
Returns
|
|
247
|
+
-------
|
|
248
|
+
numpy.ndarray
|
|
249
|
+
The ``len(g(theta))`` by ``len(theta)`` Jacobian.
|
|
250
|
+
"""
|
|
251
|
+
return to_numpy_matrix(_derivative().Jacobian(
|
|
252
|
+
callbacks.to_delegate(g, "Func<double[],double[]>"), to_net_doubles(theta), _optional_doubles(lower_bounds),
|
|
253
|
+
_optional_doubles(upper_bounds), float(rel_step), float(abs_step), int(max_backtrack)))
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def gradient(f: Any, theta: Any, lower_bounds: Any = None, upper_bounds: Any = None, rel_step: float = 1e-5,
|
|
257
|
+
abs_step: float = 1e-7, max_backtrack: int = 5) -> np.ndarray:
|
|
258
|
+
"""Return the gradient of a scalar function with bound-aware adaptive steps (``Gradient``).
|
|
259
|
+
|
|
260
|
+
Parameters
|
|
261
|
+
----------
|
|
262
|
+
f : callable, str or System.Delegate
|
|
263
|
+
Function of a float64 array returning a float.
|
|
264
|
+
theta : array_like
|
|
265
|
+
Where to differentiate.
|
|
266
|
+
lower_bounds, upper_bounds : array_like, optional
|
|
267
|
+
Parameter bounds; None for unbounded.
|
|
268
|
+
rel_step : float, optional
|
|
269
|
+
Relative step; default 1e-5.
|
|
270
|
+
abs_step : float, optional
|
|
271
|
+
Minimum absolute step; default 1e-7.
|
|
272
|
+
max_backtrack : int, optional
|
|
273
|
+
Step reductions allowed after a failed evaluation; default 5.
|
|
274
|
+
|
|
275
|
+
Returns
|
|
276
|
+
-------
|
|
277
|
+
numpy.ndarray
|
|
278
|
+
The gradient.
|
|
279
|
+
|
|
280
|
+
Raises
|
|
281
|
+
------
|
|
282
|
+
ArithmeticError
|
|
283
|
+
If f(theta) is not finite (raised by Numerics).
|
|
284
|
+
"""
|
|
285
|
+
return to_numpy_doubles(_derivative().Gradient(
|
|
286
|
+
callbacks.vector_function(f), to_net_doubles(theta), _optional_doubles(lower_bounds),
|
|
287
|
+
_optional_doubles(upper_bounds), float(rel_step), float(abs_step), int(max_backtrack)))
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def hessian(f: Any, theta: Any, lower_bounds: Any = None, upper_bounds: Any = None, rel_step: float = 1e-4,
|
|
291
|
+
abs_step: float = 1e-6, max_backtrack: int = 6) -> np.ndarray:
|
|
292
|
+
"""Return the symmetric Hessian of a scalar function with bound-aware adaptive steps (``Hessian``).
|
|
293
|
+
|
|
294
|
+
Parameters
|
|
295
|
+
----------
|
|
296
|
+
f : callable, str or System.Delegate
|
|
297
|
+
Function of a float64 array returning a float.
|
|
298
|
+
theta : array_like
|
|
299
|
+
Where to differentiate.
|
|
300
|
+
lower_bounds, upper_bounds : array_like, optional
|
|
301
|
+
Parameter bounds; None for unbounded.
|
|
302
|
+
rel_step : float, optional
|
|
303
|
+
Relative step; default 1e-4.
|
|
304
|
+
abs_step : float, optional
|
|
305
|
+
Minimum absolute step; default 1e-6.
|
|
306
|
+
max_backtrack : int, optional
|
|
307
|
+
Step reductions allowed after a failed evaluation; default 6.
|
|
308
|
+
|
|
309
|
+
Returns
|
|
310
|
+
-------
|
|
311
|
+
numpy.ndarray
|
|
312
|
+
The square Hessian.
|
|
313
|
+
|
|
314
|
+
Raises
|
|
315
|
+
------
|
|
316
|
+
ArithmeticError
|
|
317
|
+
If f(theta) is not finite (raised by Numerics).
|
|
318
|
+
"""
|
|
319
|
+
return to_numpy_matrix(_derivative().Hessian(
|
|
320
|
+
callbacks.vector_function(f), to_net_doubles(theta), _optional_doubles(lower_bounds),
|
|
321
|
+
_optional_doubles(upper_bounds), float(rel_step), float(abs_step), int(max_backtrack)))
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def calculate_step_size(x: float, order: int = 1) -> float:
|
|
325
|
+
"""Return the finite-difference step ε^(1/(1 + order))·(1 + abs(x)) Numerics uses (``CalculateStepSize``).
|
|
326
|
+
|
|
327
|
+
Parameters
|
|
328
|
+
----------
|
|
329
|
+
x : float
|
|
330
|
+
The point.
|
|
331
|
+
order : int, optional
|
|
332
|
+
The derivative order; default 1.
|
|
333
|
+
|
|
334
|
+
Returns
|
|
335
|
+
-------
|
|
336
|
+
float
|
|
337
|
+
The step.
|
|
338
|
+
"""
|
|
339
|
+
return float(_derivative().CalculateStepSize(float(x), int(order)))
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
__all__ = [
|
|
343
|
+
"backward_difference", "calculate_step_size", "central_difference", "derivative", "forward_difference",
|
|
344
|
+
"gradient", "hessian", "jacobian", "jacobian_at", "ridders", "second_derivative", "second_derivative_backward",
|
|
345
|
+
"second_derivative_forward",
|
|
346
|
+
]
|
|
347
|
+
|
|
348
|
+
translate_public(globals())
|