0
$\begingroup$

Let's say my dataset is composed by the time (hour) of when a user uses his TV, over a month. I may have something like this :
X = [2, 2, 2, 2, 2, 8, 12, 22, 22, 22, 22, 22] .
Which means :
- he used the tv 5 times at 2am .
- he used the tv 1 times at 8am .
- he used the tv 1 times at 12am .
- he used the tv 5 times at 10pm .

I would like to get the hour where the probability that the user uses his tv is the maximum.

What I did is that I used a kernel density estimator to estimate the density of the repartition over 24 hours. Then I picked the tick with the highest density.

The thing is that this dataset is hourly based, which means it's "periodic".

For my example, because most of the samples are between 10pm and 2 am, I would like to have a result around midnight. The kernel estimator doesn't give me this result because it doesn't take into account that 10pm and 2am are close each other.

I was thinking to do the following. For a given hour h :
- add the value h to the kernel estimator
- add the value h + 24 to the kernel estimator
- add the value h + 48 to the kernel estimator
- Get the tick t with the highest density given that t statisfies 24 < t < 48

But unfortunately it doesn't give good result.

How could I get around this problem ? Thanks.

$\endgroup$
0
$\begingroup$

I think it will be easier if you transform your data. Think of "hours" as angles on a clock. Each hour is 1/24 of the full circle = 15° or 0.26180 in radians.

Now the problem is that the mean value of two angles can be somewhat misleading. In your case: the average of 30° and 330° = 180° (noon). But the CLOSEST mean is 0° (midnight).

What does the trick really is converting your data into cartesian coordinates using trigonometry. Proceed through the following steps:

  1. sum up all cos(hour_angle) for all angles you want to calculate your mean of. Remember to use radians for that. Let's call that sum "x"
  2. sum up all sin(hour_angle) likewise. The sum shall be "y"
  3. Calculate the arctan2 of x and y, which is the angle of your average hour
  4. Convert the angle back to an actual time

The arctan2 gives you the arcus-tangens of a cartesian coordinate between -pi and +pi, so there is no 2pi, 4pi, 6pi, ... which is exactly what we want - a clean transition over midnight.

Let's give it a try for your test data. Remember that 1 hour equals 15° on a 24h-clock or a radians of 0.2618

  1. x = cos(0.52360)*5 + cos(2.0944) + ... = 7.16025
  2. y = sin(0.52360)*5 + sin(2.0944) + sin(3.1416) + sin(5.7596)*5 = 0.86603
  3. arctan2(y, x) = arctan2(0.86603, 7.16025) = 0.12036 (rad) = 6.89637°
  4. 6.89637° = 0.45976 hours = 27.59min = 00h 27min 35sec

Brief validation: 5*22:00 and 5*2:00 and 12:00 cancel each other out. Without the 8:00 time, your result would be 0° = midnight. So you have one value of 8:00 out of 12 in total which slightly inclines the average towards early morning. Looks good to me. You might want to double check my numbers and be sure to do it all in one calculation (e.g. Excel, python, Java ...) to avoid rounding problems.

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.