Visualising a Circular Density

This afternoon, Jean-Luc asked me some help about an old post I did publish, minuit, l’heure du crime; and some graphs published a few days after, where I used a different visualisation, in another post.

The idea is that the hour can be seen as circular, in the sense that 23:58 is actually very close to 00:03. So when we use a nonparametric kernel estimator of time events, we have to take into account that property. More specifically, consider the density of an angle, i.e. a function f(\cdot) such that \int_0^{2\pi}f(\omega)d\omega=1
with a circular relationship, in the sense that f(\omega+2\pi)=f(\omega).

In the dataset sent by Jean-Luc, we have some thefts in a big city, in France. The dataset is a simple spreadsheet with one columns, with ’00:20′ or ’17:45′ inside. Those are more or less reported time of thefts, as declared to the police.

B=read.table("Temp_Heures_VV.csv",header=TRUE,
  sep=";")
HM=as.character(B[,1])
H=substr(HM,1,(nchar(HM)-3))
M=substr(HM,(nchar(HM)-1),(nchar(HM)))
X=as.numeric(H)+as.numeric(M)/60

The time is a number from 0 to 24.

U=seq(0,1,by=1/250)
O=U*2*pi
U12=seq(0,1,by=1/24)
O12=U12*2*pi
OM=2*pi*X/24
XL=c(X-24,X,X+24)
d=density(X)
d=density(XL,bw=d$bw,n=1500)
I=which((d$x>=6)&(d$x<=30))
Od=d$x[I]/24*2*3.141592-3.141592/2
Dd=d$y[I]/max(d$y)+1

The idea to get a nice density estimation is to use a simple mirror technique : we have three versions of the data, one for today, one for yesterday, and one for tomorrow. Of course, we have to use a shorter bandwidth.

R=1/24/max(d$y)/3+1 
plot(cos(O),-sin(O),xlim=c(-2,2),ylim=c(-2,2),
     type="l",axes=FALSE,xlab="",ylab="")
for(i in 3.14159/12*(0:12)){ 
  segments(-cos(i),-sin(i),cos(i),sin(i),col="grey")} 
segments(.9*cos(O12),.9*sin(O12),
         1.1*cos(O12),1.1*sin(O12))
text(.7,0,"6")
text(-.7,0,"18")
text(0,-.7,"12")
text(0,.7,"24")
R=1/24/max(d$y)/3+1
lines(R*cos(O),R*sin(O),lty=2)
AX=R*cos(Od);AY=-R*sin(Od)
BX=Dd*cos(Od);BY=-Dd*sin(Od)
COUL=rep("blue",length(AX))
COUL[R<Dd]="red"
CM=cm.colors(200)
a=trunc(100*Dd/R)
COUL=CM[a]
segments(AX,AY,BX,BY,col=COUL,lwd=2)
lines(Dd*cos(Od),-Dd*sin(Od),lwd=2)

The dotted line would be a uniform distribution over the day. The true distribution is the black bold line. The area in purple is when we have more crimes, and the blue line is when we have less crimes. The blue area is equal to the purple one. There is a clear symmetry in the evening around midnight (but not during the day : 6 am is not the same as 6 pm). This graph is the circular visualisation of the kernel density estimator, the same way the rose diagram is the circular visualisation of the histogram.


OpenEdition suggests that you cite this post as follows:
Arthur Charpentier (October 7, 2015). Visualising a Circular Density. Freakonometrics. Retrieved September 17, 2024 from https://doi.org/10.58079/ov0q


4 thoughts on “Visualising a Circular Density”

  1. Bonjour,

    Ce billet est très intéressant mais il suppose que l’heure du crime soit connue avec précision. C’est une hypothèse pertinente pour certains types de crimes (violences aux personnes, etc) mais moins pertinents pour d’autres (vols, cambriolages, etc). Dans ce cas, on aura un intervalle de temps qui dit “ma voiture a été volée entre 8h00 du soir le 20 janvier 2012 et 9h00 du matin le 21 janvier”. Est-ce qu’il existe des méthodes pour estimer la densité de l’heure du vol à partir de données de ce type ? Ce serait super intéressant.

  2. Hello,

    I think the notion of area under the curve to represent probability is misleading in the case of polar graphs.
    You have to take into account the jacobian of the change of variable $(x, y) \rightarrow (r, \theta)$.
    The area under the curve in cartesian coordinates is

    $$\int_0^{2\pi}f(\omega)d\omega = 1,$$

    but in polar coordinates, this transforms into

    $$\int_0^{2\pi}\int_0^{f(\omega)}r dr d\omega$$

    (and this is no longer one, by the way).

    Notice the $r$ in the second integral: it will shrink (resp. expand) the area under (resp.above) the uniform case, so that eventually, the purple area will be bigger than the blue one.

    If you expand $f(\omega)$ into $f(\omega) = 1/(2\pi) + \delta(\omega)$, [the $1/(2\pi)$ term represents the uniform case, so that the integral of the additive term $\delta$ is zero] it can be shown that this shrinkage/expansion effect is proportionnal to the square of $\delta$. So this effect is tiny if the probalility is always close to the uniform case, but can become larger in other cases. You should plot something like $\sqrt(f(\omega))$ instead of $f(\omega)$ to avoid it.

    Actually, this problem affects rose wind as well .

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.