Jump to content

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 ----------------------
# 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.
'''
return lambda ps: mean(list(map(
lambda y: pow(y - x, 2),
ps
)))
 
 
# 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 lambda ps: go(ps)
 
 
# FORMATTING ----------------------------------------------
 
# showDiversityValues :: Num -> [Num] -> Either String String
Line 830 ⟶ 832:
list of predictions p.
'''
def go(x, ps):
def showDict(dct):
w = 4 + max(map(len, dct.keys()))
Line 845 ⟶ 847:
def showProblem(e):
return (
unlines(map(indentindented(1), e)) if (
isinstance(e, list)
) else indentindented(1)(repr(e))
) + '\n'
 
Line 854 ⟶ 856:
bindLR(numLR(x))(
lambda n: bindLR(numsLR(ps))(
compose(Right)(, diversityValues(n))
)
)
)
)
return lambda ps: go(x, ps)
 
 
# GENERIC ------------------------------ GENERIC FUNCTIONS -------------------
 
# Right :: b -> Either a b
def Right(x):
'''Constructor for a populated Either (option type) value'''
return {'type': 'Either', 'Left': None, 'Right': x}
 
 
# 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.'''
'''
return lambda mf: (
def go(mf):
mf(m.get('Right')) if None is m.get('Left') else m
) return (
mf(m.get('Right')) if None is m.get('Left') else m
)
return go
 
 
# compose (<<<) :: (b(a -> ca), ...) -> (a -> ba) -> a -> c
def compose(g*fs):
'''Composition, from right to left,
'''Right to left function composition.'''
of a series of functions.
return lambda f: lambda x: g(f(x))
'''
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):
'''ConcatenatedA concatenated list over which a function has been mapped.
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).'''
'''
return lambda xs: list(
def go(xs):
chain.from_iterable(
return chain.from_iterable(map(f, xs))
return )go
)
 
 
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:
 
 
# indentidentity :: Inta -> String -> Stringa
def indentidentity(nx):
'''StringThe indentedidentity by n multiplesfunction.'''
return x
of four spaces'''
return lambda s: (n * 4 * ' ') + s
 
 
# 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))
'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.'''
returndef lambda x: str(roundgo(x, n)):
return str(round(x, n))
return go
 
 
9,655

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.