Interpolation
Description
To interpolate it fits around the points to calculate a polynomial with the points that exist.
The process can be done using the panta library:
import pandas as pd
import numpy as np
year = []
value = []
for i in range(dmin,dmax + 1):
if i > dmin:
nmin = int(df.date[i-1])
nmax = int(df.date[i])
for n in range(nmin + 1,nmax):
year.append(n)
value.append(np.nan)
ndate = int(df.date[i])
year.append(ndate)
if df.value[i] == '':
value.append(np.nan)
else:
val = float(df.value[i])
if val > 99.9:
val = 99.9
elif val < 0.01:
val = 0.01
val = (val - 50)/50
val = math.atanh(val)
value.append(val)
servalue = pd.Series(value)
inpvalue = servalue.interpolate(method="polynomial", order=3)
ID:(318, 0)
Extrapolation
Description
To extrapolate, the function is scaled and proceeds to calculate the Fourier transform to then use the representation of the oscillation to estimate the future development.
The process can be done using the panta library:
import pandas as pd
import numpy as np
from numpy import fft
def fourierExtrapolation(x, n_predict):
n = x.size
n_harm = 10 # number of harmonics in model
t = np.arange(0, n)
p = np.polyfit(t, x, 1) # find linear trend in x
x_notrend = x - p[0] * t # detrended x
x_freqdom = fft.fft(x_notrend) # detrended x in frequency domain
f = fft.fftfreq(n) # frequencies
indexes = list(range(n))
t = np.arange(0, n + n_predict)
restored_sig = np.zeros(t.size)
for i in indexes[:1 + n_harm * 2]:
ampli = np.absolute(x_freqdom[i]) / n # amplitude
phase = np.angle(x_freqdom[i]) # phase
restored_sig += ampli * np.cos(2 * np.pi * f[i] * t + phase)
return restored_sig + p[0] * t
x = np.array(inpvalue)
z = (x - 50)/50
z = np.arctanh(z)
extrapolation = fourierExtrapolation(z, num)
extrapolation = 50*np.tanh(extrapolation) + 50
for i in range(len(extrapolation)):
if i < len(value):
print(year[i],value[i],inpvalue[i],extrapolation[i])
else:
print(year[0]+i,'-','-',extrapolation[i])
ID:(319, 0)