Added sampling_density to stats (replaces #1572)

This is a rebased and touched up version to fix #1572.

There is also an attempt at fixing the intermittent failure in test_rv that involves pspace.