API

DPFC

class DeterministicParticleFlowControl.DPFC(t1, t2, y1, y2, f, g, N, M, reweight=False, U=None, dens_est='nonparametric', reject=True, kern='RBF', f_true=None, brown_bridge=False, deterministic=True)[source]

Bases: object

Deterministic particle flow control top-level class.

Provides the necessary functions to sample the required probability flows and estimate the controls.

t1

Initial time.

Type

float

t2

end time point.

Type

float

y1

initial position.

Type

array_like

y2

terminal position.

Type

array_like

f

drift function handle.

Type

function, callable

g

diffusion coefficient or function handle.

Type

float or array_like

N

number of particles/trajectories.

Type

int

M

number of sparse points for grad log density estimation.

Type

int

reweight

determines if reweighting will follow.

Type

boolean

U

reweighting function to be employed during reweighting, dimensions \(dim_y1,t \to 1\).

Type

function, callable

dens_est
  • ‘nonparametric’non parametric density estimation (this was

    used in the paper)

  • TO BE ADDED:
    • ‘hermit1’parametic density estimation empoying hermite

      polynomials (physiscist’s)

    • ‘hermit2’parametic density estimation empoying hermite

      polynomials (probabilists’s)

    • ‘poly’ : parametic density estimation empoying simple polynomials

    • ‘RBF’ : parametric density estimation employing radial basis functions.

Type

str

kern
type of kernel: ‘RBF’ or ‘periodic’ (only the ‘RBF’ was used and gives

robust results. Do not use ‘periodic’ yet!).

Type

str

reject

parameter indicating whether non valid backward trajectories will be rejected.

Type

boolean

plotting

parameter indicating whether bridge statistics will be plotted.

Type

boolean

f_true

in case of Brownian bridge reweighting this is the true forward drift for simulating the forward dynamics.

Type

funtion, callable

brown_bridge

determines if the reweighting concearns contstraint or reweighting with respect to brownian bridge.

Type

boolean,

deterministic

indicates the type of dynamics the particles will follow. If False the flows are simulated with stochastic path sampling.

Type

boolean,

forward_sampling_Otto:

Creates samples of the forward flow.

forward sampling():

Samples the forward flow with stochatic particle trajectories.

f_seperate(x,t):

Drift for the deterministic propagation of partcles that are at time t in position x.

backward_simulation():

Sampling the backward density with stochastic particles.

reject_trajectories():

Rejects backward trajectories that do not end up in the vicinity of the initial point. Run only if the instance is attribute “reject” is set to True. Gives logging.warning messages.

forward_sampling_Otto_true():

Relevant only when forward sampling happens with Brownian bridge.

Methods

backward_simulation()

Sample time reversed flow with deterministic dynamics (or stochastic if self.deterministic == False).

bw_density_estimation(rev_ti)

Estimates the logaritmic gradient of the backward flow evaluated at particle positions of the backward flow.

calculate_u(grid_x, ti)

Computes the control at position(s) grid_x at timestep ti (i.e.

check_if_covered(X, ti)

Checks if test point X falls within forward and backward densities at timepoint timegrid[ti].

f_seperate(x, t)

Computes the deterministic forces for the evolution of the deterministic particles for the current particle positions, ie.

f_seperate_true(x, t)

(Relevant only when forward sampling happens with Brownian bridge reweighting) Wrapper for the drift function of the deterministic particles with the actual f (system drift) minus the logarithmic gradient term computed on current particles positions.

forward_sampling()

Sampling forward probability flow with stochastic particle dynamics.

forward_sampling_Otto()

Samples the forward probability flow with deterministic particle dynamics.

forward_sampling_Otto_true()

(Relevant only when forward sampling happens with Brownian bridge reweighting) Same as forward sampling but without reweighting.

density_estimation

__init__(t1, t2, y1, y2, f, g, N, M, reweight=False, U=None, dens_est='nonparametric', reject=True, kern='RBF', f_true=None, brown_bridge=False, deterministic=True)[source]
backward_simulation()[source]

Sample time reversed flow with deterministic dynamics (or stochastic if self.deterministic == False). Trajectories are stored in place in self.B array of dimensionality (dim x N x timegrid.size). self.B does not require a timereversion at the end, everything is stored in the correct order.

Returns

Returns 0 to ensure everything was executed correctly.

Return type

int

bw_density_estimation(rev_ti)[source]

Estimates the logaritmic gradient of the backward flow evaluated at particle positions of the backward flow.

Parameters

rev_ti (int,) – indicates the time point in the timegrid where the estimation will take place, i.e. for time t=self.timegrid[rev_ti.

Returns

grad_ln_b – with the logarithmic gradients of the time reversed (backward) flow (dim x N) for the timestep rev_ti.

Return type

2d-array,

calculate_u(grid_x, ti)[source]

Computes the control at position(s) grid_x at timestep ti (i.e. at time self.timegrid[ti]).

Parameters
  • grid_x (ndarray,) – size d x number of points to be evaluated.

  • ti (int,) – time index in timegrid for the computation of u.

Returns

u_t – same size as grid_x. These are the controls u(grid_x, t), where t=self.timegrid[ti].

Return type

ndarray,

check_if_covered(X, ti)[source]

Checks if test point X falls within forward and backward densities at timepoint timegrid[ti].

Parameters
  • X (array 1x dim or Kxdim) – Point in state space where control is evaluated.

  • ti (int) – Index in timegrid array indicating the time within the time interval [t1,t2].

Return type

Boolean variable - True if the text point X falls within the densities.

f_seperate(x, t)[source]

Computes the deterministic forces for the evolution of the deterministic particles for the current particle positions, ie. drift minus the logarithmic gradient term. Is used as a wrapper for evolving the particles, and can be provided to “any” ODE integrator.

Parameters
  • x (2d-array,) – Particle positions (dimension x number of particles).

  • t (float,) – Time t within the [t1,t2] interval.

Returns

Returns the deterministic forces required to ntegrate the particle positions for one time step, i.e. return \(f(x,t)-\frac{1}{2}\sigma^2\nabla \rho_t(x)\), evaluated at the current positions x and t.

Return type

2d-array

f_seperate_true(x, t)[source]

(Relevant only when forward sampling happens with Brownian bridge reweighting) Wrapper for the drift function of the deterministic particles with the actual f (system drift) minus the logarithmic gradient term computed on current particles positions. Provided for easy integration, and can be passed to ode integrators.

Parameters
  • x (2d-array,) – Particle positions (dimension x number of particles).

  • t (float,) – Time t within the [t1,t2] interval.

Returns

Returns the deterministic forces required to ntegrate the particle positions for one time step, i.e. return \(f(x,t)-\frac{1}{2}\sigma^2\nabla \rho_t(x)\), evaluated at the current positions x and t.

Return type

2d-array

forward_sampling()[source]

Sampling forward probability flow with stochastic particle dynamics. If reweighting is required at every time step the particles are appropriatelly reweighted accordint to function \(U(x,t)\)

Returns

Returns 0 to make sure everything runs correctly. The sampled density is stored in place in the array self.Z.

Return type

int

forward_sampling_Otto()[source]

Samples the forward probability flow with deterministic particle dynamics. If required at every timestep a particle reweighting takes place employing the weights obtained from the exponentiated path constraint \(U(x,t)\)

Returns

Returns 0 to make sure everything runs correctly. The sampled density is stored in place in the array self.Z.

Return type

int

forward_sampling_Otto_true()[source]

(Relevant only when forward sampling happens with Brownian bridge reweighting) Same as forward sampling but without reweighting.

Returns

Returns 0 to make sure everything runs correctly. The sampled density is stored in place in the array self.Ztr.

Return type

int

Score function estimators

score_function_estimators.my_cdist(r, y, output, dist='euclidean')[source]

Fast computation of pairwise distances between data points in r and y matrices. Stores the distances in the output array. Available distances: ‘euclidean’ and ‘seucledian’

Parameters
  • r (NxM array) – First set of N points of dimension M.

  • y (N2xM array) – Second set of N2 points of dimension M.

  • output (NxN2 array) – Placeholder for storing the output of the computed distances.

  • dist (type of distance, optional) – Select ‘euclidian’ or ‘sqeuclidian’ for Euclidian or squared Euclidian distances. The default is ‘euclidean’.

Return type

None. (The result is stored in place in the provided array “output”).

score_function_estimators.score_function_multid_seperate(X, Z, func_out=False, C=0.001, kern='RBF', l=1, which=1, which_dim=1)[source]

Sparse kernel based estimation of multidimensional logarithmic gradient of empirical density represented by samples X across dimension “which_dim” only.

  • When funct_out == False: computes grad-log at the sample points.

  • When funct_out == True: return a function for the grad log to be

    employed for interpolation/estimation of the logarithmic gradient in the vicinity of the samples.

For estimation across all dimensions simultaneously see also

Parameters
  • X (N x dim array ,) – N samples from the density (N x dim), where dim>=2 the dimensionality of the system.

  • Z (M x dim array,) – inducing points points (M x dim).

  • func_out (Boolean,) – True returns function, if False return grad-log-p on data points.

  • l (float or array-like,) – lengthscale of rbf kernel (scalar or vector of size dim).

  • C (float,) – weighting constant (leave it at default value to avoid unreasonable contraction of deterministic trajectories).

  • which ((depracated) ,) – do not use.

  • which_dim (int,) – which gradient of log density we want to compute (starts from 1 for the 0-th dimension).

Returns

res1 – that accepts as inputs 2dimensional arrays of dimension (K x dim), where K>=1.

Return type

array with logarithmic gadient of the density along the given dimension N_s x 1 or function

score_function_estimators.score_function_multid_seperate_all_dims(X, Z, func_out=False, C=0.001, kern='RBF', l=1)[source]

Sparse kernel based estimation of multidimensional logarithmic gradient of empirical density represented by samples X for all dimensions simultaneously.

  • When funct_out == False: computes grad-log at the sample points.

  • When funct_out == True: return a function for the grad log to be employed for interpolation/estimation of grad log

    in the vicinity of the samples.

Parameters
  • X (N x dim array,) – N samples from the density (N x dim), where dim>=2 the dimensionality of the system.

  • Z (M x dim array,) – inducing points points (M x dim).

  • func_out (Boolean,) – True returns function, if False returns grad-log-p evaluated on samples X.

  • l (float or array-like,) – lengthscale of rbf kernel (scalar or vector of size dim).

  • C (float,) – weighting constant (leave it at default value to avoid unreasonable contraction of deterministic trajectories).

  • kern (string,) –

    options:
    • ’RBF’: radial basis function/Gaussian kernel

    • ’periodic’: periodic, not functional yet.

Returns

res1 – that accepts as inputs 2dimensional arrays of dimension (K x dim), where K>=1.

Return type

array with logarithmic gradient of the density N_s x dim or function

score_function_estimators.score_function_multid_seperate_old(X, Z, func_out=False, C=0.001, kern='RBF', l=1, which=1, which_dim=1)[source]

Warning

!!!This version computes distances with cdist from scipy. If numba is not available use this estimator.!!!!

Sparse kernel based estimation of multidimensional logarithmic gradient of empirical density represented by samples X across dimension “which_dim” only.

  • When funct_out == False: computes grad-log at the sample points.

  • When funct_out == True: return a function for the grad log to be employed for interpolation/estimation of grad log

    in the vicinity of the samples.

Parameters
  • X (N samples from the density (N x dim), where dim>=2 the dimensionality of the system,) –

  • Z (inducing points points (M x dim),) –

  • func_out (Boolean, True returns function, if False return grad-log-p on data points,) –

  • l (lengthscale of rbf kernel (scalar or vector of size dim),) –

  • C (weighting constant (leave it at default value to avoid unreasonable contraction of deterministic trajectories)) –

  • which (return 1: grad log p(x)) –

  • which_dim (which gradient of log density we want to compute (starts from 1 for the 0-th dimension)) –

Returns

res1 – that accepts as inputs 2dimensional arrays of dimension (K x dim), where K>=1.

Return type

array with density along the given dimension N_s x 1 or function

For estimation across all dimensions simultaneously see also

Reweighting

Created on Sun Dec 12 04:14:07 2021

@author: maout

optimal_transport_reweighting.reweight_optimal_transport_multidim(samples, weights)[source]

Computes deterministic transport map for particle reweighting. Particle state is multidimensional.

Parameters
  • samples (array-like,) – Samples from distribution M x dim , with dim>=2.

  • weights (array-like,) – weights for each sample M.

Returns

T – transport map.

Return type

array like,

Reweighting particles according to ensemble transform particle filter (ETPF) algorithm proposed by Reich 2013. Instead of particle resampling, ETPF employes Optimal Transport to compute a deterministic particle shift which minimises the expected distances between the particles before and after the transformation. :math: CO = X^T cdot X :math: CO = diag(CO)*ones(1,M) -2*CO + ones(M,1)*diag(CO)’ :math: [dist,T] = emd(ww,ones(M,1)/M,CO,-1,3) :math: T = T cdot M

Kernels

Classes

class DeterministicParticleFlowControl.DPFC(t1, t2, y1, y2, f, g, N, M, reweight=False, U=None, dens_est='nonparametric', reject=True, kern='RBF', f_true=None, brown_bridge=False, deterministic=True)[source]

Deterministic particle flow control top-level class.

Provides the necessary functions to sample the required probability flows and estimate the controls.

t1

Initial time.

Type

float

t2

end time point.

Type

float

y1

initial position.

Type

array_like

y2

terminal position.

Type

array_like

f

drift function handle.

Type

function, callable

g

diffusion coefficient or function handle.

Type

float or array_like

N

number of particles/trajectories.

Type

int

M

number of sparse points for grad log density estimation.

Type

int

reweight

determines if reweighting will follow.

Type

boolean

U

reweighting function to be employed during reweighting, dimensions \(dim_y1,t \to 1\).

Type

function, callable

dens_est
  • ‘nonparametric’non parametric density estimation (this was

    used in the paper)

  • TO BE ADDED:
    • ‘hermit1’parametic density estimation empoying hermite

      polynomials (physiscist’s)

    • ‘hermit2’parametic density estimation empoying hermite

      polynomials (probabilists’s)

    • ‘poly’ : parametic density estimation empoying simple polynomials

    • ‘RBF’ : parametric density estimation employing radial basis functions.

Type

str

kern
type of kernel: ‘RBF’ or ‘periodic’ (only the ‘RBF’ was used and gives

robust results. Do not use ‘periodic’ yet!).

Type

str

reject

parameter indicating whether non valid backward trajectories will be rejected.

Type

boolean

plotting

parameter indicating whether bridge statistics will be plotted.

Type

boolean

f_true

in case of Brownian bridge reweighting this is the true forward drift for simulating the forward dynamics.

Type

funtion, callable

brown_bridge

determines if the reweighting concearns contstraint or reweighting with respect to brownian bridge.

Type

boolean,

deterministic

indicates the type of dynamics the particles will follow. If False the flows are simulated with stochastic path sampling.

Type

boolean,

forward_sampling_Otto:

Creates samples of the forward flow.

forward sampling():

Samples the forward flow with stochatic particle trajectories.

f_seperate(x,t):

Drift for the deterministic propagation of partcles that are at time t in position x.

backward_simulation():

Sampling the backward density with stochastic particles.

reject_trajectories():

Rejects backward trajectories that do not end up in the vicinity of the initial point. Run only if the instance is attribute “reject” is set to True. Gives logging.warning messages.

forward_sampling_Otto_true():

Relevant only when forward sampling happens with Brownian bridge.

Methods

backward_simulation()

Sample time reversed flow with deterministic dynamics (or stochastic if self.deterministic == False).

bw_density_estimation(rev_ti)

Estimates the logaritmic gradient of the backward flow evaluated at particle positions of the backward flow.

calculate_u(grid_x, ti)

Computes the control at position(s) grid_x at timestep ti (i.e.

check_if_covered(X, ti)

Checks if test point X falls within forward and backward densities at timepoint timegrid[ti].

f_seperate(x, t)

Computes the deterministic forces for the evolution of the deterministic particles for the current particle positions, ie.

f_seperate_true(x, t)

(Relevant only when forward sampling happens with Brownian bridge reweighting) Wrapper for the drift function of the deterministic particles with the actual f (system drift) minus the logarithmic gradient term computed on current particles positions.

forward_sampling()

Sampling forward probability flow with stochastic particle dynamics.

forward_sampling_Otto()

Samples the forward probability flow with deterministic particle dynamics.

forward_sampling_Otto_true()

(Relevant only when forward sampling happens with Brownian bridge reweighting) Same as forward sampling but without reweighting.

density_estimation

__init__(t1, t2, y1, y2, f, g, N, M, reweight=False, U=None, dens_est='nonparametric', reject=True, kern='RBF', f_true=None, brown_bridge=False, deterministic=True)[source]
backward_simulation()[source]

Sample time reversed flow with deterministic dynamics (or stochastic if self.deterministic == False). Trajectories are stored in place in self.B array of dimensionality (dim x N x timegrid.size). self.B does not require a timereversion at the end, everything is stored in the correct order.

Returns

Returns 0 to ensure everything was executed correctly.

Return type

int

bw_density_estimation(rev_ti)[source]

Estimates the logaritmic gradient of the backward flow evaluated at particle positions of the backward flow.

Parameters

rev_ti (int,) – indicates the time point in the timegrid where the estimation will take place, i.e. for time t=self.timegrid[rev_ti.

Returns

grad_ln_b – with the logarithmic gradients of the time reversed (backward) flow (dim x N) for the timestep rev_ti.

Return type

2d-array,

calculate_u(grid_x, ti)[source]

Computes the control at position(s) grid_x at timestep ti (i.e. at time self.timegrid[ti]).

Parameters
  • grid_x (ndarray,) – size d x number of points to be evaluated.

  • ti (int,) – time index in timegrid for the computation of u.

Returns

u_t – same size as grid_x. These are the controls u(grid_x, t), where t=self.timegrid[ti].

Return type

ndarray,

check_if_covered(X, ti)[source]

Checks if test point X falls within forward and backward densities at timepoint timegrid[ti].

Parameters
  • X (array 1x dim or Kxdim) – Point in state space where control is evaluated.

  • ti (int) – Index in timegrid array indicating the time within the time interval [t1,t2].

Return type

Boolean variable - True if the text point X falls within the densities.

density_estimation(ti, rev_ti)[source]
f_seperate(x, t)[source]

Computes the deterministic forces for the evolution of the deterministic particles for the current particle positions, ie. drift minus the logarithmic gradient term. Is used as a wrapper for evolving the particles, and can be provided to “any” ODE integrator.

Parameters
  • x (2d-array,) – Particle positions (dimension x number of particles).

  • t (float,) – Time t within the [t1,t2] interval.

Returns

Returns the deterministic forces required to ntegrate the particle positions for one time step, i.e. return \(f(x,t)-\frac{1}{2}\sigma^2\nabla \rho_t(x)\), evaluated at the current positions x and t.

Return type

2d-array

f_seperate_true(x, t)[source]

(Relevant only when forward sampling happens with Brownian bridge reweighting) Wrapper for the drift function of the deterministic particles with the actual f (system drift) minus the logarithmic gradient term computed on current particles positions. Provided for easy integration, and can be passed to ode integrators.

Parameters
  • x (2d-array,) – Particle positions (dimension x number of particles).

  • t (float,) – Time t within the [t1,t2] interval.

Returns

Returns the deterministic forces required to ntegrate the particle positions for one time step, i.e. return \(f(x,t)-\frac{1}{2}\sigma^2\nabla \rho_t(x)\), evaluated at the current positions x and t.

Return type

2d-array

forward_sampling()[source]

Sampling forward probability flow with stochastic particle dynamics. If reweighting is required at every time step the particles are appropriatelly reweighted accordint to function \(U(x,t)\)

Returns

Returns 0 to make sure everything runs correctly. The sampled density is stored in place in the array self.Z.

Return type

int

forward_sampling_Otto()[source]

Samples the forward probability flow with deterministic particle dynamics. If required at every timestep a particle reweighting takes place employing the weights obtained from the exponentiated path constraint \(U(x,t)\)

Returns

Returns 0 to make sure everything runs correctly. The sampled density is stored in place in the array self.Z.

Return type

int

forward_sampling_Otto_true()[source]

(Relevant only when forward sampling happens with Brownian bridge reweighting) Same as forward sampling but without reweighting.

Returns

Returns 0 to make sure everything runs correctly. The sampled density is stored in place in the array self.Ztr.

Return type

int