If you are working in science, chances are that you have encountered a density that you can only evaluate to a constant factor. If you want to sample from such a distribution, well-studied methods exist such as Markov Chain Monte Carlo or rejection sampling. You may also use importance sampling to get properties about the target distribution such as its expectation.

In this post we will use **normalizing flows **(that I described in a previous post) to fit the target density. Normalizing flows are particularly powerful because once trained, they allow sampling from the learned density and/or evaluate the density of new data points.

In particular, we will implement the paper Variational Inference with Normalizing Flows in about 100 lines of code.

We will focus on the section in the paper where they fit unnormalized densities. For that matter, we will use planar flows that are defined as