Exemple #1
0
    def generate_flrg(self, data, **kwargs):
        l = len(data)
        for k in np.arange(self.max_lag, l):
            if self.dump: print("FLR: " + str(k))

            sample = data[k - self.max_lag:k]

            rhs = [
                key for key in self.partitioner.ordered_sets
                if self.partitioner.sets[key].membership(data[k], [0, 1]) > 0.0
            ]

            if len(rhs) == 0:
                rhs = [
                    common.check_bounds(data[k], self.partitioner, [0, 1]).name
                ]

            lags = []

            for o in np.arange(0, self.order):
                tdisp = [0, 1]
                lhs = [
                    key for key in self.partitioner.ordered_sets
                    if self.partitioner.sets[key].membership(sample[o], tdisp)
                    > 0.0
                ]

                if len(lhs) == 0:
                    lhs = [
                        common.check_bounds(sample[o], self.partitioner,
                                            tdisp).name
                    ]

                lags.append(lhs)

            # Trace the possible paths
            for path in product(*lags):
                flrg = HighOrderNonStationaryFLRG(self.order)

                for c, e in enumerate(path, start=0):
                    flrg.append_lhs(e)

                if flrg.get_key() not in self.flrgs:
                    self.flrgs[flrg.get_key()] = flrg

                for st in rhs:
                    self.flrgs[flrg.get_key()].append_rhs(st)
Exemple #2
0
    def generate_flrg(self, data, **kwargs):
        l = len(data)
        window_size = kwargs.get("window_size", 1)
        for k in np.arange(self.order, l):
            if self.dump: print("FLR: " + str(k))

            sample = data[k - self.order: k]

            disp = common.window_index(k, window_size)

            rhs = [self.sets[key] for key in self.partitioner.ordered_sets
                   if self.sets[key].membership(data[k], disp) > 0.0]

            if len(rhs) == 0:
                rhs = [common.check_bounds(data[k], self.partitioner, disp)]

            lags = {}

            for o in np.arange(0, self.order):
                tdisp = common.window_index(k - (self.order - o), window_size)
                lhs = [self.sets[key] for key in self.partitioner.ordered_sets
                   if self.sets[key].membership(sample[o], tdisp) > 0.0]

                if len(lhs) == 0:
                    lhs = [common.check_bounds(sample[o], self.partitioner, tdisp)]

                lags[o] = lhs

            root = tree.FLRGTreeNode(None)

            tree.build_tree_without_order(root, lags, 0)

            # Trace the possible paths
            for p in root.paths():
                flrg = HighOrderNonStationaryFLRG(self.order)
                path = list(reversed(list(filter(None.__ne__, p))))

                for c, e in enumerate(path, start=0):
                    flrg.append_lhs(e)

                if flrg.get_key() not in self.flrgs:
                    self.flrgs[flrg.get_key()] = flrg;

                for st in rhs:
                    self.flrgs[flrg.get_key()].append_rhs(st)
Exemple #3
0
    def forecast_interval(self, ndata, **kwargs):

        time_displacement = kwargs.get("time_displacement", 0)

        window_size = kwargs.get("window_size", 1)

        l = len(ndata)

        ret = []

        for k in np.arange(0, l):

            # print("input: " + str(ndata[k]))

            tdisp = common.window_index(k + time_displacement, window_size)

            affected_sets = [
                [self.sets[key], self.sets[key].membership(ndata[k], tdisp)]
                for key in self.partitioner.ordered_sets
                if self.sets[key].membership(ndata[k], tdisp) > 0.0
            ]

            if len(affected_sets) == 0:
                affected_sets.append([
                    common.check_bounds(ndata[k], self.partitioner, tdisp), 1.0
                ])

            upper = []
            lower = []

            if len(affected_sets) == 1:
                aset = affected_sets[0][0]
                if aset.name in self.flrgs:
                    lower.append(self.flrgs[aset.name].get_lower(tdisp))
                    upper.append(self.flrgs[aset.name].get_upper(tdisp))
                else:
                    lower.append(aset.get_lower(tdisp))
                    upper.append(aset.get_upper(tdisp))
            else:
                for aset in affected_sets:
                    if aset[0].name in self.flrgs:
                        lower.append(
                            self.flrgs[aset[0].name].get_lower(tdisp) *
                            aset[1])
                        upper.append(
                            self.flrgs[aset[0].name].get_upper(tdisp) *
                            aset[1])
                    else:
                        lower.append(aset[0].get_lower(tdisp) * aset[1])
                        upper.append(aset[0].get_upper(tdisp) * aset[1])

            ret.append([sum(lower), sum(upper)])

        return ret
Exemple #4
0
    def forecast_interval(self, ndata, **kwargs):

        time_displacement = kwargs.get("time_displacement", 0)

        window_size = kwargs.get("window_size", 1)

        l = len(ndata)

        ret = []

        for k in np.arange(self.order, l + 1):

            sample = ndata[k - self.order: k]

            affected_flrgs, affected_flrgs_memberships = self._affected_flrgs(sample, k,
                                                                              time_displacement, window_size)

            # print([str(k) for k in affected_flrgs])
            # print(affected_flrgs_memberships)

            upper = []
            lower = []

            tdisp = common.window_index(k + time_displacement, window_size)
            if len(affected_flrgs) == 0:
                aset = common.check_bounds(sample[-1], self.sets, tdisp)
                lower.append(aset.get_lower(tdisp))
                upper.append(aset.get_upper(tdisp))
            elif len(affected_flrgs) == 1:
                _flrg = affected_flrgs[0]
                if _flrg.get_key() in self.flrgs:
                    lower.append(self.flrgs[_flrg.get_key()].get_lower(tdisp))
                    upper.append(self.flrgs[_flrg.get_key()].get_upper(tdisp))
                else:
                    lower.append(_flrg.LHS[-1].get_lower(tdisp))
                    upper.append(_flrg.LHS[-1].get_upper(tdisp))
            else:
                for ct, aset in enumerate(affected_flrgs):
                    if aset.get_key() in self.flrgs:
                        lower.append(self.flrgs[aset.get_key()].get_lower(tdisp) *
                                     affected_flrgs_memberships[ct])
                        upper.append(self.flrgs[aset.get_key()].get_upper(tdisp) *
                                     affected_flrgs_memberships[ct])
                    else:
                        lower.append(aset.LHS[-1].get_lower(tdisp) *
                                   affected_flrgs_memberships[ct])
                        upper.append(aset.LHS[-1].get_upper(tdisp) *
                                   affected_flrgs_memberships[ct])

            ret.append([sum(lower), sum(upper)])


        return ret
Exemple #5
0
    def forecast(self, ndata, **kwargs):

        time_displacement = kwargs.get("time_displacement", 0)

        window_size = kwargs.get("window_size", 1)

        l = len(ndata)

        ret = []

        for k in np.arange(0, l):

            tdisp = common.window_index(k + time_displacement, window_size)

            affected_sets = [
                [self.sets[key], self.sets[key].membership(ndata[k], tdisp)]
                for key in self.partitioner.ordered_sets
                if self.sets[key].membership(ndata[k], tdisp) > 0.0
            ]

            if len(affected_sets) == 0:
                affected_sets.append([
                    common.check_bounds(ndata[k], self.partitioner, tdisp), 1.0
                ])

            tmp = []

            if len(affected_sets) == 1:
                aset = affected_sets[0][0]
                if aset.name in self.flrgs:
                    tmp.append(self.flrgs[aset.name].get_midpoint(tdisp))
                else:
                    tmp.append(aset.get_midpoint(tdisp))
            else:
                for aset in affected_sets:
                    if aset[0].name in self.flrgs:
                        tmp.append(
                            self.flrgs[aset[0].name].get_midpoint(tdisp) *
                            aset[1])
                    else:
                        tmp.append(aset[0].get_midpoint(tdisp) * aset[1])

            pto = sum(tmp)

            #print(pto)

            ret.append(pto)

        return ret
Exemple #6
0
    def forecast(self, ndata, **kwargs):

        time_displacement = kwargs.get("time_displacement",0)

        window_size = kwargs.get("window_size", 1)

        l = len(ndata)

        ret = []

        for k in np.arange(self.order, l+1):

            sample = ndata[k - self.order: k]

            affected_flrgs, affected_flrgs_memberships = self._affected_flrgs(sample, k,
                                                                              time_displacement, window_size)

            #print([str(k) for k in affected_flrgs])
            #print(affected_flrgs_memberships)

            tmp = []
            tdisp = common.window_index(k + time_displacement, window_size)
            if len(affected_flrgs) == 0:
                tmp.append(common.check_bounds(sample[-1], self.sets, tdisp))
            elif len(affected_flrgs) == 1:
                flrg = affected_flrgs[0]
                if flrg.get_key() in self.flrgs:
                    tmp.append(self.flrgs[flrg.get_key()].get_midpoint(tdisp))
                else:
                    tmp.append(flrg.LHS[-1].get_midpoint(tdisp))
            else:
                for ct, aset in enumerate(affected_flrgs):
                    if aset.get_key() in self.flrgs:
                        tmp.append(self.flrgs[aset.get_key()].get_midpoint(tdisp) *
                                   affected_flrgs_memberships[ct])
                    else:
                        tmp.append(aset.LHS[-1].get_midpoint(tdisp)*
                                   affected_flrgs_memberships[ct])
            pto = sum(tmp)

            #print(pto)

            ret.append(pto)

        return ret
Exemple #7
0
    def forecast(self, ndata, **kwargs):

        explain = kwargs.get('explain', False)

        time_displacement = kwargs.get("time_displacement", 0)

        window_size = kwargs.get("window_size", 1)

        no_update = kwargs.get("no_update", False)

        ret = []

        l = len(ndata) if not explain else self.max_lag + 1

        if l < self.max_lag:
            return ndata
        elif l == self.max_lag:
            l += 1

        for k in np.arange(self.max_lag, l):

            sample = ndata[k - self.max_lag:k]

            if self.method == 'unconditional':
                perturb = common.window_index(k + time_displacement,
                                              window_size)
            elif self.method == 'conditional':
                if no_update:
                    perturb = [[0, 1]
                               for k in np.arange(self.partitioner.partitions)]
                else:
                    perturb = self.conditional_perturbation_factors(sample[-1])

            affected_flrgs, affected_flrgs_memberships = self._affected_flrgs(
                sample, perturb)

            tmp = []

            perturb2 = perturb[0]
            if len(affected_flrgs) == 0:
                tmp.append(
                    common.check_bounds(sample[-1], self.partitioner.sets,
                                        perturb2))
            elif len(affected_flrgs) == 1:
                flrg = affected_flrgs[0]
                if flrg.get_key() in self.flrgs:
                    tmp.append(self.flrgs[flrg.get_key()].get_midpoint(
                        self.partitioner.sets, perturb2))
                else:
                    fset = self.partitioner.sets[flrg.LHS[-1]]
                    ix = self.partitioner.ordered_sets.index(flrg.LHS[-1])
                    tmp.append(fset.get_midpoint(perturb[ix]))
            else:
                for ct, aset in enumerate(affected_flrgs):
                    if aset.get_key() in self.flrgs:

                        tmp.append(self.flrgs[aset.get_key()].get_midpoint(
                            self.partitioner.sets, perturb2) *
                                   affected_flrgs_memberships[ct])
                    else:
                        fset = self.partitioner.sets[aset.LHS[-1]]
                        ix = self.partitioner.ordered_sets.index(aset.LHS[-1])
                        tmp.append(
                            fset.get_midpoint(perturb[ix]) *
                            affected_flrgs_memberships[ct])
            pto = sum(tmp)

            ret.append(pto)

            if self.method == 'conditional' and not no_update:
                self.forecasts.append(pto)
                self.residuals.append(self.inputs[-1] - self.forecasts[-1])
                self.inputs.extend(sample)

                for g in range(self.order):
                    self.inputs.pop(0)
                self.forecasts.pop(0)
                self.residuals.pop(0)

        return ret