<jupyter_start><jupyter_text>Interpolation using Polynomials[![Latest release](https://badgen.net/github/release/Naereen/Strapdown.js)](https://github.com/eabarnes1010/course_objective_analysis/tree/main/code)[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/eabarnes1010/course_objective_analysis/blob/main/code/interpolation_polynomial_fitting.ipynb)One of the most intuitive methods for interpolating may be to fit some polynomial to all of the observations that you have and then use this curve to get values between your observational points. However, this method is not great because it is pretty unstable (as shown below).<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import scipy.signal as sig
import scipy.stats as stats<jupyter_output><empty_output><jupyter_text>Make-up some "observations" at 7 stations<jupyter_code>x = np.array((0.,1.,2.,3.,4.,5.,7.)) #position of station
y = np.array((0.,2.,4.,3.,3.,4.,6.)) # observational value at station<jupyter_output><empty_output><jupyter_text>Plot the data. Let's pretend "x" denotes the distance from my house (moving directly east) and "y" denotes the temperature anomaly.<jupyter_code>plt.figure()
plt.plot(x,y,'sk', markersize = 10, markerfacecolor = 'none', markeredgewidth = 2)
plt.xlim(-0.5,8.)
plt.ylim(-0.5,8.)
plt.ylabel('temperature (K)')
plt.xlabel('distance from my house (km)')
plt.show()<jupyter_output><empty_output><jupyter_text>Polynomial fitNow let's do a simple polynomial fit for the data. To start, we can choose the order of our polynomial to be 5, but this can be changed.<jupyter_code>poly_order = 5<jupyter_output><empty_output><jupyter_text>Now let's use polyfit to fit the data. "xfit" denotes our fine mesh x-grid we will use to create a fully smooth polynomial after we determine the regression coefficients.<jupyter_code>p = np.polyfit(x, y, poly_order, rcond=None, full=False, w=None, cov=False)<jupyter_output><empty_output><jupyter_text>Now that we have the coefficients for our fit, let's make a smooth curve for plotting.<jupyter_code>xfit = np.arange(0.,7.+.01,.01)
smooth_polyfit = np.poly1d(p)(xfit)

plt.figure()

plt.plot(x,y,'sk', markersize = 10, markerfacecolor = 'none', markeredgewidth = 2)
plt.plot(xfit,smooth_polyfit,'--k')

plt.xlim(-0.5,8.)
plt.ylim(-0.5,8.)
plt.ylabel('temperature (K)')
plt.xlabel('distance from my house (km)')
plt.show()<jupyter_output><empty_output><jupyter_text>As you can see, the polynomial fit for order = 5 does a pretty nice job. So what's the problem?! Well, the issue is if you remove one of the points (say, the monitor was down that day)...  Removing a single pointLet's remove the third point from our data (at x = 2km) and see what happens...<jupyter_code>xnew = np.delete(x,2)
ynew = np.delete(y,2)

plt.figure()

plt.plot(x,y,'sk', markersize = 10, markerfacecolor = 'none', markeredgewidth = 2)
plt.plot(xfit,smooth_polyfit,'--k')
plt.plot(xnew,ynew,'or',markersize = 8)

plt.xlim(-0.5,8.)
plt.ylim(-0.5,8.)
plt.ylabel('temperature (K)')
plt.xlabel('distance from my house (km)')
plt.show()<jupyter_output><empty_output><jupyter_text>What is our new polynomial fit? Performing the same analysis as above leads to...<jupyter_code>p = np.polyfit(xnew, ynew, poly_order, rcond=None, full=False, w=None, cov=False)
smooth_polyfit_new = np.poly1d(p)(xfit)

plt.figure()

plt.plot(x,y,'sk', markersize = 10, markerfacecolor = 'none', markeredgewidth = 2)
plt.plot(xfit,smooth_polyfit,'--k')
plt.plot(xnew,ynew,'or',markersize = 8)
plt.plot(xfit,smooth_polyfit_new,'--r')

plt.xlim(-0.5,8.)
plt.ylim(-0.5,8.)
plt.ylabel('temperature (K)')
plt.xlabel('distance from my house (km)')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1431241655540721, 'fraction_numerical': 0.020026702269692925, 'mean_word_length': 6.001869158878504, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '296722', 'license': 'MIT', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1331, 'n_words': 343, 'path': 'code/interpolation_polynomial_fitting.ipynb', 'repo_name': 'eabarnes1010/course_objective_analysis', 'size': '3745'}