Skip to content

Instantly share code, notes, and snippets.

@henningpohl
Last active November 20, 2017 20:38
Show Gist options
  • Save henningpohl/671bc088035801ba7b3f to your computer and use it in GitHub Desktop.
Save henningpohl/671bc088035801ba7b3f to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import linregress
from scipy.optimize import curve_fit
data = [
(1982, 165),
(1983, 176),
(1985, 170),
(1986, 122),
(1987, 166),
(1988, 187),
(1989, 199),
(1990, 260),
(1991, 240),
(1992, 216),
(1993, 330),
(1994, 263),
(1995, 228),
(1996, 256),
(1997, 234),
(1998, 351),
(1999, 312),
(2000, 336),
(2001, 352),
(2002, 414),
(2003, 468),
(2004, 578),
(2005, 372),
(2006, 626),
(2007, 840),
(2008, 714),
(2009, 1130),
(2010, 1346),
(2011, 1532),
(2012, 1577),
(2013, 1963),
(2014, 2043),
(2015, 2120),
(2016, 2435),
(2017, 2400),
(2018, 2592)]
years = np.array([d[0] for d in data])
submissions = np.array([d[1] for d in data])
def func(x, a, b, c, d):
return a + b * np.exp(c + d * x)
p0 = (0, 1, 0, 0.000001)
fit_data, covariance = curve_fit(func, years, submissions, p0, maxfev=100000)
x2 = np.linspace(years[0], years[-1] + 10, 500)
y2 = func(x2, *fit_data)
_, _, r, p, stdErr = linregress(submissions, func(years, *fit_data))
print("R-squared:", r ** 2)
print(years[-1] + 1, 'prediction:', int(func(years[-1] + 1, *fit_data)))
fig = plt.figure(frameon=False, facecolor='w')
plt.scatter(years, submissions, marker='o', s=30, linewidth=0, c='.2', zorder=2)
plt.plot(x2, y2, c=(.8, .3, .1), lw=1.5, zorder=1)
plt.xlim(1980, 2025)
plt.ylim(0, 10000)
plt.tight_layout()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment