Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Average start time handling midnight

by oiskuu (Hermit)
on Jul 21, 2016 at 20:54 UTC ( [id://1168272]=note: print w/replies, xml ) Need Help??


in reply to Re: Average start time handling midnight
in thread Average start time handling midnight

Not that complicated, really, although it does seem a bit silly to do trigonometry where simple arithmetic would suffice.

#! /usr/bin/perl -wl my @times = qw( 17:00 19:00 11:00 13:00 23:00 01:00 10:30 13:00 19:40 01:20 16:00 02:00 ); # keep angles in range -pi .. pi sub _PI () { 2 * atan2(1, 0) } sub _TC () { 24 * 60 / (2 * _PI) } sub time2angle { map { my ($h, $m) = split /:/; _PI - (60 * $h + $m) / _TC } @_ } sub angle2time { map { my $m = int _TC * (_PI - $_); sprintf "%02d:%02d", $m / 60, $m % 60 } @_ } use List::Util qw( sum ); sub circ_avg { atan2 sum(map sin, @_), sum(map cos, @_) } for (; @times > 1; shift @times) { my @t = @times[0, 1]; print "@t => @{[angle2time circ_avg time2angle @t]}"; }

Thank you for the wikipedia link.

Replies are listed 'Best First'.
Re^3: Average start time handling midnight
by pryrt (Abbot) on Jul 21, 2016 at 22:40 UTC

    When averaging two times, yes, it's silly. But when averaging more than two, it comes out with a very different result.

    ... # your code thru the definition of sub circ_avg; print "@times"; print " => circle vs line"; print " => @{[angle2time circ_avg time2angle @times]} vs @{ +[angle2time( sum( time2angle @times ) / @times ) ]}"; for (; @times > 1; shift @times) { my @t = @times[0, 1]; print "@t => @{[angle2time circ_avg time2angle @t]} vs @{[angle2ti +me( sum( time2angle @t ) / @t ) ]}"; } __END__ __OUTPUT__ 17:00 19:00 11:00 13:00 23:00 01:00 10:30 13:00 19:40 01:20 16:00 02:0 +0 => circle vs line => 17:46 vs 12:12 17:00 19:00 => 18:00 vs 18:00 19:00 11:00 => 15:00 vs 15:00 11:00 13:00 => 12:00 vs 12:00 13:00 23:00 => 18:00 vs 18:00 23:00 01:00 => 00:00 vs 12:00 01:00 10:30 => 05:45 vs 05:45 10:30 13:00 => 11:45 vs 11:45 13:00 19:40 => 16:20 vs 16:20 19:40 01:20 => 22:30 vs 10:30 01:20 16:00 => 20:40 vs 08:40 16:00 02:00 => 21:00 vs 09:00

    The pairs of times come out to a simple average that mostly matches the circles (except on the ones with midnight between time1 and time2); but you get very different results between the two averages for the entire list. Previously, I had compared my own implementation of the angular average (yours is better) to the results of salva's solution of finding the center-with-minimum-variance and found that the results for salva's example list (or a list of random times) gave very similar means to the angular average, which would be very different from the arithmetic mean for the same set of data

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1168272]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 12:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found