I looked into this because a friend wanted to know how this would work using code. If you can't see the animation of the gif - the size of the spheres changes according to the point density.
You can get this effect by default from the scatter node on the Output Attributes Tab

The node has a formula using the density attribute to calculate an appropiate pscale value.
You can find the info on that in the docs for the scatter node. The values are slightly changing in different situations.
- point on a curve --> exactly 0.5/density
- point on a surface --> ~0.537/sqrt(density)
- point in a volume or tetrahedron ~0.561/cbrt(density)
What does that mean for us? I created this example on a grid so I am dealing with a surface. Then I need the density of my surface. I am using the measure node for that and promote the area attribute to points just to make it easy to use in a point wrangle.

Now the formula for pscale is .537 / sqrt(density) - that means I need to calc density first. Simple enough - thats the amount of points divided by the area value.
Then simply type in the formula as it is written in the docs and you get a proper result.
Having this formula at hand allows you to make use of it when your points are not generated by a scatter node.

Cheers
Dave