My favorites | Sign in
Project Home Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from datetime import datetime, timedelta
import math

class ScoreCalc():
"""
Base functions for calculating half-life values from a stream of scoring events.

Time units are abstract and zero-based. The default half-life is 1 time unit.

The Net score is valid at a particular time, tLast. LogS is globally comparable as it is
based at time t = 0.
"""

def __init__(self, tHalf=1.0, value=0.0, tLast=0.0):
"""
The score cannot be 0 - since we use Log(S) as a ordering key. Instead, all scores
are based at value = 1 at time = 0. Negative log scores would occur for score values less
than 1 at time = 0 - these are not allowed.
"""
self.tHalf = float(tHalf)
self.k = 0.5 ** (1.0/self.tHalf)
self.tLast = 0.0
self.LogS = 0.0
self.Increment(value, tLast)

def Increment(self, value=0.0, t=0.0):
value = float(value)

t = float(t)

if t > self.tLast:
self.S = 2.0 ** (self.LogS - t/self.tHalf)
self.S += value
self.tLast = t
else:
self.S = 2.0 ** (self.LogS - self.tLast/self.tHalf)
self.S += (self.k ** (self.tLast - t)) * value

try:
self.LogS = math.log(self.S)/math.log(2) + self.tLast/self.tHalf
except:
# On underflow - reset to minimum value - 1 at time zero
self.S = 1.0
self.tLast = 0.0
self.LogS = 0.0

Change log

r498 by mckoss on Aug 31, 2009   Diff
Kill self.S - does not need to be stored
Go to: 
Sign in to write a code review

Older revisions

r479 by mckoss on Jun 3, 2009   Diff
Decouple util from timescore module -
move unit tests out
r473 by mckoss on Jun 2, 2009   Diff
offline, simplify SS, from notebook
r472 by mckoss on Jun 1, 2009   Diff
Bulk load of maps for /user/X display
from comments
All revisions of this file

File info

Size: 1579 bytes, 46 lines
Powered by Google Project Hosting