Exemple #1
0
    def forecast(self, ndata, **kwargs):

        ret = []

        l = len(ndata)

        if l <= self.max_lag:
            return ndata

        for k in np.arange(self.max_lag, l + 1):
            flrgs = self.generate_lhs_flrg(ndata[k - self.max_lag:k])

            tmp = []
            for flrg in flrgs:

                if flrg.get_key() not in self.flrgs:
                    if len(flrg.LHS) > 0:
                        tmp.append(self.sets[flrg.LHS[-1]].centroid)
                else:
                    flrg = self.flrgs[flrg.get_key()]
                    tmp.append(flrg.get_midpoint(self.sets))

            ret.append(np.nanmean(tmp))

        return ret
Exemple #2
0
    def forecast(self, data, **kwargs):

        ret = []
        main_factor = data.ix[:, 0].values
        main_key = data.columns[0]
        main_fs = list(self.fuzzySetsDict[main_key].values())

        l = len(main_factor)

        if l <= self.order:
            return data

#       ndata = self.doTransformations(data)

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

            lags = []
            print("Forecasting: instance " + str(k) + " of " + str(l))

            for o in range(k - self.order, k):
                lhs = []
                lhs.append([
                    set for set in main_fs
                    if set.membership(main_factor[o]) > 0.0
                ])
                #lhs.append([FuzzySet.getMaxMembershipFuzzySet(main_factor[o], main_fs)])

                for c in range(1, len(data.columns)):
                    sec_key = data.columns[c]
                    sec_factor = data.ix[:, sec_key].values
                    sec_fs = list(self.fuzzySetsDict[sec_key].values())
                    lhs.append([
                        set for set in sec_fs
                        if set.membership(sec_factor[o]) > 0.0
                    ])
                    #lhs.append([FuzzySet.getMaxMembershipFuzzySet(sec_factor[o], sec_fs)])

                lags.append(lhs)

            tmpflrg = MultivariateHighOrderFLRG(self.order)
            tmpflrg.LHS = lags

            if tmpflrg.strLHS() not in self.flrgs:
                ret.append(tmpflrg.LHS[-1][0][0].centroid)
            else:
                flrg = self.flrgs[tmpflrg.strLHS()]
                ret.append(flrg.get_midpoint())

        #ret = self.doInverseTransformations(ret, params=[data[self.order-1:]])

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

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

        ret = []

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

        if l < self.max_lag:
            return ndata

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

            if explain:
                print("Fuzzyfication \n")

            if not kwargs.get('fuzzyfied', False):
                flrgs = self.generate_lhs_flrg(ndata[k - self.max_lag: k], explain)
            else:
                flrgs = self.generate_lhs_flrg_fuzzyfied(ndata[k - self.max_lag: k], explain)

            if explain:
                print("Rules:\n")

            tmp = []
            for flrg in flrgs:

                if flrg.get_key() not in self.flrgs:
                    if len(flrg.LHS) > 0:
                        mp = self.partitioner.sets[flrg.LHS[-1]].centroid
                        tmp.append(mp)

                        if explain:
                            print("\t {} -> {} (Naïve)\t Midpoint: {}\n".format(str(flrg.LHS), flrg.LHS[-1],
                                                                                            mp))
                else:
                    flrg = self.flrgs[flrg.get_key()]
                    mp = flrg.get_midpoint(self.partitioner.sets)
                    tmp.append(mp)

                    if explain:
                        print("\t {} \t Midpoint: {}\n".format(str(flrg), mp))

            final = np.nanmean(tmp)
            ret.append(final)

            if explain:
                print("Deffuzyfied value: {} \n".format(final))

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

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

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

        mode = kwargs.get('mode', 'mean')

        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 not fuzzyfied:
                flrgs = self.generate_lhs_flrg(sample, explain)
            else:
                flrgs = self.generate_lhs_flrg_fuzzyfied(sample, explain)

            midpoints = []
            memberships = []
            for flrg in flrgs:

                if flrg.get_key() not in self.flrgs:
                    if len(flrg.LHS) > 0:
                        mp = self.partitioner.sets[flrg.LHS[-1]].centroid
                        mv = self.partitioner.sets[flrg.LHS[-1]].membership(
                            sample[-1]) if not fuzzyfied else None
                        midpoints.append(mp)
                        memberships.append(mv)

                        if explain:
                            self.append_log(
                                "Rule Matching",
                                "{} -> {} (Naïve) Midpoint: {}".format(
                                    str(flrg.LHS), flrg.LHS[-1], mp))
                else:
                    flrg = self.flrgs[flrg.get_key()]
                    mp = flrg.get_midpoint(self.partitioner.sets)
                    mv = flrg.get_membership(
                        sample,
                        self.partitioner.sets) if not fuzzyfied else None
                    midpoints.append(mp)
                    memberships.append(mv)

                    if explain:
                        self.append_log(
                            "Rule Matching",
                            "{}, Midpoint: {} Membership: {}".format(
                                flrg.get_key(), mp, mv))

            if mode == "mean" or fuzzyfied:
                final = np.nanmean(midpoints)
                if explain:
                    self.append_log("Deffuzyfication",
                                    "By Mean: {}".format(final))
            else:
                final = np.dot(midpoints, memberships) / np.nansum(memberships)
                if explain:
                    self.append_log("Deffuzyfication",
                                    "By Memberships: {}".format(final))

            ret.append(final)

        return ret