borglab / SwiftFusion

Apache License 2.0
115 stars 13 forks source link

Swifty MCMC and python iterop #96

Open dellaert opened 4 years ago

dellaert commented 4 years ago

We should think/talk about MCMC algorithm API and tfp interop.

@marcrasi says:

As a direct translation of the Python API, the current API looks right to me. If you want to go this direction, then that sounds good to me!


The "Swifty" way can make some things a lot nicer so I'll describe a "Swifty" idea here to tempt you :)

The TransitionKernel looks like an infinite Sequence. So there's no need to define a protocol TransitionKernel -- you can conform things to Sequence instead.

For example, RandomWalkMetropolis would be something like:

struct RandomWalkMetropolis<State>: Sequence, IteratorProtocol {

  var previousLogProb: Double
  var currentState: State

  let target_log_prob_fn: (State) -> Double
  let new_state_fn : (State)->State

  init(_ target_log_prob_fn: @escaping (State) -> Double, _ new_state_fn : @escaping (State)->State, _ initialState: State) {
    ...
  }

  mutating func next() -> State {
    let result = currentState

    // update `currentState` and `previousLogProb` according to metropolis rules
    ...

    return result
  }
}

This is nice because it allows us to use all the Sequence algorithms. For example, sampleChain becomes something like:

Array(sequence.dropFirst(num_burnin_steps).prefix(num_results))

Originally posted by @marcrasi in https://github.com/borglab/SwiftFusion/pull/95/files

marcrasi commented 4 years ago

I'll add an agenda item for this to our Friday meeting!

dabrahams commented 4 years ago

While we're in this area, consider #143