You can calculate the average and the variance for the 24h intervals centered around every hour (0, 1, 2,... 23) and pick the center which minimizes the variance:
use POSIX qw(fmod); my @data = (1, 1, 1, 0, 1, 2, 3, 4, 21, 22, 20, 22, 23, 20, 22); sub circular_average { my ($pivot, $round, @data) = @_; my $sum = 0; my $sum2 = 0; my $displacement = 1.5 * $round - $pivot; for my $data (@data) { my $displaced = fmod($data + $displacement, $round); # printf "%2i->%3i,%2i ", $data, $displaced, $displaced - $dis +placement; $sum += $displaced; $sum2 += $displaced * $displaced; } # print "\n"; my $inv_n = 1.0 / @data; my $avg = fmod($inv_n * $sum + 0.5 * $round + $pivot, $round); wantarray ? ($avg, $inv_n * $sum2 - $inv_n * $inv_n * $sum * $sum) + : $avg; } my ($best_avg, $best_s2); for my $time (0..23) { my ($avg, $s2) = circular_average $time, 24, @data; if (not defined $best_s2 or $best_s2 > $s2) { $best_avg = $avg; $best_s2 = $s2; } } printf "avg: %.1f, s2: %.1f\n", $best_avg, $best_s2;
Then, you can even iterate a few times centering the interval around the best average ($best_avg) found:
for (0..5) { ($best_avg, $best_s2) = circular_average $best_avg, 24, @data; printf "avg: %.1f, s2: %.1f\n", $best_avg, $best_s2; }

In reply to Re: Average start time handling midnight by salva
in thread Average start time handling midnight by chrisjej

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.