What does Koopman Modes for Audience Turning Points conclude, in numbers, about “Cash Flow: Linearising Daily, Weekly and Monthly Swings with Modes, and Choosing the Observable”?

Answer: Chapter 6 applies the method to a shop's daily cash flow, with a 28-day forecast error of 2301 yuan against 4665 for "the same day last week"; taking logarithms is a Koopman lift that cuts the rank-3 error from 0.1645 to 0.0257.

Scope of the numbers: Marked boxes separate published evidence, runnable Python calculations (19 programs), classical sources and the author's hypotheses.

中文版

Data table

The numbers below are printed verbatim by the chapter's Python program and can be reproduced by running it. standard-library Python onlyprinted values that match the answer: 2301, 28, 4665

mode |lambda| growth/day period (days)
1.00319+0.00319none
0.99912-0.0008830.30
0.99912-0.0008830.30
0.99849-0.001513.50
0.99849-0.001513.50
0.99838-0.001627.01
0.99838-0.001627.01
28-day forecast RMSE (yuan)vs observedvs noise-free
Hankel DMD rank 723011236
same weekday46655686
flat 7-day mean54056287
Show the full program
# Chapter 6. p61: daily net cash inflow as a handful of modes.
# Illustrative shop (yuan per day, 168 days): 20000 * 1.003^t growth, a
# weekly pattern (6000 cos(2 pi t/7) + 2500 cos(4 pi t/7 + 0.5)), a
# 30-day settlement swing 4000 cos(2 pi t/30 + 1) and noise sd 2500,
# fixed seed. Hankel DMD (42 rows, rank 7) is fitted on days 0-139 and
# forecasts days 140-167; it is compared with "same weekday last week"
# and with a flat 7-day average. Not a forecast of any real business.


import math
import cmath
import random


def dot(a, b):
    return math.fsum(x * y for x, y in zip(a, b))


def matmul(a, b):
    bt = list(zip(*b))
    return [[dot(r, c) for c in bt] for r in a]


def transpose(a):
    return [list(r) for r in zip(*a)]


def svd(a):
    # 单边 Jacobi(Hestenes):两两旋转列直到正交,A = U S V^T
    m, n = len(a), len(a[0])
    if n > m:
        u, s, v = svd(transpose(a))
        return v, s, u
    cols = [[a[i][j] for i in range(m)] for j in range(n)]
    vc = [[1.0 if i == j else 0.0 for i in range(n)] for j in range(n)]
    for sweep in range(60):
        rotated = False
        for p in range(n - 1):
            for q in range(p + 1, n):
                al = dot(cols[p], cols[p])
                be = dot(cols[q], cols[q])
                ga = dot(cols[p], cols[q])
                if ga == 0.0 or abs(ga) <= 1e-15 * math.sqrt(al * be):
                    continue
                rotated = True
                ze = (be - al) / (2.0 * ga)
                t = math.copysign(1.0, ze) / (
                    abs(ze) + math.sqrt(1.0 + ze * ze))
                c = 1.0 / math.sqrt(1.0 + t * t)
                s = c * t
                cp, cq = cols[p], cols[q]
                cols[p] = [c * x - s * y for x, y in zip(cp, cq)]
                cols[q] = [s * x + c * y for x, y in zip(cp, cq)]
                vp, vq = vc[p], vc[q]
                vc[p] = [c * x - s * y for x, y in zip(vp, vq)]
                vc[q] = [s * x + c * y for x, y in zip(vp, vq)]
        if not rotated:
            break
    sig = [math.sqrt(dot(c_, c_)) for c_ in cols]
    order = sorted(range(n), key=lambda j: (-sig[j], j))
    s = [sig[j] for j in order]
    u = [[cols[j][i] / sig[j] if sig[j] > 0.0 else 0.0 for j in order]
         for i in range(m)]
    v = [[vc[j][i] for j in order] for i in range(n)]
    return u, s, v


def hankel(x, rows):
    # 延迟嵌入:第 i 行是从第 i 分钟起的一段序列
    k = len(x) - rows + 1
    return [[x[i + j] for j in range(k)] for i in range(rows)]


def dmd(x, rows, r):
    # Hankel 上的精确 DMD:X、Y 为错开一列的两块,A~ = U^T Y V S^-1
    h = hankel(x, rows)
    xs = [row[:-1] for row in h]
    ys = [row[1:] for row in h]
    u, s, v = svd(xs)
    ur = [row[:r] for row in u]
    vr = [row[:r] for row in v]
    at = matmul(matmul(transpose(ur), ys), vr)
    at = [[at[i][j] / s[j] for j in range(r)] for i in range(r)]
    return ur, at, s, h


def charpoly(a):
    # Faddeev-LeVerrier:特征多项式 z^n + c1 z^(n-1) + c2 z^(n-2) 直到 cn
    n = len(a)
    m = [[0.0] * n for _ in range(n)]
    coef = [1.0]
    for k in range(1, n + 1):
        m = matmul(a, m)
        for i in range(n):
            m[i][i] += coef[-1]
        am = matmul(a, m)
        coef.append(-math.fsum(am[i][i] for i in range(n)) / k)
    return coef


def roots(coef):
    # Durand-Kerner 同时迭代求全部复根(cmath)
    n = len(coef) - 1
    z = [cmath.rect(0.9, 0.4 + 2.0 * math.pi * k / n) for k in range(n)]

    def p(x):
        r = complex(1.0, 0.0)
        for c in coef[1:]:
            r = r * x + c
        return r
    for _ in range(800):
        new = []
        for i in range(n):
            den = complex(1.0, 0.0)
            for j in range(n):
                if j != i:
                    den *= z[i] - z[j]
            new.append(z[i] - p(z[i]) / den)
        moved = max(abs(a - b) for a, b in zip(new, z))
        z = new
        if moved < 1e-14:
            break
    return z


def eigvals(a):
    z = roots(charpoly(a))
    z = [complex(w.real, 0.0) if abs(w.imag) < 1e-12 else w for w in z]
    return sorted(z, key=lambda w: (-round(abs(w), 9), w.imag))


def forecast(x, rows, r, steps):
    # 用降阶线性算子把最后一列延迟向量往前推 steps 步
    ur, at, s, h = dmd(x, rows, r)
    last = [row[-1] for row in h]
    z = [dot(col, last) for col in zip(*ur)]
    out = []
    for _ in range(steps):
        z = [dot(row, z) for row in at]
        out.append(dot(ur[-1], z))
    return out


def clean(t):
    return (20000.0 * 1.003 ** t
            + 6000.0 * math.cos(2.0 * math.pi * t / 7.0)
            + 2500.0 * math.cos(4.0 * math.pi * t / 7.0 + 0.5)
            + 4000.0 * math.cos(2.0 * math.pi * t / 30.0 + 1.0))


rng = random.Random(20261119)
x = [clean(t) + rng.gauss(0.0, 2500.0) for t in range(168)]
fit, fut = x[:140], x[140:]
ur, at, s, h = dmd(fit, 42, 7)
print("mode  |lambda|   growth/day   period (days)")
for lam in eigvals(at):
    ang = abs(cmath.phase(lam))
    per = "%.2f" % (2.0 * math.pi / ang) if ang > 1e-9 else "none"
    print("      %.5f   %+.5f     %s" % (abs(lam), abs(lam) - 1.0, per))
f_dmd = forecast(fit, 42, 7, 28)
f_week = [fit[140 - 7 + (k % 7)] for k in range(28)]
f_flat = [math.fsum(fit[-7:]) / 7.0] * 28


def rmse(f, ref):
    return math.sqrt(math.fsum((a - b) ** 2 for a, b in zip(f, ref))
                     / len(ref))


truth = [clean(t) for t in range(140, 168)]
print("28-day forecast RMSE (yuan)   vs observed   vs noise-free")
for name, f in (("Hankel DMD rank 7", f_dmd), ("same weekday", f_week),
                ("flat 7-day mean", f_flat)):
    print("  %-26s %9.0f    %9.0f" % (name, rmse(f, fut), rmse(f, truth)))

Single source and citation

Fan, Y. (2026). Koopman Operators and Dynamic Mode Decomposition: Linearising Nonlinear Cash-Flow Swings and Forecasting Turning Points in Livestream Audience Curves (Version 1.0.0), Chapter 6: Cash Flow: Linearising Daily, Weekly and Monthly Swings with Modes, and Choosing the Observable. 5D Library. DOI pending

DOI pending · monograph page goes live after publication · 5DL-B-238 · v1.0.0 · CC-BY-NC-ND-4.0

BibTeX
@book{fan2026238,
  author    = {Fan, Yafu},
  title     = {Koopman Operators and Dynamic Mode Decomposition: Linearising Nonlinear Cash-Flow Swings and Forecasting Turning Points in Livestream Audience Curves},
  year      = {2026},
  publisher = {5D Library},
  edition   = {1.0.0},
  note      = {DOI pending}
}

Other chapter answers in this book