In [3]:
import numpy as np
from scipy.stats import chisquare
In [183]:
x, y = np.array([5, 4, 6, 7]), np.array([5.1, 3.8, 6.1, 7])
chisquare(x, y)
Out[183]:
In [10]:
def chisqr(x, y):
return np.sum((x - y)**2 / y )
chisqr(x, y)
Out[10]:
The scipy.stats.chisquare value is the same as the manual chisqr above.
In [45]:
z = np.array([[1, 1]]).T
print(z)
print(z.T)
X = z * x
XX = x * z
print(X)
print(XX)
model = np.array([[1,2,3,4,5,6,7]])
model.T * XX.T
Y = y * z
chisquare(X,Y)
In [96]:
pix = np.arange(1, 1000)
alpha = np.arange(1, 9, 0.3)
broad = pix[:, np.newaxis] * alpha
c = chisquare(4*pix[:, np.newaxis], broad)
c.statistic
Out[96]:
In [99]:
import matplotlib.pyplot as plt
plt.plot(alpha, c.statistic)
plt.show()
In [98]:
alpha[np.argmin(c.statistic)]
Out[98]:
In [188]:
pix = np.arange(1, 10000)
alpha = np.linespace(1, 9, 500)
cc = np.empty_like(alpha)
In [186]:
# Timing with he results
%time
broad = pix[:, np.newaxis] * alpha
c = chisquare(4*pix[:, np.newaxis], broad)
c.statistic
Out[186]:
In [187]:
# Timing with broadcasting
% time
for i, a in enumerate(alpha):
b = pix * a
cc[i] = chisquare(pix, b)
cc
In [101]:
# Duel phoenix model:
In [135]:
x = np.random.rand(7)
y = np.random.rand(7)
obs = x + 0.1 * y
alpha = np.arange(0.01, 0.3, 0.02)
rv =
print(alpha.shape)
print(y.shape)
yy = y * alpha[:, np.newaxis]
In [140]:
mod = x + yy
a = (obs - mod)**2 / yy
In [146]:
a
alpha[np.argmin(a.T)]
Out[146]:
In [148]:
b = chisquare(obs, yy)
chisqr = b.statistic
alpha[np.argmin(b.statistic)]
Out[148]:
In [149]:
This vectorizes one of the biggest inner steps. alpha value and chisqure generation.
In [150]:
# Can I vectorize the doppler shift?
import PyAstronomy.pyasl as pyasl
In [180]:
wav = np.random.rand(1, 5)
flux = np.random.rand(4, 5)
v = np.random.rand(2,1)
a,b = pyasl.dopplerShift(wav, flux, v)
In [ ]: