Schrodinger is an (early-stage) project for probabilistic programming in Scala 3, built for the Cats ecosystem. At its heart is the RVT monad transformer for modeling pseudo-random effects that is designed to fully compose with asynchronous effects such as IO. The goal is that this construct will facilitate the implementation of high-performance, concurrent Monte Carlo algorithms for simulation and inference.
Furthermore, Schrodinger encodes the foundational probability distributions as a set of typeclasses to be used to write "universal" probabilistic programs, for which simulators and inference algorithms can be automatically derived by implementing interpreters in terms of these same typeclasses.
libraryDependencies += "com.armanbilge" %% "schrodinger" % "0.3-193-ed9a8ba"kernel: essential typeclasses for writing probabilistic programs.Random[F[_]]encodes the primitive of generating random bits in the form ofIntandLong.PseudoRandom[F[_], G[_], S]: encodes the ability to pseudo-randomly simulate ("compile") aRandomeffectFto another effectGvia a seedS. ExtendsRandom[F].Distribution[F[_], P, X]: the KleisliP => F[X], encoding the mapping from parametersP(e.g., the mean and standard deviation of a Gaussian) for a distribution onX(e.g., the reals represented asDouble) to an instance of an effectF[X](e.g., an effect implementing theRandomtypeclass).- Various
case classes parameterizing different distribution families, to be used as arguments forPabove, as well as convenient aliases that can be used with the usual typeclass syntax. Density[F[_], X, R]: the KleisliX => F[R], encoding the probability density (or probability mass) function in some effectF.
random: samplers for the distribution families inkernel. These are implemented purely monadically, in terms ofRandom[F]or each other.schrodinger: the core module, also brings inrandom.RVT[F[_], S, A]: a monad transformer for pseudo-random effects. Use this to simulate your probabilistic programs. Externally it is pure, although internally it is implemented with a mutable RNG for performance. Notably, it implements all the Cats Effect typeclasses up toAsync(given thatFis equally capable) by utilizing the underlying RNG's capability to deterministically "split" itself, such that each fiber has its own source of randomness. Not only does this avoid contention and synchronization, it makes it possible to write pseudo-random programs that are concurrent yet deterministic, and thus reproducible. Anyone who has debugged a complex Monte Carlo algorithm knows this is a big deal.Rng[S]: a mutable and thus unsafe random number generator with stateS.RngDispatcher[F[_], S]: "dispatches" a mutable RNG that can be used to run pseudo-random imperative programs, for interop with unsafe lands. This also relies on the splitting capability described above.
stats:Densityimplementations for the distribution families inkernel.monte-carlo: algorithms and datastructures for Monte Carlo inference.WeightedandWeightedT: encodes a sample from a distribution along with its weight and probability density. This is useful for implementing importance sampling-based algorithms.ImportanceSampler: derives a sampler for a distributionPin terms of a sampler for a distributionQ.
math: assorted math stuff.LogDoublefor probability calculations in log space.
laws: currently empty besides a silly law forPseudoRandom. Still figuring this one out in #2.kernel-testkit: currently mostly used to testrandom.- the
PureRVTmonad, implemented in terms of Cats'StateT. It is completely pure, unlikeRVTin core which is run with an unsafe mutable RNG. - *waves hands*
Eqfor a pseudo-random effectF.
- the
testkit: used to testRVT.
If not readily apparent, various aspects of the design are heavily influenced by Cats Effect 3.
monte-carlois like a "std" lib, and so-called middlewares can ideally be implemented only in terms of that andkernel. The implementations provided by therandomandstatsmodules andRVTitself are only needed at runtime and indeed can be substituted with (more performant!) alternatives.- The unsafe
Rngthat is used to simulate anRVTis kind of like the unsafeIORuntimethat runsIO. RngDispatcherfacilitates interop with "unsafe lands" inspired by theDispatcherin CE3.
