2

I need to get average sleep interval from following data:

  • 22:00-06:00
  • 00:00-08:00
  • 02:00-10:00

=> expected: 00:00-08:00

  • 22:00-06:00
  • 00:00-08:00
  • 02:00-10:00
  • 04:00-08:00

=> expected: 01:00-08:00

The problem is oscillation around midnight, where part is under 00:00 and part over 00:00. Simple mean 22+00+02+04 doesn't work. I could count number of times over midnight (3 in this case) and if it's more than those before midnight, I should add 25 co compensate. But this doesn't count with those people, who work at night and go sleep around 8-14!

My theory is: First, somehow I need found peak, something like the most frequented area (e.g., in 9-10 there is 5 record, in 10-11, there is 3 etc.) and then I can decide co compensate border values by adding 24 hours.

What do you think?

Tomas Votruba
  • 23,240
  • 9
  • 79
  • 115

3 Answers3

1

What about taking into account relative difference with midnight ? The result would be (-2+0+2+4)/4 = 00:45

Guillaume Poussel
  • 9,572
  • 2
  • 33
  • 42
0

Determine a lower limit where you can say

Okay, people won't go to sleep this time of the day, if I get this time, it must mean they worked all night and only sleep now

and if the time is below that limit, add 24 hours to both start and finish.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
0

Making the assumption that the person is not sleeping more than 24 hours, then make a method to calculate like so (pseudo code):

calculateSleepTime(sleepTime, wakeupTime) {
    if (sleepTime > wakeupTime) {
        return (24 - sleepTime) + wakeupTime;
    } else {
        return wakeupTime - sleepTime;
    }
}

averageSleepTime(sleepTimesArray) {
    totalSleptTime = 0;
    totalTimesSlept = 0;

    foreach (oneSleepTime in sleepTimesArray) { 
        totalTimesSlept++;
        totalSleptTime += calculateSleepTime(oneSleepTime);
    }

    return totalSleptTime / totalTimesSlept;
}

After you get the average sleep time, calculate either the average sleep time, or average wake up time, and do addition/substraction to find your interval. The alternative to this is finding the average sleep time and the average wakeup time (taking relative to midnight times into account).

abelito
  • 1,094
  • 1
  • 7
  • 18