Adaptive interpolation (adaptive_interp
)#
Holds recursive interpolation class. This includes the recursive training algorithm and consistency checks.
See Adaptive interpolation for example usage.
Functions:
|
Returns a sliding window (of width n) over data from seq. |
|
Calculate relative mean and relative error of DataArray along dimension. |
|
Test relative fluctuations of model. |
|
Add states to satisfy some tolerance. |
|
Add states to satisfy some tolerance. |
|
Check polynomial consistency across subsegments. |
|
Example factory function to create single state. |
|
The callback function is called each iteration after model is created. |
|
Plotter for polynomial consistency. |
- thermoextrap.adaptive_interp.window(seq, n=2)[source]#
Returns a sliding window (of width n) over data from seq.
s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ...
- thermoextrap.adaptive_interp.relative_fluctuations(da, dim)[source]#
Calculate relative mean and relative error of DataArray along dimension.
- thermoextrap.adaptive_interp.test_relative_fluctuations(alphas, model, states, reduce_dim='rep', predict_kws=None, tol=0.003, alpha_tol=0.01)[source]#
Test relative fluctuations of model.
- thermoextrap.adaptive_interp.train_iterative(alphas, factory_state, factory_statecollection, states=None, reduce_dim='rep', maxiter=10, state_kws=None, statecollection_kws=None, predict_kws=None, tol=0.003, alpha_tol=0.01, callback=None, callback_kws=None)[source]#
Add states to satisfy some tolerance.
Each iteration calculates the relative error, then adds a state where error is largest.
NOTE: The big difference between this and the recursive interpolation is that a single set of alphas is passed, and always considered. That is, each iteration considers the whole range of alphas. If this is undesirable, we go back to a recursive?
- Parameters:
alphas (array-like) – values of alpha to calculate along
factory_state (
callable()
) – state creation factory function. state = factory_state(alpha, **state_kws). This state must have a dimension reduce_dim.factory_statecollection (
callable()
) – state collection factory. model = factory_statecollection(states)states (
list
ofobject
, optional) – initial states list. If not passed, first guess at states is [factory_state(alphas[0]), factory_state(alphas[-1])]reduce_dim (
str
) – dimension to calculate statistics along.maxiter (
int
, default10
) – number of iterationsstates_avail (
list
, optional) – Not implemented yetstate_kws (
dict
, optional) – extra arguments to factory_statestatecollection_kws (
dict
, optional) – extra arguments to factory_statecollectionpredict_kws (
dict
, optional) – extra arguments to model.predict(alphas, **predict_kws)tol (
float
, default0.003
) – relative tolerance. If max err_rel < tol then not new state addedalpha_tol (
float
, default0.01
) – new states must have abs(alpha_new - alpha) > alpha_tol for all existing states.callback (
callable()
) – stop = callback(model, alphas, info_dict, **callback_kws). If callback returns something that evaluates True, then the iteration stops. * model : current model. * alphas : sequence of alphas * info_dict : dictionary containing current estimate information * info_dict[‘alpha0’] : alpha0 values in the model * info_dict[‘err’] : relative error in the model * info_dict[‘ave’] : average estimate of the model * info_dict[‘depth’] : depth of interactioncallback_kws (
dict
, optional) – extra arguments to callback
- Returns:
model (
thermoextrap.models.StateCollection
instance) – final output of factory_statecollection
- thermoextrap.adaptive_interp.train_recursive(alphas, factory_state, factory_statecollection, state0=None, state1=None, states=None, info=None, reduce_dim='rep', depth=0, maxiter=10, state_kws=None, statecollection_kws=None, predict_kws=None, tol=0.003, alpha_tol=0.01, callback=None, callback_kws=None)[source]#
Add states to satisfy some tolerance.
Each iteration calculates the relative error, then adds a state where error is largest.
NOTE: The big difference between this and the recursive interpolation is that a single set of alphas is passed, and always considered. That is, each iteration considers the whole range of alphas. If this is undesirable, we go back to a recursive?
- Parameters:
alphas (array-like) – values of alpha to calculate along
factory_state (
callable()
) – state creation factory function. state = factory_state(alpha, **state_kws). This state must have a dimension reduce_dim.factory_statecollection (
callable()
) – state collection factory. model = factory_statecollection(states)state0, state1 (
object
) – states to be used for building model. defaults to building states at alphas[0] and alphas[-1]states (
list
ofobject
, optional) – initial states list. If not passed, first guess at states is [factory_state(alphas[0]), factory_state(alphas[-1])]reduce_dim (
str
) – dimension to calculate statistics along.maxiter (
int
, default10
) – number of iterationsstates_avail (
list
, optional) – Not implemented yetstate_kws (
dict
, optional) – extra arguments to factory_statestatecollection_kws (
dict
, optional) – extra arguments to factory_statecollectionpredict_kws (
dict
, optional) – extra arguments to model.predict(alphas, **predict_kws)tol (
float
, default0.003
) – relative tolerance. If max err_rel < tol then not new state addedalpha_tol (
float
, default0.01
) – new states must have abs(alpha_new - alpha) > alpha_tol for all existing states.callback (
callable()
) – stop = callback(model, alphas, info_dict, **callback_kws). If callback returns something that evaluates True, then the iteration stops. model is the current model. alphas is the sequence of alphas info_dict dictionary containing info_dict[‘alpha0’] the alpha0 values in the model info_dict[‘err’] the normalized error in the model info_dict[‘depth’] the depth of interactioncallback_kws (
dict
, optional) – extra arguments to callbackdepth (
int
) – Internal variable used during recursion.info (
list
) – Internal variable used during recursion.
- Returns:
- thermoextrap.adaptive_interp.check_polynomial_consistency(states, factory_statecollection, reduce_dim='rep')[source]#
Check polynomial consistency across subsegments.
- Parameters:
factory_statecollection (
callable()
) – model = factory_statecollection(states, **statecollection_kws)reduce_dim (
str
, default"rep"
) – dimension to reduce alongorder (
int
, optional) – order passed to model.predictstatecollection_kws (
dict
, optional) – extra arguments to factory_statecollection
- Returns:
- thermoextrap.adaptive_interp.factory_state_idealgas(beta, order, nrep=100, rep_dim='rep', nconfig=10000, npart=1000, rng=None)[source]#
Example factory function to create single state.
This particular state function returns the a beta extrapolation model for the position of an ideal gas particle in an external field.
This can be as complicated as you want. It just needs to return a state at the given value of alpha. It also should have a dimension with the same name as reduce_dim below Here, that dimension is ‘rep’, the resampled/replicated dimension
Extra arguments can be passed via the state_kws dictionary
- Parameters:
seed_from_beta (
bool
, defaultTrue
) – If True, then set rng seed based on beta value. For testing purposes. Only applies if rng = None.rng (
numpy.random.Generator
, optional)
- thermoextrap.adaptive_interp.callback_plot_progress(model, alphas, info_dict, verbose=True, maxdepth_stop=None, ax=None)[source]#
The callback function is called each iteration after model is created.
Optionally, it can return value True to stop iteration
- Parameters:
maxdepth_stop (
int
, optional) – Note that this is redundant with maxdepth, but for demonstration purposesax (
matplotlib.axes.Axes
, optional)