pypcga.PCGA#
- class pypcga.PCGA(*, s_init: ~numpy.ndarray[tuple[~typing.Any, ...], ~numpy.dtype[~numpy.float64]], obs: ~numpy.ndarray[tuple[~typing.Any, ...], ~numpy.dtype[~numpy.float64]], cov_obs: ~covmats._covariances.CovarianceMatrix, forward_model: ~typing.Callable, Q: ~covmats._covariances.CovViaEigenFactorization, drift: ~covmats._priors.DriftMatrix | None = None, prior_s_var: float | ~numpy.ndarray[tuple[~typing.Any, ...], ~numpy.dtype[~numpy.float64]] | None = None, callback: ~typing.Callable | None = None, is_line_search: bool = False, is_lm: bool = False, is_direct_solve: bool = False, random_state: int | ~numpy.random._generator.Generator | ~numpy.random.mtrand.RandomState | None = Generator(PCG64) at 0x75F28C1A7060, is_objfun_exact: bool = False, max_it_lm: int = 2, alphamax_lm: float = 1000.0, lm_smin: float | None = None, lm_smax: float | None = None, max_it_ls: int = 20, max_workers_lm: int = 2, maxiter: int = 10, ftol: float = 1e-05, ftarget: float | None = None, restol: float = 0.01, logger: ~logging.Logger | None = None, is_save_jac: bool = True, eps: float = 1e-08)[source]#
Solve inverse problem with PCGA (approx to quasi-linear method)
Every values are represented as 2D np.array.
- __init__(*, s_init: ~numpy.ndarray[tuple[~typing.Any, ...], ~numpy.dtype[~numpy.float64]], obs: ~numpy.ndarray[tuple[~typing.Any, ...], ~numpy.dtype[~numpy.float64]], cov_obs: ~covmats._covariances.CovarianceMatrix, forward_model: ~typing.Callable, Q: ~covmats._covariances.CovViaEigenFactorization, drift: ~covmats._priors.DriftMatrix | None = None, prior_s_var: float | ~numpy.ndarray[tuple[~typing.Any, ...], ~numpy.dtype[~numpy.float64]] | None = None, callback: ~typing.Callable | None = None, is_line_search: bool = False, is_lm: bool = False, is_direct_solve: bool = False, random_state: int | ~numpy.random._generator.Generator | ~numpy.random.mtrand.RandomState | None = Generator(PCG64) at 0x75F28C1A7060, is_objfun_exact: bool = False, max_it_lm: int = 2, alphamax_lm: float = 1000.0, lm_smin: float | None = None, lm_smax: float | None = None, max_it_ls: int = 20, max_workers_lm: int = 2, maxiter: int = 10, ftol: float = 1e-05, ftarget: float | None = None, restol: float = 0.01, logger: ~logging.Logger | None = None, is_save_jac: bool = True, eps: float = 1e-08) None[source]#
Initialize the instance.
- Parameters:
s_init (NDArrayFloat) – 1D array of initial control parameters, i.e., initial solution for Gauss-Newton method. In theory, the choice of s_init does not affect the estimation while total number of iterations/number of forward model runs depend on s_init. Expected shape is (\(N_{s}\)) or (\(N_{s}, 1\)).
obs (numpy.ndarray, optional) – 1D array of (noisy) measurements used for inversion.
cov_obs (covmats.CovarianceMatrix) – Covariance matrix of observed data measurement errors with dimensions (\(N_{\mathrm{obs}}\), \(N_{\mathrm{obs}}\)). Also denoted \(R\) in the literature. All covariance representations provided by
covmatsare supported.forward_model (Callable) – Wrapper for forward model obs = f(s). See a template python file in each example for more information. Return shape (\(N_{\mathrm{obs}}\), \(N_{e}\)).
Q (covmats.CovViaEigenFactorization) – Eigen factorization of the Covariance matrix of the inverted parameters with shape (\(N_{s}\), \(N_{s}\)).
drift (Optional[DriftMatrix], optional) – _description_, by default None
prior_s_var (Optional[Union[float, NDArrayFloat]], optional) – _description_, by default None
callback (Optional[Callable], optional) – _description_, by default None
is_line_search (bool, optional) – Whether to use line search (add ref) if the Gauss-Newton iteration fails to lower the cost function value. It comes at the cost of extra forward model runs (not parallelized). By default False.
is_lm (bool, optional) – Whether to use Levenberg Marquard inner iterations in each Gauss-Newton iterations. It consists in inflating the covariance matrix of observed data measurement errors (cov_obs), by adding weights on its diagonal. It acts as a regularization and can help to make the objective function more convex. This comes at the price of extra system inversions and max_it_lm forward model runs. The runs can be performed in parallel since it relies on forward_model. But it is the user’s responsibility to parallelize forward_model. By default False.
is_direct_solve (bool, optional) –
Whether to solve the saddle point system (Ax = b), see eq 4.53 in (add ref), with:
the direct approach (Cholesky factorization of A), see eq (4.57) in [Collet, 2024]
or to use the alternative iterative Krylov subspace approach as described in :cite`saibabaEfficientMethodsLargescale2012, saibabaFastAlgorithmsGeostatistical2013`.
Using direct solve is practical if the number of observations is around 100 or less. Beyond, it is advise to rely on the The default is False (using iterative Kryloc subspace by default).
random_state (Optional[ Union[int, np.random.Generator, np.random.RandomState]]) – Pseudorandom number generator state used to generate resamples. If random_state is
None(or np.random), the numpy.random.RandomState singleton is used. If random_state is an int, a newRandomStateinstance is used, seeded with random_state. If random_state is already aGeneratororRandomStateinstance then that instance is used.is_objfun_exact (bool, optional) – _description_, by default False
max_it_lm (int) – Maximum number iterations when using Levenberg Marquard regularization. Only applies if is_lm is True. By default use all available CPUs.
alphamax_lm (float, optional) – Maximum weight for LM. TODO: add the formula. By default 10.0**3.
lm_smax (Optional[float], optional) – Maximum LM solution, by default None
max_it_ls (int, optional) – Maximum number of iterations when using line search, by default 20.
maxiter (int, optional) – Maximum Gauss-Newton iterations, by default 10.
ftarget (Optional[Union[float, Callable]] = None, optional) – Target objective function (stop criterion) . The iteration stops when
f^{k+1} <= fmin. If None, the stop criterion is ignored. The default is None.ftol (float, optional) – Objective function minimum change (stop criterion). The iteration stops when
(f^k - f^{k+1})/max{|f^k|,|f^{k+1}|,1} <= ftol. Typical values for ftol on a computer with 15 digits of accuracy in double precision are as follows: ftol = 5e-3 for low accuracy; ftol = 5e-8 for moderate accuracy; ftol = 5e-14 for extremely high accuracy. If ftol = 0, the test will stop the algorithm only if the objective function remains unchanged after one iteration. The default is 1e-5.restol (float, optional) –
Mininmum change in the update value. Below this threshold, changes are considered unisgnificant and inversion is stopped. The change is computed as the euclidean norm of the difference between the current updated values and the values at the previous iteration scaled by the euclidean norm of the values at the previous iteration:
\[\mathrm{change} = \dfrac{\left\lVert \mathbf{s}_{\ell+1} - \mathbf{s}_{\ell} \right\rVert^{2}}{\left\lVert \mathbf{s}_{\ell} \right\rVert^{2}}\]by default 1e-2.
logger (Optional[Logger], optional) – Logger instance. If no logger is passed, there will be no output. By default None.
is_save_jac (bool, optional) – _description_, by default False
eps (float, optional) – PCGA perturbation scalar (see eq. in …), By default 1.0e-8.
Properties
Get the observation errors covariance matrix.
Return the number of forecast data.
Return the number of internal optimization loops.
Return the number of observations/forecast data.
Get the a priori variance of the control variables.
Return the length of the parameters vector.
Methods
Gauss-newton iteration
Inflation factors used in each internal loop.
Return the dense posterior covariance matrix..
Return the posterior covariance matrix through an Eigen factorization.
Return the low rank inverse of A as a linear operator.
Get the matrix HQH^{T} + lpha R.
Jacobian times Matrix (Vectors) in Parallel perturbation interval delta determined following Brown and Saad [1990]
Solve the geostatistical system using a direct solver.
0.5(y-h(s))^TR^{-1}(y-h(s)) + 0.5*(s-Xb)^TQ^{-1}(s-Xb)
simul_obs with shape (n_obs, ne)
marginalized objective w.r.t.
marginalized objective w.r.t.
s_cur with shape (N_s, Ne) beta_cur with shape (N_b, Ne) 0.5(s-Xb)^TC^{-1}(s-Xb)
Return the root mean square error.