The density transform (density) finds the density of a probability distribution at a particular point. This transform is a specialized form of the disintegrate transform that also computes the expectation of the probabilistic distribution as part of its work.


Your Hakaru program must be a probability distribution (type measure(x)) in order to use the density transform. Most Hakaru programs that end with a return statement do not meet this requirement because they return values instead of functions on values.

You can use the density transform in the command line by calling:


This transformation will produce a new Hakaru program containing an anonymous function representing the density function for that model.


The normal distribution is a commonly used distribution in probabilistic modeling. A simple Hakaru program modelling the simplest normal distribution is:


Assuming that this program is named, we can calculate the density function for this distribution by running:


Note: The output for density will be printed in the console. You can easily save this program to a file by redirecting the output to a file by calling density > For this example, we will call our new program

When you open the new Hakaru program,, you will find an anonymous Hakaru function. You can then assign values to the function’s arguments to test the probabilistic density at a specific point in the model.

fn x0 real: 
 (exp((negate(((x0 + 0) ^ 2)) / 2)) / 1 / sqrt((2 * pi)) / 1)

For example, if you wanted to test the density at 0.2, you could alter to:

normalDensity = fn x0 real: 
 (exp((negate(((x0 + 0) ^ 2)) / 2)) / 1 / sqrt((2 * pi)) / 1)

return normalDensity(0.2)

You could then use the hakaru command to have Hakaru compute the density:

$ hakaru | head -n 1

Note: If the argument head -n 1 is omitted, the hakaru command will print out the resulting density value indefinitely.