예제 #1
0
파일: flrg.py 프로젝트: goldstar111/pyFTS
 def get_upper(self, t, window_size=1):
     if len(self.RHS) > 0:
         if isinstance(self.RHS, (list, set)):
             return max([r.get_upper(common.window_index(t, window_size)) for r in self.RHS])
         elif isinstance(self.RHS, dict):
             return max([self.RHS[r].get_upper(common.window_index(t, window_size)) for r in self.RHS.keys()])
     else:
         return self.LHS[-1].get_upper(common.window_index(t, window_size))
예제 #2
0
파일: flrg.py 프로젝트: goldstar111/pyFTS
 def get_midpoint(self, t, window_size=1):
     if len(self.RHS) > 0:
         if isinstance(self.RHS, (list, set)):
             tmp = [r.get_midpoint(common.window_index(t, window_size)) for r in self.RHS]
         elif isinstance(self.RHS, dict):
             tmp = [self.RHS[r].get_midpoint(common.window_index(t, window_size)) for r in self.RHS.keys()]
         return sum(tmp) / len(tmp)
     else:
         return self.LHS[-1].get_midpoint(common.window_index(t, window_size))
예제 #3
0
파일: flrg.py 프로젝트: goldstar111/pyFTS
    def get_membership(self, data, t, window_size=1):
        ret = 0.0
        if isinstance(self.LHS, (list, set)):
            #assert len(self.LHS) == len(data)

            ret = min([self.LHS[ct].membership(dat, common.window_index(t - (self.order - ct), window_size))
                       for ct, dat in enumerate(data)])
        else:
            ret = self.LHS.membership(data, common.window_index(t, window_size))
        return ret
예제 #4
0
파일: honsfts.py 프로젝트: bekti7/pmi
    def _affected_flrgs(self, sample, k, time_displacement, window_size):
        # print("input: " + str(ndata[k]))

        affected_flrgs = []
        affected_flrgs_memberships = []

        lags = {}

        for ct, dat in enumerate(sample):
            tdisp = common.window_index((k + time_displacement) - (self.order - ct), window_size)

            sel = [ct for ct, key in enumerate(self.partitioner.ordered_sets)
                   if self.sets[key].membership(dat, tdisp) > 0.0]

            if len(sel) == 0:
                sel.append(common.check_bounds_index(dat, self.partitioner, tdisp))

            lags[ct] = sel

        # Build the tree with all possible paths

        root = tree.FLRGTreeNode(None)

        tree.build_tree_without_order(root, lags, 0)

        # Trace the possible paths and build the PFLRG's

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

            for kk in path:
                flrg.append_lhs(self.sets[self.partitioner.ordered_sets[kk]])

            affected_flrgs.append(flrg)
            # affected_flrgs_memberships.append_rhs(flrg.get_membership(sample, disp))

            #                print(flrg.get_key())

            # the FLRG is here because of the bounds verification
            mv = []
            for ct, dat in enumerate(sample):
                td = common.window_index((k + time_displacement) - (self.order - ct), window_size)
                tmp = flrg.LHS[ct].membership(dat, td)


                mv.append(tmp)
            # print(mv)

            affected_flrgs_memberships.append(np.prod(mv))

        return [affected_flrgs, affected_flrgs_memberships]
예제 #5
0
파일: flrg.py 프로젝트: zilla-liu/pyFTS
 def get_membership(self, data, *args):
     sets, t, window_size = self.unpack_args(*args)
     ret = 0.0
     if isinstance(self.LHS, (list, set)):
         ret = min([
             sets[self.LHS[ct]].membership(
                 dat, common.window_index(t - (self.order - ct),
                                          window_size))
             for ct, dat in enumerate(data)
         ])
     else:
         ret = self.LHS.membership(data,
                                   common.window_index(t, window_size))
     return ret
예제 #6
0
파일: flrg.py 프로젝트: zilla-liu/pyFTS
 def get_upper(self, *args):
     sets, t, window_size = self.unpack_args(*args)
     if len(self.RHS) > 0:
         if isinstance(self.RHS, (list, set)):
             return max([
                 sets[r].get_upper(common.window_index(t, window_size))
                 for r in self.RHS
             ])
         elif isinstance(self.RHS, dict):
             return max([
                 sets[r].get_upper(common.window_index(t, window_size))
                 for r in self.RHS.keys()
             ])
     else:
         return sets[self.LHS[-1]].get_upper(
             common.window_index(t, window_size))
예제 #7
0
파일: flrg.py 프로젝트: zilla-liu/pyFTS
 def get_midpoint(self, *args):
     sets, t, window_size = self.unpack_args(*args)
     if len(self.RHS) > 0:
         if isinstance(self.RHS, (list, set)):
             tmp = [
                 sets[r].get_midpoint(common.window_index(t, window_size))
                 for r in self.RHS
             ]
         elif isinstance(self.RHS, dict):
             tmp = [
                 sets[r].get_midpoint(common.window_index(t, window_size))
                 for r in self.RHS.keys()
             ]
         return sum(tmp) / len(tmp)
     else:
         return sets[self.LHS[-1]].get_midpoint(
             common.window_index(t, window_size))
예제 #8
0
파일: honsfts.py 프로젝트: bekti7/pmi
    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)
예제 #9
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
예제 #10
0
파일: honsfts.py 프로젝트: bekti7/pmi
    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
예제 #11
0
파일: nsfts.py 프로젝트: goldstar111/pyFTS
    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
예제 #12
0
파일: honsfts.py 프로젝트: bekti7/pmi
    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
예제 #13
0
    def forecast(self, ndata, **kwargs):

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

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

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

        l = len(ndata)

        ret = []

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

            sample = ndata[k]

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

            affected_sets = self._affected_sets(sample, perturb)

            numerator = []
            denominator = []

            if len(affected_sets) == 1:
                ix = affected_sets[0][0]
                aset = self.partitioner.ordered_sets[ix]
                if aset in self.flrgs:
                    numerator.append(self.flrgs[aset].get_midpoint(
                        self.sets, perturb[ix]))
                else:
                    fuzzy_set = self.sets[aset]
                    numerator.append(fuzzy_set.get_midpoint(perturb[ix]))
                denominator.append(1)
            else:
                for aset in affected_sets:
                    ix = aset[0]
                    fs = self.partitioner.ordered_sets[ix]
                    tdisp = perturb[ix]
                    if fs in self.flrgs:
                        numerator.append(
                            self.flrgs[fs].get_midpoint(self.sets, tdisp) *
                            aset[1])
                    else:
                        fuzzy_set = self.sets[fs]
                        numerator.append(
                            fuzzy_set.get_midpoint(tdisp) * aset[1])
                    denominator.append(aset[1])

            if sum(denominator) > 0:
                pto = sum(numerator) / sum(denominator)
            else:
                pto = sum(numerator)

            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.append(sample)

                self.inputs.pop(0)
                self.forecasts.pop(0)
                self.residuals.pop(0)

        return ret
예제 #14
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