def fit_and_plot(cand): data = cand.profile n = len(data) xs = np.linspace(0.0, 1.0, n, endpoint=False) G = gauss._compute_data(cand) print "k: %g, log(k): %g" % (G.k, np.log10(G.k)) test_ks = np.logspace(np.log10(G.k)-2, np.log10(G.k)+1, 1e3) #test_ks = np.exp(np.linspace(np.log(1e-1),np.log(1e3),1e3)) plt.figure(1) resids = [gauss._rms_residual(k,data) for k in test_ks] plt.loglog(test_ks,resids,color="green", label="_nolabel_") #plt.axvline(true_k,color="red", label="true k") best_k = test_ks[np.argmin(resids)] plt.axvline(best_k,color="green", label="best k") plt.axvline(G.k,color="cyan", label="k from fit") plt.ylabel("RMS of residuals") plt.xlabel("Value of k used (i.e. held fixed) when fitting") plt.legend(loc="best") plt.figure(2) mue, ae, be = gauss._fit_all_but_k(best_k, data) #plt.plot(xs, true_prof, color="red", label="true") plt.plot(xs, data, color="black", label="data") plt.plot(xs, (ae*utils.vonmises_histogram(best_k,mue,n)+be), color="green", label="exhaustive best fit") plt.plot(xs, G.histogram(n), color="cyan", label="best fit") plt.legend(loc="best") plt.show()
def fake_profile(cand): # Modify profile data and remove existing gaussian fit print "create fake profile to fit" true_k = 8 n = 128 s = 10 a = 10 mu = 0.1 k2 = 30 a2 = 10 mu2 = 0.5 true_prof = a*utils.vonmises_histogram(true_k,mu,n) + a2*utils.vonmises_histogram(k2,mu2,n) data = true_prof + s*np.random.randn(n) import copy modcand = copy.deepcopy(cand) modcand.profile = data del modcand.gaussfit return modcand
def _fit_all_but_k(cls, k, data): n = len(data) fft = np.fft.rfft(data) nup = 16*n corr = np.fft.irfft(fft*utils.vonmises_coefficient(k, np.arange(len(fft))), nup) mu = np.argmax(corr)/float(nup) x = utils.vonmises_histogram(k, mu, n) data = data - np.mean(data) x = x - np.mean(x) a = np.dot(data, x)/np.dot(x, x) b = np.mean(data) - a*np.mean(x) return mu, a, b
def _fit_all_but_k(cls, k, data): n = len(data) fft = np.fft.rfft(data) nup = 16 * n corr = np.fft.irfft( fft * utils.vonmises_coefficient(k, np.arange(len(fft))), nup) mu = np.argmax(corr) / float(nup) x = utils.vonmises_histogram(k, mu, n) data = data - np.mean(data) x = x - np.mean(x) a = np.dot(data, x) / np.dot(x, x) b = np.mean(data) - a * np.mean(x) return mu, a, b
def _rms_residual(cls, k, data): n = len(data) mu, a, b = cls._fit_all_but_k(k, data) return np.sqrt( np.mean((data - (a * utils.vonmises_histogram(k, mu, n) + b))**2))
def histogram(self, n): return self.a * utils.vonmises_histogram(self.k, self.mu, n) + self.b
def histogram(self, n): return self.a*utils.vonmises_histogram(self.k, self.mu, n) + self.b
def _rms_residual(cls, k, data): n = len(data) mu, a, b = cls._fit_all_but_k(k, data) return np.sqrt(np.mean((data-(a*utils.vonmises_histogram(k,mu,n)+b))**2))