Commits

dan mackinlay committed f249c3f

Weighted coins in there

  • Participants
  • Parent commits b548b04

Comments (0)

Files changed (1)

File privateer_pyevolve/lottery.py

 
 class Lottery(dict):
     """
-    A namespace into which to stash information abou this particular type of lottery. Lotteries are just dicts of fn variables
+    A namespace into which to stash information about this particular type of lottery. Lotteries are just dicts of fn variables
     """
     def __init__(self, *args, **kwargs):
         super(Lottery, self).__init__(*args, **kwargs)
     
     def expectation(self, *args, **kwargs):
         raise NotImplementedError
+
+class WeightedCoinToss(Lottery):
+    """
+    toss a biased coin and return either the lower or upper bound
+    should have keys of 'upper', 'lower' and 0<'lowerweight'<1
+    """
         
-class CoinToss(Lottery):
-    """
-    toss a coin and return either the lower or upper bound
-    should have keys.upper and keys.lower
-    """
     def decide(self, *args, **kwargs):
         rng = self.get('rng', DEFAULT_RNG)
-        return self['lower'] if rng.random()< 0.5 else self['upper']
+        return self['lower'] if rng.random()<self['lowerweight'] else self['upper']
     
-    def std_dev(self, *args, **kwargs):
-        return (self['upper'] - self['lower'])/2.0
+    def expectation(self, *args, **kwargs):
+        return (self['lower']*self['lowerweight'] +
+           self['upper']*(1-self['lowerweight']))
 
-    def expectation(self, *args, **kwargs):
-        return (self['lower'] + self['upper'])/2.0
-
-class UniformLottery(CoinToss):
+class UniformLottery(Lottery):
     """
     toss a coin and return either the lower or upper bound
     should have keys.upper and keys.lower
     
     def std_dev(self, *args, **kwargs):
         return (self['upper'] - self['lower'])/(2.0*sqrt(3))
+    
+    def expectation(self, *args, **kwargs):
+        return (self['lower']+self['lower'])*0.5
 
+