Commits

dan mackinlay committed c588538

lottery classes for the sim

Comments (0)

Files changed (1)

privateer_pyevolve/lottery.py

+#!/usr/bin/env python
+# encoding: utf-8
+"""
+market.py
+
+Created by dan mackinlay on 2010-09-24.
+Copyright (c) 2010 __MyCompanyName__. All rights reserved.
+"""
+
+from random import Random
+from math import sqrt
+DEFAULT_RNG = Random()
+
+
+class Lottery(dict):
+    """
+    A namespace into which to stash information abou this particular type of lottery. Lotteries are just dicts of fn variables
+    """
+    def __init__(self, *args, **kwargs):
+        super(Lottery, self).__init__(*args, **kwargs)
+    
+    def decide(self, *args, **kwargs):
+        raise NotImplementedError
+    
+    def std_dev(self, *args, **kwargs):
+        raise NotImplementedError
+    
+    def expectation(self, *args, **kwargs):
+        raise NotImplementedError
+        
+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']
+    
+    def std_dev(self, *args, **kwargs):
+        return (self['upper'] - self['lower'])/2.0
+
+    def expectation(self, *args, **kwargs):
+        return (self['lower'] + self['upper'])/2.0
+
+class UniformLottery(CoinToss):
+    """
+    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 rng.random()*(self['upper']-self['lower'])+self['lower']
+    
+    def std_dev(self, *args, **kwargs):
+        return (self['upper'] - self['lower'])/(2.0*sqrt(3))