-
Notifications
You must be signed in to change notification settings - Fork 1
/
polyfit.py
200 lines (165 loc) · 5.37 KB
/
polyfit.py
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
"""
Bayesian fitting of Polynomial model
%%file requirements.txt
numpy
scipy
matplotlib
emcee
arviz
"""
__author__ = "Morgan Fouesneau"
__copyright__ = "Copyright (c) 2022, Max Planck Institute for Astronomy"
__credits__ = ["Morgan Fouesneau", "Coryn Bailer-Jones", "Ivelina Momcheva"]
__license__ = "BSD 3-Clause"
__version__ = "1.0.0"
__maintainer__ = "Morgan Fouesneau"
__email__ = "[email protected]"
__status__ = "Production"
import numpy as np
from numpy.polynomial.polynomial import polyvander
from scipy.stats import norm
from typing import Union
def polynomial_model(
deg: int,
coeffs: np.array,
x: np.array
):
"""
Generate a polynomial model y = sum(a[k] * x ** k, k=[0..deg])
:param deg: degree of the polynomial
:param coeffs: coefficients of the polynomials
:param x: evaluation points or the polynomial expanded version.
:returns: polynomial model evaluated at x
"""
if len(coeffs) != (deg + 1):
raise AttributeError(f"The coefficient vector is not of length 'deg':{deg + 1} vs. {coeffs.shape}")
if np.ndim(x) == 1:
X = polyvander(x, deg) # [1, x, x**2, ..., x ** deg]
return X @ np.array(coeffs)
return x @ np.array(coeffs)
def lnprior(
deg: int,
coeffs: np.array,
l1: float = 0.,
l2: float = 0.) -> float:
"""ln Prior on the polynomial parameters: ln p(coeffs)
Generate a polynomial model y = sum(a[k] * x ** k, k=[0..deg])
:param deg: degree of the polynomial
:param coeffs: coefficients of the polynomials
:param l1: l1 norm contribution
:param l2: l2 norm contribution
:return: ln-prior value
"""
if (l1 == 0) and (l2 == 0):
return 0.
return np.log(np.sum(l1 * np.abs(coeffs) + 0.5 * l2 * coeffs ** 2))
def lnlikelihood(
coeffs: np.array,
x: np.array,
y: np.array,
sy: np.array,
deg: int):
""" ln Likelihood: ln p(y | x, sy, coeffs)
:param coeffs: coefficients of the polynomials
:param x: evaluation points
:param y: observed values
:param sy: observed value uncertainties
:param deg: degree of the polynomial
:return: ln-likelihood
"""
ypred = polynomial_model(deg, coeffs, x)
return np.sum(norm.logpdf(y, loc=ypred, scale=sy))
def lnposterior(
coeffs: np.array,
x: np.array,
y: np.array,
sy: np.array,
deg: int):
""" ln posterior: ln p(y | x, sy, coeffs) + ln p(coeffs)
:param coeffs: coefficients of the polynomials
:param x: evaluation points
:param y: observed values
:param sy: observed value uncertainties
:param deg: degree of the polynomial
:return: ln-likelihood
"""
return lnprior(deg, coeffs) + lnlikelihood(coeffs, x, y, sy, deg)
# mock data
np.random.seed(123)
N = 10
x = np.sort(np.random.uniform(-5, 5, N))
ctrue = np.array([0, 1, -2, 0.5])
deg = len(ctrue) - 1
sy = 5 * np.random.rand(N)
ytrue = polynomial_model(deg, ctrue, x)
y = ytrue + np.random.normal(0, sy)
# plot the mock data
xplot = np.linspace(-5, 5, 1000)
yplot = polynomial_model(deg, ctrue, xplot)
plt.plot(xplot, yplot, color='0.5', label='ytrue')
plt.errorbar(x, y, yerr=sy, linestyle='None', marker='o', mfc='w', label='yobs')
plt.ylim(y.min() - 5, y.max() + 5)
plt.legend(loc='best', frameon=False)
plt.xlabel('x')
plt.ylabel('y');
# Proceed to mcmc fitting
import emcee
ndim, nwalkers = deg + 1, 10
p0 = np.random.randn(nwalkers, ndim)
X = polyvander(x, deg) # precompute
sampler = emcee.EnsembleSampler(nwalkers, ndim, lnposterior, args=[X, y, sy, deg])
# We could run sampler.run_mcmc(p0, 2000) but sometimes it could be slow,
# and it is nice to monitor what happens
# Let's run the burn-in part
for state in sampler.sample(p0, iterations=1_000, progress=True):
pass
sampler.reset() # tells the sampler to forget the chains
# Restart from the current state of the chains
for _ in sampler.sample(state, iterations=1_000, progress=True):
pass
# Explore the outputs
import arviz as az
names = ["1", "x"] + [rf"$x^{k}$" for k in range(2, deg+1)]
data = az.from_emcee(sampler, var_names=names, )
axes = az.plot_pair(
data,
# var_names=names,
kind="kde",
marginals=True,
point_estimate="median",
kde_kwargs={
# "hdi_probs": [0.682, 0.954, 0.997], # Plot HDI contours
"hdi_probs": [0.682, 0.954, 0.997], # Plot HDI contours
"contourf_kwargs": {"cmap": "Blues"},
},
figsize=(8, 6)
)
# add true values for comparison
for dim in range(len(ctrue)):
val = ctrue[dim]
for ax in axes[:, dim]:
lim = ax.get_ylim()
ax.vlines([val], *lim, color='r')
ax.set_ylim(lim)
for dim in range(1, len(ctrue)):
val = ctrue[dim]
for ax in axes[dim, :dim]:
lim = ax.get_xlim()
ax.hlines([val], *lim, color='r')
ax.set_xlim(lim)
plt.subplots_adjust(wspace=0.05, hspace=0.05)
plt.figure()
# plot the ppc
xplot = np.linspace(-5, 5, 1000)
yplot = polynomial_model(deg, ctrue, xplot)
params = sampler.flatchain[-1000:]
Xpred = polyvander(xplot, deg)
ypred = np.array([polynomial_model(deg, pos, Xpred) for pos in params])
plt.plot(xplot, ypred.T, rasterized=True, color='k', alpha=0.2, lw=0.1)
plt.plot(xplot, yplot, color='w', lw=5)
plt.plot(xplot, yplot, color='C0', lw=3, label='ytrue')
plt.errorbar(x, y, yerr=sy, linestyle='None', marker='o', mfc='w', label='yobs')
plt.ylim(y.min() - 5, y.max() + 5)
plt.legend(loc='best', frameon=False)
plt.xlabel('x')
plt.ylabel('y');