Adaptive interpolation (adaptive_interp)#

Holds recursive interpolation class. This includes the recursive training algorithm and consistency checks.

See Adaptive interpolation for example usage.

Functions:

window(seq[, n])

Returns a sliding window (of width n) over data from seq.

relative_fluctuations(da, dim)

Calculate relative mean and relative error of DataArray along dimension.

test_relative_fluctuations(alphas, model, states)

Test relative fluctuations of model.

train_iterative(alphas, factory_state, ...)

Add states to satisfy some tolerance.

train_recursive(alphas, factory_state, ...)

Add states to satisfy some tolerance.

check_polynomial_consistency(states, ...[, ...])

Check polynomial consistency across subsegments.

factory_state_idealgas(beta, order[, nrep, ...])

Example factory function to create single state.

callback_plot_progress(model, alphas, info_dict)

The callback function is called each iteration after model is created.

plot_polynomial_consistency(alphas, states, ...)

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 of object, 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, default 10) – number of iterations

  • states_avail (list, optional) – Not implemented yet

  • state_kws (dict, optional) – extra arguments to factory_state

  • statecollection_kws (dict, optional) – extra arguments to factory_statecollection

  • predict_kws (dict, optional) – extra arguments to model.predict(alphas, **predict_kws)

  • tol (float, default 0.003) – relative tolerance. If max err_rel < tol then not new state added

  • alpha_tol (float, default 0.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 interaction

  • callback_kws (dict, optional) – extra arguments to callback

Returns:

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 of object, 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, default 10) – number of iterations

  • states_avail (list, optional) – Not implemented yet

  • state_kws (dict, optional) – extra arguments to factory_state

  • statecollection_kws (dict, optional) – extra arguments to factory_statecollection

  • predict_kws (dict, optional) – extra arguments to model.predict(alphas, **predict_kws)

  • tol (float, default 0.003) – relative tolerance. If max err_rel < tol then not new state added

  • alpha_tol (float, default 0.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 interaction

  • callback_kws (dict, optional) – extra arguments to callback

  • depth (int) – Internal variable used during recursion.

  • info (list) – Internal variable used during recursion.

Returns:

  • states (list of object) – list of states

  • info (list of dict) – Information from each iteration

thermoextrap.adaptive_interp.check_polynomial_consistency(states, factory_statecollection, reduce_dim='rep')[source]#

Check polynomial consistency across subsegments.

Parameters:
  • states (sequence of object) – sequence of states

  • factory_statecollection (callable()) – model = factory_statecollection(states, **statecollection_kws)

  • reduce_dim (str, default "rep") – dimension to reduce along

  • order (int, optional) – order passed to model.predict

  • statecollection_kws (dict, optional) – extra arguments to factory_statecollection

Returns:

  • p_values (dict) – p value for pairs of models. Keys will be of the form ((alpha0, alpha1), (alpha2, alpha3))

  • models (dict) – collection of models created. Keys are of the form (alpha0, alpha1)

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, default True) – 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:
  • verbose (bool, default True)

  • maxdepth_stop (int, optional) – Note that this is redundant with maxdepth, but for demonstration purposes

  • ax (matplotlib.axes.Axes, optional)

thermoextrap.adaptive_interp.plot_polynomial_consistency(alphas, states, factory_statecollection)[source]#

Plotter for polynomial consistency.