aboutsummaryrefslogtreecommitdiffstats
path: root/buch/papers/laguerre/scripts/gamma_approx.py
blob: 9c8f3eee7d779375c6e2aee5243e6fcd56674735 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
from pathlib import Path

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import scipy.special

EPSILON = 1e-7
root = str(Path(__file__).parent)
img_path = f"{root}/../images"


def _prep_zeros_and_weights(x, w, n):
    if x is None or w is None:
        return np.polynomial.laguerre.laggauss(n)
    return x, w


def drop_imag(z):
    if abs(z.imag) <= EPSILON:
        z = z.real
    return z


def pochhammer(z, n):
    return np.prod(z + np.arange(n))


def find_shift(z, target):
    factor = 1.0
    steps = int(np.floor(target - np.real(z)))
    zs = z + steps
    if steps > 0:
        factor = 1 / pochhammer(z, steps)
    elif steps < 0:
        factor = pochhammer(zs, -steps)
    return zs, factor


def find_optimal_shift(z, n):
    mhat = 1.34093 * n + 0.854093
    steps = int(np.ceil(mhat - np.real(z))) - 1
    return steps


def get_shifting_factor(z, steps):
    if steps > 0:
        factor = 1 / pochhammer(z, steps)
    elif steps < 0:
        factor = pochhammer(z + steps, -steps)
    return factor


def laguerre_gamma_shifted(z, x=None, w=None, n=8, target=11):
    x, w = _prep_zeros_and_weights(x, w, n)
    n = len(x)

    z += 0j
    z_shifted, correction_factor = find_shift(z, target)

    res = np.sum(x ** (z_shifted - 1) * w)
    res *= correction_factor
    res = drop_imag(res)
    return res


def laguerre_gamma_opt_shifted(z, x=None, w=None, n=8):
    x, w = _prep_zeros_and_weights(x, w, n)
    n = len(x)

    z += 0j
    opt_shift = find_optimal_shift(z, n)
    correction_factor = get_shifting_factor(z, opt_shift)
    z_shifted = z + opt_shift

    res = np.sum(x ** (z_shifted - 1) * w)
    res *= correction_factor
    res = drop_imag(res)
    return res


def laguerre_gamma_simple(z, x=None, w=None, n=8):
    x, w = _prep_zeros_and_weights(x, w, n)
    z += 0j
    res = np.sum(x ** (z - 1) * w)
    res = drop_imag(res)
    return res


def laguerre_gamma_mirror(z, x=None, w=None, n=8):
    x, w = _prep_zeros_and_weights(x, w, n)
    z += 0j
    if z.real < 1e-3:
        return np.pi / (
            np.sin(np.pi * z) * laguerre_gamma_simple(1 - z, x, w)
        )  # Reflection formula
    return laguerre_gamma_simple(z, x, w)


def eval_laguerre_gamma(z, x=None, w=None, n=8, func="simple", **kwargs):
    x, w = _prep_zeros_and_weights(x, w, n)
    if func == "simple":
        f = laguerre_gamma_simple
    elif func == "mirror":
        f = laguerre_gamma_mirror
    elif func == "optimal_shifted":
        f = laguerre_gamma_opt_shifted
    else:
        f = laguerre_gamma_shifted
    return np.array([f(zi, x, w, n, **kwargs) for zi in z])


def calc_rel_error(x, y):
    return (y - x) / x


# Simple / naive
xmin = -5
xmax = 30
ns = np.arange(2, 12, 2)
ylim = np.array([-11, 6])
x = np.linspace(xmin + EPSILON, xmax - EPSILON, 400)
gamma = scipy.special.gamma(x)
fig, ax = plt.subplots(num=1, clear=True, constrained_layout=True, figsize=(5, 2.5))
for n in ns:
    gamma_lag = eval_laguerre_gamma(x, n=n)
    rel_err = calc_rel_error(gamma, gamma_lag)
    ax.semilogy(x, np.abs(rel_err), label=f"$n={n}$")
ax.set_xlim(x[0], x[-1])
ax.set_ylim(*(10.0 ** ylim))
ax.set_xticks(np.arange(xmin, xmax + EPSILON, 5))
ax.set_xticks(np.arange(xmin, xmax), minor=True)
ax.set_yticks(10.0 ** np.arange(*ylim, 2))
ax.set_yticks(10.0 ** np.arange(*ylim, 2))
ax.set_xlabel(r"$z$")
ax.set_ylabel("Relativer Fehler")
ax.legend(ncol=3, fontsize="small")
ax.grid(1, "both")
# fig.savefig(f"{img_path}/rel_error_simple.pgf")


# Mirrored
xmin = -15
xmax = 15
ylim = np.array([-11, 1])
x = np.linspace(xmin + EPSILON, xmax - EPSILON, 400)
gamma = scipy.special.gamma(x)
fig2, ax2 = plt.subplots(num=2, clear=True, constrained_layout=True, figsize=(5, 2.5))
for n in ns:
    gamma_lag = eval_laguerre_gamma(x, n=n, func="mirror")
    rel_err = calc_rel_error(gamma, gamma_lag)
    ax2.semilogy(x, np.abs(rel_err), label=f"$n={n}$")
ax2.set_xlim(x[0], x[-1])
ax2.set_ylim(*(10.0 ** ylim))
ax2.set_xticks(np.arange(xmin, xmax + EPSILON, 5))
ax2.set_xticks(np.arange(xmin, xmax), minor=True)
ax2.set_yticks(10.0 ** np.arange(*ylim, 2))
# locmin = mpl.ticker.LogLocator(base=10.0,subs=0.1*np.arange(1,10),numticks=100)
# ax2.yaxis.set_minor_locator(locmin)
# ax2.yaxis.set_minor_formatter(mpl.ticker.NullFormatter())
ax2.set_xlabel(r"$z$")
ax2.set_ylabel("Relativer Fehler")
ax2.legend(ncol=1, loc="upper left", fontsize="small")
ax2.grid(1, "both")
# fig2.savefig(f"{img_path}/rel_error_mirror.pgf")


# Move to target
bests = []
N = 200
step = 1 / (N - 1)
a = 11 / 8
b = 1 / 2
x = np.linspace(step, 1 - step, N + 1)
gamma = scipy.special.gamma(x)[:, None]
ns = np.arange(2, 13)
for n in ns:
    zeros, weights = np.polynomial.laguerre.laggauss(n)
    est = np.ceil(b + a * n)
    targets = np.arange(max(est - 2, 0), est + 3)
    gamma_lag = np.stack(
        [
            eval_laguerre_gamma(x, target=target, x=zeros, w=weights, func="shifted")
            for target in targets
        ],
        -1,
    )
    rel_error = np.abs(calc_rel_error(gamma, gamma_lag))
    best = np.argmin(rel_error, -1) + targets[0]
    bests.append(best)
bests = np.stack(bests, 0)

fig3, ax3 = plt.subplots(num=3, clear=True, constrained_layout=True, figsize=(5, 3))
v = ax3.imshow(bests, cmap="inferno", aspect="auto", interpolation="nearest")
plt.colorbar(v, ax=ax3, label=r"$m$")
ticks = np.arange(0, N + 1, N // 5)
ax3.set_xlim(0, 1)
ax3.set_xticks(ticks)
ax3.set_xticklabels([f"{v:.2f}" for v in ticks / N])
ax3.set_xticks(np.arange(0, N + 1, N // 20), minor=True)
ax3.set_yticks(np.arange(len(ns)))
ax3.set_yticklabels(ns)
ax3.set_xlabel(r"$z$")
ax3.set_ylabel(r"$n$")
# fig3.savefig(f"{img_path}/targets.pdf")

targets = np.mean(bests, -1)
intercept, bias = np.polyfit(ns, targets, 1)
fig4, axs4 = plt.subplots(
    2, num=4, sharex=True, clear=True, constrained_layout=True, figsize=(5, 4)
)
xl = np.array([ns[0] - 0.5, ns[-1] + 0.5])
axs4[0].plot(xl, intercept * xl + bias, label=r"$\hat{m}$")
axs4[0].plot(ns, targets, "x", label=r"$\bar{m}$")
axs4[1].plot(ns, ((intercept * ns + bias) - targets), "-x", label=r"$\hat{m} - \bar{m}$")
axs4[0].set_xlim(*xl)
# axs4[0].set_title("Schätzung von Mittelwert")
# axs4[1].set_title("Fehler")
axs4[-1].set_xlabel(r"$z$")
for ax in axs4:
    ax.grid(1)
    ax.legend()
# fig4.savefig(f"{img_path}/schaetzung.pgf")

print(f"Intercept={intercept:.6g}, Bias={bias:.6g}")
predicts = np.ceil(intercept * ns[:, None] + bias - x)
print(f"Error: {int(np.sum(np.abs(bests-predicts)))}")

# Comparison relative error between methods
N = 200
step = 1 / (N - 1)
x = np.linspace(step, 1 - step, N + 1)
gamma = scipy.special.gamma(x)[:, None]
n = 8
targets = np.arange(10, 14)
gamma = scipy.special.gamma(x)
fig5, ax5 = plt.subplots(num=1, clear=True, constrained_layout=True)
for target in targets:
    gamma_lag = eval_laguerre_gamma(x, target=target, n=n, func="shifted")
    rel_error = np.abs(calc_rel_error(gamma, gamma_lag))
    ax5.semilogy(x, rel_error, label=f"$m={target}$")
gamma_lgo = eval_laguerre_gamma(x, n=n, func="optimal_shifted")
rel_error = np.abs(calc_rel_error(gamma, gamma_lgo))
ax5.semilogy(x, rel_error, label="$m^*$")
ax5.set_xlim(x[0], x[-1])
ax5.set_ylim(5e-9, 5e-8)
ax5.set_xlabel(r"$z$")
ax5.grid(1, "both")
ax5.legend()
fig5.savefig(f"{img_path}/rel_error_shifted.pgf")

N = 200
x = np.linspace(-5+ EPSILON, 5-EPSILON, N)
gamma = scipy.special.gamma(x)[:, None]
n = 8
gamma = scipy.special.gamma(x)
fig6, ax6 = plt.subplots(num=1, clear=True, constrained_layout=True)
gamma_lgo = eval_laguerre_gamma(x, n=n, func="optimal_shifted")
rel_error = np.abs(calc_rel_error(gamma, gamma_lgo))
ax6.semilogy(x, rel_error, label="$m^*$")
ax6.set_xlim(x[0], x[-1])
ax6.set_ylim(5e-9, 5e-8)
ax6.set_xlabel(r"$z$")
ax6.grid(1, "both")
ax6.legend()
fig6.savefig(f"{img_path}/rel_error_range.pgf")

# plt.show()