示例#1
0
 def gaussian_univariate_lc_y_dimension_test(self):
     """Test if the Normal LC DLM observation has the expected dimension.
     """
     lc = UnivariateStructure.locally_constant(1.0)
     dlm = NormalDLM(structure=lc, V=1.4)
     y = dlm.observation(np.array([0]))
     assert_true(np.isscalar(y), "Observation is not a scalar")
示例#2
0
 def univariate_lc_state_dimension_mismatch_test(self):
     """
     Test if a state of wrong dimensions throws an exception
     """
     lc = UnivariateStructure.locally_constant(1.0)
     dlm = NormalDLM(structure=lc, V=1.4)
     state0 = np.array([0, 0])
     dlm.observation(state0)
示例#3
0
    def composite_lcll_state_dimension_test(self):
        """Test if the composite LC/LL state has the expected dimensions."""
        lc = UnivariateStructure.locally_constant(1.0)
        ll = UnivariateStructure.locally_linear(np.eye(2))

        ndlm1 = NormalDLM(lc, 1.0)
        ndlm2 = NormalDLM(ll, 1.0)

        composite = CompositeDLM(ndlm1, ndlm2)

        m0 = np.array([0, 0, 0])
        state = composite.state(m0)
        assert_equals(state.shape, (3,), "state dimensions not correct")
示例#4
0
    def composite_lc_state_dimension_test(self):
        """Test if the composite LC state has the expected dimensions."""
        lc1 = UnivariateStructure.locally_constant(1.0)
        lc2 = UnivariateStructure.locally_constant(1.0)

        ndlm1 = NormalDLM(lc1, 1.0)
        ndlm2 = NormalDLM(lc2, 1.0)

        composite = CompositeDLM(ndlm1, ndlm2)

        m0 = np.array([0, 0])
        state = composite.state(m0)
        shape = state.shape
        assert_equals(shape, (2,), "state dimensions not correct")
示例#5
0
 def univariate_ll_prior_dimensions_test(self):
     """The default LL DLM state prior should have the correct dimensions
     """
     ll = UnivariateStructure.locally_linear(np.eye(2))
     dlm = NormalDLM(structure=ll, V=1.4)
     assert_equal(dlm.current_state.shape, (2, ),
                  "Prior has the wrong dimensions")
示例#6
0
 def univariate_lc_prior_dimensions_test(self):
     """The default LC DLM state prior should have the correct dimensions
     """
     lc = UnivariateStructure.locally_constant(1.0)
     dlm = NormalDLM(structure=lc, V=1.4)
     assert_equal(dlm.current_state.shape, (1, ),
                  "Prior has the wrong dimensions")
示例#7
0
    def composite_lcll_structure_dimension_test(self):
        """Test if the composite LC/LL structure has the expected dimensions."""
        lc = UnivariateStructure.locally_constant(1.0)
        ll = UnivariateStructure.locally_linear(np.eye(2))

        ndlm1 = NormalDLM(lc, 1.0)
        ndlm2 = NormalDLM(ll, 1.0)

        composite = CompositeDLM(ndlm1, ndlm2)

        shape = composite.structure.F.shape
        assert_equals(shape, (3, 2), "F dimensions not correct")
        shape = composite.structure.G.shape
        assert_equals(shape, (3, 3), "G dimensions not correct")
        shape = composite.structure.W.shape
        assert_equals(shape, (3, 3), "W dimensions not correct")
示例#8
0
    def composite_lc_structure_dimension_test(self):
        """Test if the composite LC structure has the expected dimensions."""
        lc1 = UnivariateStructure.locally_constant(1.0)
        lc2 = UnivariateStructure.locally_constant(1.0)

        ndlm1 = NormalDLM(lc1, 1.0)
        ndlm2 = NormalDLM(lc2, 1.0)

        composite = CompositeDLM(ndlm1, ndlm2)

        shape = composite.structure.F.shape
        assert_equals(shape, (2, 2), "F dimensions not correct")
        shape = composite.structure.G.shape
        assert_equals(shape, (2, 2), "G dimensions not correct")
        shape = composite.structure.W.shape
        assert_equals(shape, (2, 2), "W dimensions not correct")
示例#9
0
def main(args):
    logging.basicConfig(level=args.logging)
    logging.info('brokers={}'.format(args.brokers))
    logging.info('topic={}'.format(args.topic))
    logging.info('conf={}'.format(args.conf))

    if args.conf:
        model, state, period, name, anomalies = parse_configuration(
            _read_conf(args.conf))
    else:
        state = np.array([0])
        lc = UnivariateStructure.locally_constant(1.0)
        model = NormalDLM(structure=lc, V=1.4)
        period = 2.0
        name = 'data'
        anomalies = [lambda x: x]

    logging.info('creating kafka producer')
    producer = KafkaProducer(bootstrap_servers=args.brokers)

    logging.info('sending lines (frequency = {})'.format(period))
    while True:

        dimensions = np.size(state)

        if dimensions == 1:
            logging.debug("state = {}".format(state))
            _state = anomalies[0](state)
            logging.debug("anomaly = {}".format(_state))
        else:
            _state = np.copy(state)
            for i in range(dimensions):
                logging.debug("state {} = {}".format(i, state[i]))
                _state[i] = anomalies[i](state[i])
                logging.debug("anomaly {} = {}".format(i, state[i]))

        y = model.observation(_state)
        state = model.state(state)

        message = build_message(name, y)
        logging.info("message = {}".format(message))
        producer.send(args.topic, message)
        time.sleep(period)
示例#10
0
 def univariate_ll_iterator_dimensions_test(self):
     """The LL DLM iterator result should have the correct dimensions"""
     ll = UnivariateStructure.locally_linear(np.eye(2))
     dlm = NormalDLM(structure=ll, V=1.4)
     items = [next(dlm) for _ in range(10)]
     print(items)
     assert_equal(items[0][0].shape, (2, ),
                  "State has the wrong dimensions")
     assert_true(np.isscalar(items[0][1]),
                 "Observation has the wrong dimensions")
示例#11
0
    def setUpClass(cls):
        # Locally constant
        lc = {"structure": UnivariateStructure.locally_constant(1.0)}
        lc["model"] = NormalDLM(structure=lc["structure"], V=1.4)
        lc["nobs"] = 100
        # the initial state prior
        m0 = np.array([0])
        C0 = np.matrix([[1]])
        state0 = rand.multivariate_normal(m0, C0)

        lc["states"] = [state0]

        for t in range(1, lc["nobs"]):
            lc["states"].append(lc["model"].state(lc["states"][t - 1]))

        lc["obs"] = [None]
        for t in range(1, lc["nobs"]):
            lc["obs"].append(lc["model"].observation(lc["states"][t]))

        cls._lc = lc

        # Locally linear
        ll = {"structure": UnivariateStructure.locally_linear(np.identity(2))}
        ll["model"] = NormalDLM(structure=ll["structure"], V=1.4)
        ll["nobs"] = 100
        # the initial state prior
        m0 = np.array([0, 0])
        C0 = np.identity(2)
        state0 = rand.multivariate_normal(m0, C0)

        ll["states"] = [state0]

        for t in range(1, ll["nobs"]):
            ll["states"].append(ll["model"].state(ll["states"][t - 1]))

        ll["obs"] = [None]
        for t in range(1, ll["nobs"]):
            ll["obs"].append(ll["model"].observation(ll["states"][t]))

        cls._ll = ll
示例#12
0
def _parse_observations(obs, structure):
    if obs['type'] == 'continuous':
        model = NormalDLM(structure=structure, V=obs['noise'])
    elif obs['type'] == 'discrete':
        model = PoissonDLM(structure=structure)
    elif obs['type'] == 'categorical':
        if 'values' in obs:
            values = obs['values'].split(',')
            model = BinomialTransformer(structure=structure, source=values)
        elif 'categories' in obs:
            model = BinomialDLM(structure=structure,
                                categories=obs['categories'])
        else:
            raise ValueError("Categorical models must have either 'values' "
                             "or 'categories'")
    else:
        raise ValueError("Model type {} is not valid".format(obs['type']))
    return model