Diversity prediction theorem: Difference between revisions
→{{header|Python}}: Tidied, updated primitives.
(→{{header|Python}}: Tidied, updated primitives.) |
|||
Line 768:
# diversityValues :: Num a => a -> [a] ->
# { mean-Error :: a, crowd-error :: a, diversity :: a }
def diversityValues(x):
'''The mean error, crowd error and
diversity, for a given observation x
and a non-empty list of predictions ps.
'''
def go(ps):
mp = mean(ps)
return {
'mean-error': meanErrorSquared(x)(ps),
'crowd-error': pow(x - mp, 2),
'diversity': meanErrorSquared(mp)(ps)
}
return go
# meanErrorSquared :: Num -> [Num] -> Num
def meanErrorSquared(x):
'''The mean of the squared differences
between the observed value x and
a non-empty list of predictions ps.
'''
def go(ps):
return mean([
pow(p - x, 2) for p in ps
])
return go
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
Line 792 ⟶ 823:
# ---------------------- FORMATTING ----------------------
# showDiversityValues :: Num -> [Num] -> Either String String
Line 830 ⟶ 832:
list of predictions p.
'''
def go(
def showDict(dct):
w = 4 + max(map(len, dct.keys()))
Line 845 ⟶ 847:
def showProblem(e):
return (
unlines(map(
isinstance(e, list)
) else
) + '\n'
Line 854 ⟶ 856:
bindLR(numLR(x))(
lambda n: bindLR(numsLR(ps))(
compose(Right
)
)
)
)
return
#
# Left :: a -> Either a b
def Left(x):
'''Constructor for an empty Either (option type) value
with an associated string.
'''
return {'type': 'Either', 'Right': None, 'Left': x}
# Right :: b -> Either a b
def Right(x):
'''Constructor for a populated Either (option type) value'''
return {'type': 'Either', 'Left': None, 'Right': x}
Line 882 ⟶ 885:
Two computations sequentially composed,
with any value produced by the first
passed as an argument to the second.
'''
def go(mf):
mf(m.get('Right')) if None is m.get('Left') else m
)
return go
# compose
def compose(
'''Composition, from right to left,
of a series of functions.
'''
def go(f, g):
def fg(x):
return f(g(x))
return fg
return reduce(go, fs, identity)
# concatMap :: (a -> [b]) -> [a] -> [b]
def concatMap(f):
'''
The list monad can be derived by using a function f which
wraps its output in a list,
(using an empty list to represent computational failure).
'''
def go(xs):
return
return
Line 910 ⟶ 921:
def either(fl):
'''The application of fl to e if e is a Left value,
or the application of fr to e if e is a Right value.
'''
return lambda fr: lambda e: fl(e['Left']) if (
None is e['Right']
Line 916 ⟶ 928:
#
def
'''
return x
# indented :: Int -> String -> String
def indented(n):
'''String indented by n multiples
of four spaces.
'''
return lambda s: (4 * ' ' * n) + s
# mean :: [Num] -> Float
Line 937 ⟶ 955:
return Right(x) if (
isinstance(x, (float, int))
) else Left(
'Expected number, saw: ' + (
str(type(x)) + ' ' + repr(x)
)
)
Line 980 ⟶ 1,002:
# showPrecision :: Int -> Float -> String
def showPrecision(n):
'''A string showing a floating point number
at a given degree of precision.'''
return str(round(x, n))
return go
|