Skip to main content

ferrifuzz.fts

Chen (1996) fuzzy time series: partition the value range into fuzzy sets, learn which set follows which, forecast by averaging. First order (FOFTS) and high order (HOFTS). The method is explained step by step in Fuzzy time series; the enrollment forecasting tutorial is a full worked example.

from ferrifuzz.fts import FuzzyTimeSeries

FuzzyTimeSeries​

FuzzyTimeSeries(
n_partitions=7,
order=1,
mf_type="triangle",
partitioner="equal_width",
margin=0.1,
universe=None,
)
ParameterDefaultDescription
n_partitions7number of fuzzy sets A1…AnA_1 … A_n over the universe; must be ≥ 2
order1how many past steps form a rule antecedent. 1 = first order, ≥ 2 = high order; must be ≥ 1
mf_type"triangle"partition shape: "triangle" or "gaussian"; anything else raises ValueError
partitioner"equal_width"partitioning strategy; "equal_width" is the only one implemented
margin0.1when universe is not given: fraction of the data range added below the minimum and above the maximum
universeNoneexplicit (lo, hi) universe of discourse; overrides the data range and margin

Invalid arguments (n_partitions < 2, order < 1, an unknown mf_type or partitioner, a universe with lo ≥ hi) raise ValueError.

Because every value is assigned to its highest‑membership partition and both shapes peak at the same centers, mf_type does not change the forecasts; it only changes how the partitions look when plotted.

Partitions are equal width: centers ci=lo+(i−1) stepc_i = lo + (i-1)\,\text{step} with step=(hi−lo)/(n−1)\text{step} = (hi - lo)/(n - 1). Triangles reach from one neighbour's center to the next; Gaussians use σ=step/2\sigma = \text{step}/2. Labels are A1 … An, low to high.

fit(series)​

Learns the universe, the partitions and the fuzzy logical relationship groups (FLRGs). Returns self, so it chains. The series must be longer than order; a NaN or infinite value in the training series raises ValueError (fill or drop missing values first).

DATA = [13055, 13563, 13867, 14696, 15460, 15311, 15603, 15861, 16807, 16919, 16388,
15433, 15497, 15145, 15163, 15984, 16859, 18150, 18970, 19328, 19337, 18876]

model = FuzzyTimeSeries(n_partitions=7, order=1).fit(DATA)
print(model.summary())
# FuzzyTimeSeries(n_partitions=7, order=1, mf_type='triangle', fitted, 5 FLRGs)
# Universe : [12426.8000, 19965.2000]
# Partitions : 7 (triangle)
# FLRGs found : 5

forecast(series): one step ahead​

model.forecast(series) -> list[float]

For each t≥ordert \ge \text{order}, forecasts series[t] from series[t-order:t] only (no look‑ahead). The result has len(series) - order values, aligned with series[order:]:

y_pred = model.forecast(DATA)
y_true = DATA[model.order:]

predict(history, steps=1): multi step​

model.predict(history, steps=1) -> list[float]

Forecasts steps values past the end of history (which needs at least order values). Each forecast is fuzzified and fed back in as input for the next step, so a first‑order model quickly settles on one value (here partition A6A_6 only ever leads to itself).

print(model.predict(DATA[-3:], steps=3))    # [18708.8, 18708.8, 18708.8]

evaluate(series)​

model.evaluate(series) -> dict[str, float]

Shortcut for score(series[order:], model.forecast(series)): RMSE, MAE and MAPE of the one‑step‑ahead forecasts on the aligned window.

print(model.evaluate(DATA))    # {'rmse': 535.81..., 'mae': 467.83..., 'mape': 2.87...}

To score on held‑out data, prepend the last order training values so the first test point gets a forecast too: model.evaluate(train[-model.order:] + test).

Values outside the universe​

A value outside the fitted universe (for example a test value larger than anything seen in training) is mapped to the nearest partition: A1A_1 below the universe, AnA_n above it. Chen's model can therefore never forecast beyond the outermost partition centers. If you know the plausible range in advance, set it with universe=(lo, hi) or widen margin.

Inspection​

MemberDescription
get_partitions()list[Partition], low to high
get_flrgs()list[FLRG] in order of first appearance
summary()multi‑line description (universe, partitions, number of FLRGs)
repr(model)one line, with fitted, k FLRGs or not fitted
n_partitions, order, mf_type, partitioner, margin, universethe constructor arguments

forecast, predict, evaluate, get_partitions, get_flrgs and summary raise RuntimeError if called before fit.

Partition​

One fuzzy set of the universe.

MemberDescription
label"A1", "A2", …
centerthe partition's center (its MF peak)
mfthe underlying Type1MF
membership(x)degree of x in this partition

FLRG​

A fuzzy logical relationship group: an antecedent window and every partition that followed it in the training data.

MemberDescription
antecedenttuple of partition indices (0‑based), length order
consequentslist of partition indices that followed it (no duplicates)
for g in model.get_flrgs():
print(g)
# FLRG(A2 -> A2, A3)
# FLRG(A3 -> A3, A4)
# FLRG(A4 -> A4, A5, A3)
# FLRG(A5 -> A4, A6)
# FLRG(A6 -> A6)

The forecast for a window is the mean of its consequents' centers. If a window never occurred in training, the forecast is the center of the window's last partition.