def __init__(self, a, b, beginning=None, ending=None, beginning_factor=None, ending_factor=None): """ start and end can be in either datetime or unix time """ a, b = UnixTime(a), UnixTime(b) assert a < b, "'b' should be greater than 'a'" if (beginning, ending) != (None, None): assert (beginning_factor, ending_factor) == (None, None), "PiecewiseTemporalEvent() only accepts " \ "either 'beginning_factor' and 'ending_factor' " \ "or 'beginning' and 'ending'" if beginning_factor is not None: assert beginning_factor > 0 self.beginning_factor = beginning_factor if ending_factor is not None: assert ending_factor > 0 self.ending_factor = ending_factor if (beginning, ending) != (None, None): beginning = UnixTime(beginning) ending = UnixTime(ending) else: beginning, ending = 0, 0 while not a < beginning < ending < b: beginning = random_time( a, b, probability_distribution=t( # df, mean, variance 4, a + float(b - a) / self.beginning_factor, float(b - a) / self.beginning_factor)) ending = random_time( a, b, probability_distribution=t( # df, mean, variance 4, b - float(b - a) / self.ending_factor, float(b - a) / self.ending_factor)) TemporalEventPiecewiseLinear.__init__(self, { a: 0, beginning: 1 }, { ending: 1, b: 0 })
def __init__(self, a, b, beginning=None, ending=None, beginning_factor=None, ending_factor=None): """ start and end can be in either datetime or unix time """ a, b = UnixTime(a), UnixTime(b) assert a < b, "'b' should be greater than 'a'" if (beginning, ending) != (None, None): assert (beginning_factor, ending_factor) == (None, None), "PiecewiseTemporalEvent() only accepts " \ "either 'beginning_factor' and 'ending_factor' " \ "or 'beginning' and 'ending'" if not a < beginning < ending < b: raise AttributeError( "The inputs should satisfy 'a < beginning < ending < b' relation" ) if beginning_factor is not None: assert beginning_factor > 0 self.beginning_factor = beginning_factor if ending_factor is not None: assert ending_factor > 0 self.ending_factor = ending_factor if (beginning, ending) == (None, None): beginning, ending = 0, 0 while not a < beginning < ending < b: beginning = random_time( a, b, probability_distribution=t( # df, mean, variance 4, a + float(b - a) / self.beginning_factor, float(b - a) / self.beginning_factor)) ending = random_time( a, b, probability_distribution=t( # df, mean, variance 4, b - float(b - a) / self.ending_factor, float(b - a) / self.ending_factor)) TemporalEvent.__init__(self, uniform(loc=a, scale=UnixTime(beginning - a)), uniform(loc=ending, scale=UnixTime(b - ending)), bins=4)
def __init__(self, a, b, beginning=None, ending=None, beginning_factor=None, ending_factor=None): """ start and end can be in either datetime or unix time """ a, b = UnixTime(a), UnixTime(b) assert a < b, "'b' should be greater than 'a'" if (beginning, ending) != (None, None): assert (beginning_factor, ending_factor) == (None, None), "PiecewiseTemporalEvent() only accepts " \ "either 'beginning_factor' and 'ending_factor' " \ "or 'beginning' and 'ending'" if not a < beginning and ending < b and (beginning < ending or almost_equals(beginning, ending)): raise AttributeError("The inputs should satisfy 'a < beginning <= ending < b' relation") if beginning_factor is not None: assert beginning_factor > 0 self.beginning_factor = beginning_factor if ending_factor is not None: assert ending_factor > 0 self.ending_factor = ending_factor if (beginning, ending) == (None, None): beginning, ending = 0, 0 while not a < beginning < ending < b: beginning = random_time( a, b, probability_distribution=t( # df, mean, variance 4, a + float(b - a) / self.beginning_factor, float(b - a) / self.beginning_factor ) ) ending = random_time( a, b, probability_distribution=t( # df, mean, variance 4, b - float(b - a) / self.ending_factor, float(b - a) / self.ending_factor ) ) TemporalEvent.__init__(self, uniform(loc=a, scale=UnixTime(beginning - a)), uniform(loc=ending, scale=UnixTime(b - ending)), bins=4)
def __init__(self, a, b, beginning=None, ending=None, beginning_factor=None, ending_factor=None): """ start and end can be in either datetime or unix time """ a, b = UnixTime(a), UnixTime(b) assert a < b, "'b' should be greater than 'a'" if (beginning, ending) != (None, None): assert (beginning_factor, ending_factor) == (None, None), "PiecewiseTemporalEvent() only accepts " \ "either 'beginning_factor' and 'ending_factor' " \ "or 'beginning' and 'ending'" if beginning_factor is not None: assert beginning_factor > 0 self.beginning_factor = beginning_factor if ending_factor is not None: assert ending_factor > 0 self.ending_factor = ending_factor if (beginning, ending) != (None, None): beginning = UnixTime(beginning) ending = UnixTime(ending) else: beginning, ending = 0, 0 while not a < beginning < ending < b: beginning = random_time( a, b, probability_distribution=t( # df, mean, variance 4, a + float(b - a) / self.beginning_factor, float(b - a) / self.beginning_factor ) ) ending = random_time( a, b, probability_distribution=t( # df, mean, variance 4, b - float(b - a) / self.ending_factor, float(b - a) / self.ending_factor ) ) TemporalEventPiecewiseLinear.__init__(self, [a, beginning, ending, b], [0, 1, 1, 0])
def random_time(self, start=None, stop=None, probability_distribution=None): if start is None: start = self.a else: start = UnixTime(start) assert self.a <= start <= self.b, "'start' should be within the interval of 'self'" if stop is None: stop = self.b else: stop = UnixTime(stop) assert self.a <= stop <= self.b, "'stop' should be within the interval of 'self'" return random_time(start, stop, probability_distribution)
def random_time(self, start=None, stop=None, probability_distribution=None): if start is None: start = self.a else: start = UnixTime(start) assert self.a <= start <= self.b, "'start' should be within the interval of 'self'" if stop is None: stop = self.b else: stop = UnixTime(stop) assert self.a <= stop <= self.b, "'stop' should be within the interval of 'self'" return random_time(start, stop, probability_distribution)
def rvs(self, size=None): if size is None: size = 1 else: assert isinstance(size, int) result = [] start, end = 0, 0 for i in xrange(size): rand = random() for a, b, share in self.roulette_wheel: if rand < share: start, end = a, b break result.append(random_time(start, end)) if size == 1: return result[0] return result
def rvs(self, size=None): if size is None: size = 1 else: assert isinstance(size, int) result = [] start, end = 0, 0 for i in xrange(size): rand = random() for a, b, share in self.roulette_wheel: if rand < share: start, end = a, b break result.append(random_time(start, end)) if size == 1: return result[0] return result