<jupyter_start><jupyter_text>Welch's Method to Determine Seasonality Terms<jupyter_code>f,pxx = signal.welch(y.values.flatten(), fs=1, nperseg=len(y))
print(len(f))
print(len(y))

freqs = [1/91.1875, 1/182.375, 1/357.25, 1/364.75, 1/6.980861244019138, 1/729.5]


fig, ax = plt.subplots(figsize=(50, 4))
for freq in freqs:
    plt.axvline(x=freq, c='grey', linestyle='dashed')
ax.set_xlabel('Frequency ($T^{-1}$)')
ax.set_ylabel('Power Spectum Density')
plt.plot(f, pxx)

plt.show()
    
print(np.sqrt(pxx.max()).argmax())
print(pxx.argmax())
print(pxx.argsort()[0:])
print(1/f[16])
print(1/f[8])
print(1/f[4])
print(1/f[209])
print(1/f[208])
print(1/f[2])
y_to_train = y.iloc[:(len(y))]
y_to_test = y.iloc[(len(y)-365):]<jupyter_output><empty_output><jupyter_text>Fourier Transform to Determine Seasonality Terms<jupyter_code># # Frequency and sampling rate
# f = 10 # frequency
# Fs = 100 # sampling rate
# t = np.arange(0,1,1/Fs)
# # # Sine function
# y = np.sin(2 * np.pi * f * t)
# # Perform Fourier transform using scipy
# from scipy import fftpack
# y_fft = fftpack.fft(y)
# # Plot data
# n = np.size(t)
# fr = Fs/2 * np.linspace(0,1,n/2)
# y_m = 2/n * abs(y_fft[0:np.size(fr)])
# fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(15,5))
# ax[0].plot(t, y)    # plot time series
# ax[1].stem(fr, y_m) # plot freq domain

# y_fft = fftpack.fft(y.values.flatten())

# amplitude = np.abs(y_fft)
# power = amplitude**2
# angle = np.angle(y_fft)

# sample_freq = fftpack.fftfreq(y.size, 1)

# amp_freq = np.array([amplitude, sample_freq])

# amp_position = amp_freq[1,:].argmax()

# peak_freq =  amp_freq[1, amp_position]

# plt.plot(amplitude, amp_freq.T)

# print(amp_position)
# print(peak_freq)

# The FFT of the signal
sig_fft = fftpack.fft(y)

# And the power (sig_fft is of complex dtype)
power = np.abs(sig_fft)**2

# The corresponding frequencies
sample_freq = fftpack.fftfreq(y.size, d=1)

# Plot the FFT power
plt.figure(figsize=(20, 5))
plt.plot(sample_freq, power)
plt.xlabel('Frequency [Hz]')
plt.ylabel('plower')

# Find the peak frequency: we can focus on only the positive frequencies
pos_mask = np.where(sample_freq > 0)
freqs = sample_freq[pos_mask]
peak_freq = freqs[power[pos_mask].argmax()]

# Check that it does indeed correspond to the frequency that we generate
# the signal with
np.allclose(peak_freq, 1./1426.)

# An inner plot to show the peak frequency
axes = plt.axes([0.55, 0.3, 0.3, 0.5])
plt.title('Peak frequency')
plt.plot(freqs[:8], power[:8])
plt.setp(axes, yticks=[])

# scipy.signal.find_peaks_cwt can also be used for more advanced
# peak detection<jupyter_output><empty_output><jupyter_text>TBATS<jupyter_code># Fit the model
estimator = TBATS(seasonal_periods=(7.004901960784315, 30.404255319148938, 357.25)) # Best so far: (7, 30.5, 91.3, 365) and (7, 75 (???), 365)
model = estimator.fit(y_to_train)

# Forecast 365 days ahead
y_forecast = model.forecast(steps=365)
print(model.summary())
#get the predictions and residuals
predictions = pd.Series(y_forecast, index=y_to_test.index)
residuals = y_to_test - predictions
plt.figure(figsize=(10,4))

plt.plot(y)
plt.plot(predictions)

plt.legend(('Data', 'Predictions'), fontsize=16)

plt.title('London Uber Travel Times in Seconds', fontsize=20)
plt.ylabel('Seconds', fontsize=16)
# for year in range(start_date.year,end_date.year):
#     plt.axvline(pd.to_datetime(str(year)+'-01-01'), color='k', linestyle='--', alpha=0.2)
# Mean Absolute Percentage Error (MAPE)
def mean_absolute_percentage_error(y_to_test, predictions): 
    y_to_test, predictions = np.array(y_to_test), np.array(predictions)
    return np.mean(np.abs((predictions - y_to_test)/y_to_test)) * 100

mean_absolute_percentage_error(y_to_test, predictions)
# Root Mean Square Error (RMSE)
def rmse(y_to_test, predictions):
    y_to_test, predictions = np.array(y_to_test), np.array(predictions)
    return np.sqrt(np.mean((predictions - y_to_test)**2))

rmse(y_to_test, predictions)
# Plotting trend, seasonality and residuals
decomposition = seasonal_decompose(y, model='additive', period=91)
trend    = decomposition.trend
seasonal = decomposition.seasonal
residual = decomposition.resid

fig, ax1 = plt.subplots(1, figsize=(50,10))
plt.xticks(fontsize=30)
plt.yticks(fontsize=30)

ax1.plot(y.index.unique(), seasonal)
ax1.set_title("Seasonal Decomposition", fontsize=40)
ax1.set_ylabel("Mean Travel Times (Seconds)", fontsize=30)

# ax2.plot(y.index.unique(), seasonal)
# ax2.set_title("Seasonal", fontsize=40)

# ax3.plot(y.index.unique(), residual)
# ax3.set_title("Residual", fontsize=40)

fig.tight_layout()
plt.show()
y_saved = y.to_csv('grouped_london.csv')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16237113402061856, 'fraction_numerical': 0.04896907216494845, 'mean_word_length': 5.710374639769452, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, 'https://': 0, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '470463', 'license': 'Apache-2.0', 'n_tokens_mistral': 2058, 'n_tokens_neox': 1870, 'n_words': 372, 'path': 'Uber_TS_TBATS_Test.ipynb', 'repo_name': 'GunH-colab/UCS-21', 'size': '4656'}