Last active
October 15, 2018 01:40
-
-
Save tuankiet65/e55fc6517d8ee68a89e61aba067c9321 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
import scipy.optimize | |
from matplotlib import pyplot as plt | |
def calculate_rmse(poly, x, y): | |
y_poly = numpy.polyval(poly, x) | |
mse = ((y - y_poly) ** 2).mean() | |
return numpy.sqrt(mse) | |
def calculate_rmse_exp(popt, x, y): | |
y_exp = exponential_fit(x, *popt) | |
mse = ((y - y_exp) ** 2).mean() | |
return numpy.sqrt(mse) | |
def exponential_fit(x, a, b, c): | |
return a * numpy.exp(-b * x) + c | |
data = numpy.loadtxt("vietnam_gdp.csv", delimiter = ",") | |
x = data[:,0] | |
y = data[:,1] | |
polys = [] | |
for i in range(3, 4): | |
poly = numpy.polyfit(x, y, deg = i) | |
print(f"{i} poly: ", poly) | |
poly = numpy.poly1d(poly) | |
polys.append(poly) | |
plt.title("Vietnam's GDP, 1989-2017 period") | |
plt.xlabel("Year") | |
plt.ylabel("GDP (in 10 billion USD)") | |
plt.plot(x, y, '.', label = "Original data", zorder = 10) | |
for poly in polys: | |
range_x = numpy.linspace(min(x) - 3, max(x) + 3, num = 100) | |
plt.plot(range_x, poly(range_x), label = f"{len(poly)}-degree polyfit (RMSE: {calculate_rmse(poly, x, y):.4f})") | |
popt, _ = scipy.optimize.curve_fit(exponential_fit, x, y, p0 = (1e-6, 1e-6, 1e-6), maxfev = 100000) | |
range_x = numpy.linspace(min(x) - 3, max(x) + 3, num = 100) | |
plt.plot(range_x, exponential_fit(range_x, *popt), label = f"Exponential fit, 100k calls (RMSE: {calculate_rmse_exp(popt, x, y):.4f})") | |
print("exp: ", popt) | |
maple_poly = numpy.poly1d([0.000671226382184633, -3.99601866044572, 7929.82423581614, -5245381.72886966]) | |
plt.plot(range_x, maple_poly(range_x), label = f"{len(maple_poly)}-degree polyfit (Maple) (RMSE: {calculate_rmse(maple_poly, x, y):.4f})") | |
plt.legend(loc = "best") | |
plt.show() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1989 | 0.629330497459403 | |
---|---|---|
1990 | 0.647174080556984 | |
1991 | 0.961336952041885 | |
1992 | 0.986699023643587 | |
1993 | 1.31809535981716 | |
1994 | 1.62864335333228 | |
1995 | 2.07361644589505 | |
1996 | 2.46574705747501 | |
1997 | 2.68437004415482 | |
1998 | 2.72096020500452 | |
1999 | 2.86836590067752 | |
2000 | 3.11725184033162 | |
2001 | 3.26851987353053 | |
2002 | 3.50641055008345 | |
2003 | 3.95525133160734 | |
2004 | 4.54278546932554 | |
2005 | 5.76332556182731 | |
2006 | 6.63716648170436 | |
2007 | 7.74144255322452 | |
2008 | 9.91303040991274 | |
2009 | 10.6014659770222 | |
2010 | 11.5931749697241 | |
2011 | 13.5539438559709 | |
2012 | 15.5820001920492 | |
2013 | 17.1222025117381 | |
2014 | 18.6204652922262 | |
2015 | 19.3241108709536 | |
2016 | 20.5276172134901 | |
2017 | 22.3863996354655 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment