def simulate(self, numSample: int = 1000, numPoint: int = 100) -> TimeseriesStatistic: """ Runs a simulation using the parameters from the bootstrap. Parameters ---------- numSample: number of fitted parameters to sample numPoint: number of points in the simulation Returns ------- TimeseriesStatistic """ params_list = self._sampleParams(numSample) fittedTS = self.fitter.simulate(params=params_list[0], numPoint=numPoint) timeseriesStatistic = TimeseriesStatistic(fittedTS, percentiles=PERCENTILES) timeseriesStatistic.accumulate(fittedTS) # Do the simulation for params in params_list[1:]: fittedTS = self.fitter.simulate(params=params, numPoint=numPoint) timeseriesStatistic.accumulate(fittedTS) timeseriesStatistic.calculate() return timeseriesStatistic
class TestNamedTimeseries(unittest.TestCase): def setUp(self): self.timeseries = SIMPLE_TS self.statistic = TimeseriesStatistic(self.timeseries) def testConstructor(self): if IGNORE_TEST: return colnames = list(COLNAMES) colnames.remove(TIME) diff = set(self.statistic.colnames).symmetric_difference(colnames) self.assertEqual(len(diff), 0) def testAccumulate(self): if IGNORE_TEST: return self.statistic.accumulate(SIMPLE_TS) self.assertTrue(self.statistic.sumTS.equals(SIMPLE_TS)) def testCalculate1(self): if IGNORE_TEST: return for _ in range(SIMPLE_CNT): self.statistic.accumulate(SIMPLE_TS) self.statistic.calculate() self.assertEqual(self.statistic.count, SIMPLE_CNT) self.assertTrue(self.statistic.meanTS.equals(SIMPLE_TS)) stdTS = SIMPLE_TS.copy(isInitialize=True) self.assertTrue(self.statistic.stdTS.equals(stdTS)) def mkStatistics(self, count): result = [] for _ in range(count): statistic = TimeseriesStatistic(UNIFORM_TS) for _ in range(UNIFORM_CNT): statistic.accumulate( mkTimeseries(UNIFORM_LEN, COLNAMES, isRandom=True)) result.append(statistic) return result def evaluateStatistic(self, statistic, count=1): statistic.calculate() self.assertEqual(statistic.count, count * UNIFORM_CNT) mean = np.mean(statistic.meanTS.flatten()) self.assertLess(np.abs(mean - UNIFORM_MEAN), 0.1) std = np.mean(statistic.stdTS.flatten()) self.assertLess(np.abs(std - UNIFORM_STD), 0.1) for percentile in self.statistic.percentiles: value = np.mean(statistic.percentileDct[percentile].flatten()) self.assertLess(np.abs(value - 0.01 * percentile), 0.01) def testCalculate2(self): if IGNORE_TEST: return statistic = self.mkStatistics(1)[0] self.evaluateStatistic(statistic) def testEquals(self): if IGNORE_TEST: return statistic = TimeseriesStatistic(self.timeseries) self.assertTrue(self.statistic.equals(statistic)) # statistic.accumulate(SIMPLE_TS) self.assertFalse(self.statistic.equals(statistic)) def testCopy(self): if IGNORE_TEST: return statistic = self.statistic.copy() self.assertTrue(self.statistic.equals(statistic)) # statistic = self.mkStatistics(1)[0] self.assertTrue(statistic.equals(statistic)) def testMerge(self): if IGNORE_TEST: return NUM = 4 statistics = self.mkStatistics(NUM) statistic = TimeseriesStatistic.merge(statistics) statistic.calculate() self.evaluateStatistic(statistic, count=NUM) def testRpickleInterface(self): if IGNORE_TEST: return serialization = rpickle.Serialization(self.statistic) statistic = serialization.deserialize() self.assertTrue(statistic.meanTS.equals(self.statistic.meanTS))