Skip to content

Commit 7456701

Browse files
author
nate
committed
Simplification of getProb and solomonoffInduction
getProb now takes Bit first. This allows solomonoffInduction to be a oneliner.
1 parent 9a93512 commit 7456701

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

OracleMachines.hs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ flipR r = do
205205
-- Finds the probability that a machine, run on a given input, outputs a given bit.
206206
-- Basically does binary refinement using the oracle.
207207
-- Generates a series of nested intervals.
208-
getProb :: POM m => [Bit] -> Machine -> Bit -> Real m
209-
getProb bits m bit = if bit == One then prob1 else oneMinus prob1 where
208+
getProb :: POM m => Bit -> [Bit] -> Machine -> Real m
209+
getProb bit bits m = if bit == One then prob1 else oneMinus prob1 where
210210
prob1 = makeStream restrictInterval (pure (1, 0))
211211
restrictInterval pbounds = do
212212
(hi, lo) <- pbounds
@@ -216,7 +216,7 @@ getProb bits m bit = if bit == One then prob1 else oneMinus prob1 where
216216

217217
-- Finds the probability that a machine would have output a given bit sequence.
218218
getStringProb :: POM m => [Bit] -> Machine -> Real m
219-
getStringProb bits m = realProduct [getProb prev m bit | (prev, bit) <- events bits]
219+
getStringProb bits m = realProduct [getProb bit prev m | (prev, bit) <- events bits]
220220

221221
-- Given a measure of how likely each machine is to accept x (in some abstract
222222
-- fashion) and x, this function generates the generic probability (over all
@@ -255,8 +255,7 @@ sampleMachine bias = do
255255
-- predicts the behavior of each machine in proportion to its posterior
256256
-- probability given the bits seen so far.
257257
solomonoffInduction :: POM m => [Bit] -> m Bit
258-
solomonoffInduction bs = pick >>= \m -> flipR (getProb bs m One) where
259-
pick = sampleMachine $ getStringProb bs
258+
solomonoffInduction bs = flipR . getProb One bs =<< sampleMachine (getStringProb bs)
260259

261260
-- Actions and Observations are bitstrings.
262261
-- You must use a prefix-free encoding.
@@ -329,7 +328,7 @@ events xs = [(take n xs, xs !! n) | n <- [0 .. pred $ length xs]]
329328
-- Given a machine and a list of OA pairs, compute the probability that that
330329
-- machine would have produced those observations (given those actions).
331330
getHistProb :: POM m => History -> Machine -> Real m
332-
getHistProb rhist m = realProduct [getProb bs m b | (bs, b) <- bitEvents] where
331+
getHistProb rhist m = realProduct [getProb b bs m | (bs, b) <- bitEvents] where
333332
obsEvents = second fst <$> events (reverse rhist)
334333
bitEvents = concatMap (uncurry o2b) obsEvents
335334
o2b h o = first (histStr h ++) <$> events o
@@ -347,14 +346,14 @@ envInductor :: POM m => EnvHistory -> m (Machine, Bit)
347346
envInductor (EnvHistory bits mm hist) = getM >>= \m -> (m,) <$> predict m where
348347
-- Note that if we need to sample a new machine, bits must be [].
349348
getM = maybe (sampleMachine $ getHistProb hist) return mm
350-
predict m = flipR (getProb (histStr hist ++ bits) m One)
349+
predict = flipR . getProb One (histStr hist ++ bits)
351350

352351
-- Runs the interaction of an agent with an environment, starting with an
353352
-- environment (which may be partway through outputting an observation).
354353
interactE :: POM m => Agent m -> Environment m -> EnvHistory -> m (Stream OA)
355354
interactE agent env hist = if isObservation $ partialO hist then a else e where
356355
a = interactA agent env newAhist
357-
e = env hist >>= (interactE agent env . newEhist)
356+
e = env hist >>= interactE agent env . newEhist
358357
newAhist = AgentHistory [] (partialO hist) (prevHistE hist)
359358
newEhist (m, bit) = hist{partialO = bit : partialO hist, currentM = Just m}
360359

@@ -363,7 +362,7 @@ interactE agent env hist = if isObservation $ partialO hist then a else e where
363362
interactA :: POM m => Agent m -> Environment m -> AgentHistory -> m (Stream OA)
364363
interactA agent env hist = if isAction $ partialA hist then e else a where
365364
e = ((currentO hist, partialA hist):!) <$> interactE agent env newEhist
366-
a = agent hist >>= (interactA agent env . newAhist)
365+
a = agent hist >>= interactA agent env . newAhist
367366
newEhist = EnvHistory [] Nothing $ (currentO hist, partialA hist) : prevHistA hist
368367
newAhist bit = hist{partialA = bit : partialA hist}
369368

0 commit comments

Comments
 (0)