This introduces finding the lower peaks of a financial time series to interpolate these values across the data points of the lower peaks to generate the lower envelope of this time series.
There are different interpolation methods introduced here. Piecewise Cubic Hermite Interpolating Polynomial (PCHIP), Natural Cubic Spline Interpolation, and AKIMA Spline Interpolations are the effective tool for the enveloping. The choice will differ depending on the purpose of finding the envelope.
My Python codes are displayed below:
# importing necessary tools for math and plot
# For mathematics
import math
import numpy as np
from datetime import datetime
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.find_peaks.html
# https://stackoverflow.com/questions/1713335/peak-finding-algorithm-for-python-scipy
from scipy.signal import find_peaks
# Piecewise Cubic Hermite Interpolating Polynomial (PCHIP)
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.pchip_interpolate.html
# https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter17.03-Cubic-Spline-Interpolation.html
from scipy.interpolate import pchip_interpolate
# Cubic Spline Interpolation
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.CubicSpline.html
from scipy.interpolate import CubicSpline
# AKIMA Spling Interpolation
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.Akima1DInterpolator.html
from scipy.interpolate import Akima1DInterpolator
# Inline animations in Jupyter
# https://stackoverflow.com/questions/43445103/inline-animations-in-jupyter
from matplotlib.animation import FuncAnimation
import matplotlib.animation as animation
import itertools # for joining lists inside a list
from itertools import count
# For graphing
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import pandas as pd
from pandas.tseries.offsets import *
# Setting Plot Size
plt.rcParams["figure.figsize"] = (8,3)
# SymPy for writing polynomial formula for determining the slopes
import sympy as smp
# Calling stock values from Yahoo finance API
# Ref. https://www.kaggle.com/code/alessandrozanette/s-p500-analysis-using-yfinance-data
## Calling API from Yahoo Finance: Uncomment the line "!pip install yfinance" if not done
# !pip install yfinance # Uncomment it to run if not installed yet in a different cell separated from this one.
import yfinance as yf
%config InlineBackend.figure_format='retina'
import warnings
warnings.filterwarnings("ignore")
' ######### Calling Yahoo Finance dataset with API ######### '
' ######### Calling Yahoo Finance dataset with API ######### '
def Dataset(Ticker_stock, No_Years_Data):
# Parameter Adjustment
# Set Ticker Symbol
# e.g. Ticker="^GSPC" for S&P 500, Ticker="NDAQ" for NASDAQ, Ticker="TOPX" for TOPIX
# Ticker="AMZN" for Amazon.com Inc., "AAPL" for Apple Inc., Ticker="NTDOY" for NINTENDO
Ticker_stock=Ticker_stock # Ticker symbok for a stock
# Set the number of years to download data
No_Years_Data=No_Years_Data;
if Ticker_stock==0:
# In case cannot download from yahoo API
# Copied dataset of GOLD on 18th Februrary 2025 #
t_j=['2020-02-19','2020-02-20','2020-02-21','2020-02-24','2020-02-25','2020-02-26','2020-02-27','2020-02-28','2020-03-02','2020-03-03','2020-03-04','2020-03-05','2020-03-06','2020-03-09','2020-03-10','2020-03-11','2020-03-12','2020-03-13','2020-03-16','2020-03-17','2020-03-18','2020-03-19','2020-03-20','2020-03-23','2020-03-24','2020-03-25','2020-03-26','2020-03-27','2020-03-30','2020-03-31','2020-04-01','2020-04-02','2020-04-03','2020-04-06','2020-04-07','2020-04-08','2020-04-09','2020-04-13','2020-04-14','2020-04-15','2020-04-16','2020-04-17','2020-04-20','2020-04-21','2020-04-22','2020-04-23','2020-04-24','2020-04-27','2020-04-28','2020-04-29','2020-04-30','2020-05-01','2020-05-04','2020-05-05','2020-05-06','2020-05-07','2020-05-08','2020-05-11','2020-05-12','2020-05-13','2020-05-14','2020-05-15','2020-05-18','2020-05-19','2020-05-20','2020-05-21','2020-05-22','2020-05-26','2020-05-27','2020-05-28','2020-05-29','2020-06-01','2020-06-02','2020-06-03','2020-06-04','2020-06-05','2020-06-08','2020-06-09','2020-06-10','2020-06-11','2020-06-12','2020-06-15','2020-06-16','2020-06-17','2020-06-18','2020-06-19','2020-06-22','2020-06-23','2020-06-24','2020-06-25','2020-06-26','2020-06-29','2020-06-30','2020-07-01','2020-07-02','2020-07-06','2020-07-07','2020-07-08','2020-07-09','2020-07-10','2020-07-13','2020-07-14','2020-07-15','2020-07-16','2020-07-17','2020-07-20','2020-07-21','2020-07-22','2020-07-23','2020-07-24','2020-07-27','2020-07-28','2020-07-29','2020-07-30','2020-07-31','2020-08-03','2020-08-04','2020-08-05','2020-08-06','2020-08-07','2020-08-10','2020-08-11','2020-08-12','2020-08-13','2020-08-14','2020-08-17','2020-08-18','2020-08-19','2020-08-20','2020-08-21','2020-08-24','2020-08-25','2020-08-26','2020-08-27','2020-08-28','2020-08-31','2020-09-01','2020-09-02','2020-09-03','2020-09-04','2020-09-08','2020-09-09','2020-09-10','2020-09-11','2020-09-14','2020-09-15','2020-09-16','2020-09-17','2020-09-18','2020-09-21','2020-09-22','2020-09-23','2020-09-24','2020-09-25','2020-09-28','2020-09-29','2020-09-30','2020-10-01','2020-10-02','2020-10-05','2020-10-06','2020-10-07','2020-10-08','2020-10-09','2020-10-12','2020-10-13','2020-10-14','2020-10-15','2020-10-16','2020-10-19','2020-10-20','2020-10-21','2020-10-22','2020-10-23','2020-10-26','2020-10-27','2020-10-28','2020-10-29','2020-10-30','2020-11-02','2020-11-03','2020-11-04','2020-11-05','2020-11-06','2020-11-09','2020-11-10','2020-11-11','2020-11-12','2020-11-13','2020-11-16','2020-11-17','2020-11-18','2020-11-19','2020-11-20','2020-11-23','2020-11-24','2020-11-25','2020-11-27','2020-11-30','2020-12-01','2020-12-02','2020-12-03','2020-12-04','2020-12-07','2020-12-08','2020-12-09','2020-12-10','2020-12-11','2020-12-14','2020-12-15','2020-12-16','2020-12-17','2020-12-18','2020-12-21','2020-12-22','2020-12-23','2020-12-24','2020-12-28','2020-12-29','2020-12-30','2020-12-31','2021-01-04','2021-01-05','2021-01-06','2021-01-07','2021-01-08','2021-01-11','2021-01-12','2021-01-13','2021-01-14','2021-01-15','2021-01-19','2021-01-20','2021-01-21','2021-01-22','2021-01-25','2021-01-26','2021-01-27','2021-01-28','2021-01-29','2021-02-01','2021-02-02','2021-02-03','2021-02-04','2021-02-05','2021-02-08','2021-02-09','2021-02-10','2021-02-11','2021-02-12','2021-02-16','2021-02-17','2021-02-18','2021-02-19','2021-02-22','2021-02-23','2021-02-24','2021-02-25','2021-02-26','2021-03-01','2021-03-02','2021-03-03','2021-03-04','2021-03-05','2021-03-08','2021-03-09','2021-03-10','2021-03-11','2021-03-12','2021-03-15','2021-03-16','2021-03-17','2021-03-18','2021-03-19','2021-03-22','2021-03-23','2021-03-24','2021-03-25','2021-03-26','2021-03-29','2021-03-30','2021-03-31','2021-04-01','2021-04-05','2021-04-06','2021-04-07','2021-04-08','2021-04-09','2021-04-12','2021-04-13','2021-04-14','2021-04-15','2021-04-16','2021-04-19','2021-04-20','2021-04-21','2021-04-22','2021-04-23','2021-04-26','2021-04-27','2021-04-28','2021-04-29','2021-04-30','2021-05-03','2021-05-04','2021-05-05','2021-05-06','2021-05-07','2021-05-10','2021-05-11','2021-05-12','2021-05-13','2021-05-14','2021-05-17','2021-05-18','2021-05-19','2021-05-20','2021-05-21','2021-05-24','2021-05-25','2021-05-26','2021-05-27','2021-05-28','2021-06-01','2021-06-02','2021-06-03','2021-06-04','2021-06-07','2021-06-08','2021-06-09','2021-06-10','2021-06-11','2021-06-14','2021-06-15','2021-06-16','2021-06-17','2021-06-18','2021-06-21','2021-06-22','2021-06-23','2021-06-24','2021-06-25','2021-06-28','2021-06-29','2021-06-30','2021-07-01','2021-07-02','2021-07-06','2021-07-07','2021-07-08','2021-07-09','2021-07-12','2021-07-13','2021-07-14','2021-07-15','2021-07-16','2021-07-19','2021-07-20','2021-07-21','2021-07-22','2021-07-23','2021-07-26','2021-07-27','2021-07-28','2021-07-29','2021-07-30','2021-08-02','2021-08-03','2021-08-04','2021-08-05','2021-08-06','2021-08-09','2021-08-10','2021-08-11','2021-08-12','2021-08-13','2021-08-16','2021-08-17','2021-08-18','2021-08-19','2021-08-20','2021-08-23','2021-08-24','2021-08-25','2021-08-26','2021-08-27','2021-08-30','2021-08-31','2021-09-01','2021-09-02','2021-09-03','2021-09-07','2021-09-08','2021-09-09','2021-09-10','2021-09-13','2021-09-14','2021-09-15','2021-09-16','2021-09-17','2021-09-20','2021-09-21','2021-09-22','2021-09-23','2021-09-24','2021-09-27','2021-09-28','2021-09-29','2021-09-30','2021-10-01','2021-10-04','2021-10-05','2021-10-06','2021-10-07','2021-10-08','2021-10-11','2021-10-12','2021-10-13','2021-10-14','2021-10-15','2021-10-18','2021-10-19','2021-10-20','2021-10-21','2021-10-22','2021-10-25','2021-10-26','2021-10-27','2021-10-28','2021-10-29','2021-11-01','2021-11-02','2021-11-03','2021-11-04','2021-11-05','2021-11-08','2021-11-09','2021-11-10','2021-11-11','2021-11-12','2021-11-15','2021-11-16','2021-11-17','2021-11-18','2021-11-19','2021-11-22','2021-11-23','2021-11-24','2021-11-26','2021-11-29','2021-11-30','2021-12-01','2021-12-02','2021-12-03','2021-12-06','2021-12-07','2021-12-08','2021-12-09','2021-12-10','2021-12-13','2021-12-14','2021-12-15','2021-12-16','2021-12-17','2021-12-20','2021-12-21','2021-12-22','2021-12-23','2021-12-27','2021-12-28','2021-12-29','2021-12-30','2021-12-31','2022-01-03','2022-01-04','2022-01-05','2022-01-06','2022-01-07','2022-01-10','2022-01-11','2022-01-12','2022-01-13','2022-01-14','2022-01-18','2022-01-19','2022-01-20','2022-01-21','2022-01-24','2022-01-25','2022-01-26','2022-01-27','2022-01-28','2022-01-31','2022-02-01','2022-02-02','2022-02-03','2022-02-04','2022-02-07','2022-02-08','2022-02-09','2022-02-10','2022-02-11','2022-02-14','2022-02-15','2022-02-16','2022-02-17','2022-02-18','2022-02-22','2022-02-23','2022-02-24','2022-02-25','2022-02-28','2022-03-01','2022-03-02','2022-03-03','2022-03-04','2022-03-07','2022-03-08','2022-03-09','2022-03-10','2022-03-11','2022-03-14','2022-03-15','2022-03-16','2022-03-17','2022-03-18','2022-03-21','2022-03-22','2022-03-23','2022-03-24','2022-03-25','2022-03-28','2022-03-29','2022-03-30','2022-03-31','2022-04-01','2022-04-04','2022-04-05','2022-04-06','2022-04-07','2022-04-08','2022-04-11','2022-04-12','2022-04-13','2022-04-14','2022-04-18','2022-04-19','2022-04-20','2022-04-21','2022-04-22','2022-04-25','2022-04-26','2022-04-27','2022-04-28','2022-04-29','2022-05-02','2022-05-03','2022-05-04','2022-05-05','2022-05-06','2022-05-09','2022-05-10','2022-05-11','2022-05-12','2022-05-13','2022-05-16','2022-05-17','2022-05-18','2022-05-19','2022-05-20','2022-05-23','2022-05-24','2022-05-25','2022-05-26','2022-05-27','2022-05-31','2022-06-01','2022-06-02','2022-06-03','2022-06-06','2022-06-07','2022-06-08','2022-06-09','2022-06-10','2022-06-13','2022-06-14','2022-06-15','2022-06-16','2022-06-17','2022-06-21','2022-06-22','2022-06-23','2022-06-24','2022-06-27','2022-06-28','2022-06-29','2022-06-30','2022-07-01','2022-07-05','2022-07-06','2022-07-07','2022-07-08','2022-07-11','2022-07-12','2022-07-13','2022-07-14','2022-07-15','2022-07-18','2022-07-19','2022-07-20','2022-07-21','2022-07-22','2022-07-25','2022-07-26','2022-07-27','2022-07-28','2022-07-29','2022-08-01','2022-08-02','2022-08-03','2022-08-04','2022-08-05','2022-08-08','2022-08-09','2022-08-10','2022-08-11','2022-08-12','2022-08-15','2022-08-16','2022-08-17','2022-08-18','2022-08-19','2022-08-22','2022-08-23','2022-08-24','2022-08-25','2022-08-26','2022-08-29','2022-08-30','2022-08-31','2022-09-01','2022-09-02','2022-09-06','2022-09-07','2022-09-08','2022-09-09','2022-09-12','2022-09-13','2022-09-14','2022-09-15','2022-09-16','2022-09-19','2022-09-20','2022-09-21','2022-09-22','2022-09-23','2022-09-26','2022-09-27','2022-09-28','2022-09-29','2022-09-30','2022-10-03','2022-10-04','2022-10-05','2022-10-06','2022-10-07','2022-10-10','2022-10-11','2022-10-12','2022-10-13','2022-10-14','2022-10-17','2022-10-18','2022-10-19','2022-10-20','2022-10-21','2022-10-24','2022-10-25','2022-10-26','2022-10-27','2022-10-28','2022-10-31','2022-11-01','2022-11-02','2022-11-03','2022-11-04','2022-11-07','2022-11-08','2022-11-09','2022-11-10','2022-11-11','2022-11-14','2022-11-15','2022-11-16','2022-11-17','2022-11-18','2022-11-21','2022-11-22','2022-11-23','2022-11-25','2022-11-28','2022-11-29','2022-11-30','2022-12-01','2022-12-02','2022-12-05','2022-12-06','2022-12-07','2022-12-08','2022-12-09','2022-12-12','2022-12-13','2022-12-14','2022-12-15','2022-12-16','2022-12-19','2022-12-20','2022-12-21','2022-12-22','2022-12-23','2022-12-27','2022-12-28','2022-12-29','2022-12-30','2023-01-03','2023-01-04','2023-01-05','2023-01-06','2023-01-09','2023-01-10','2023-01-11','2023-01-12','2023-01-13','2023-01-17','2023-01-18','2023-01-19','2023-01-20','2023-01-23','2023-01-24','2023-01-25','2023-01-26','2023-01-27','2023-01-30','2023-01-31','2023-02-01','2023-02-02','2023-02-03','2023-02-06','2023-02-07','2023-02-08','2023-02-09','2023-02-10','2023-02-13','2023-02-14','2023-02-15','2023-02-16','2023-02-17','2023-02-21','2023-02-22','2023-02-23','2023-02-24','2023-02-27','2023-02-28','2023-03-01','2023-03-02','2023-03-03','2023-03-06','2023-03-07','2023-03-08','2023-03-09','2023-03-10','2023-03-13','2023-03-14','2023-03-15','2023-03-16','2023-03-17','2023-03-20','2023-03-21','2023-03-22','2023-03-23','2023-03-24','2023-03-27','2023-03-28','2023-03-29','2023-03-30','2023-03-31','2023-04-03','2023-04-04','2023-04-05','2023-04-06','2023-04-10','2023-04-11','2023-04-12','2023-04-13','2023-04-14','2023-04-17','2023-04-18','2023-04-19','2023-04-20','2023-04-21','2023-04-24','2023-04-25','2023-04-26','2023-04-27','2023-04-28','2023-05-01','2023-05-02','2023-05-03','2023-05-04','2023-05-05','2023-05-08','2023-05-09','2023-05-10','2023-05-11','2023-05-12','2023-05-15','2023-05-16','2023-05-17','2023-05-18','2023-05-19','2023-05-22','2023-05-23','2023-05-24','2023-05-25','2023-05-26','2023-05-30','2023-05-31','2023-06-01','2023-06-02','2023-06-05','2023-06-06','2023-06-07','2023-06-08','2023-06-09','2023-06-12','2023-06-13','2023-06-14','2023-06-15','2023-06-16','2023-06-20','2023-06-21','2023-06-22','2023-06-23','2023-06-26','2023-06-27','2023-06-28','2023-06-29','2023-06-30','2023-07-03','2023-07-05','2023-07-06','2023-07-07','2023-07-10','2023-07-11','2023-07-12','2023-07-13','2023-07-14','2023-07-17','2023-07-18','2023-07-19','2023-07-20','2023-07-21','2023-07-24','2023-07-25','2023-07-26','2023-07-27','2023-07-28','2023-07-31','2023-08-01','2023-08-02','2023-08-03','2023-08-04','2023-08-07','2023-08-08','2023-08-09','2023-08-10','2023-08-11','2023-08-14','2023-08-15','2023-08-16','2023-08-17','2023-08-18','2023-08-21','2023-08-22','2023-08-23','2023-08-24','2023-08-25','2023-08-28','2023-08-29','2023-08-30','2023-08-31','2023-09-01','2023-09-05','2023-09-06','2023-09-07','2023-09-08','2023-09-11','2023-09-12','2023-09-13','2023-09-14','2023-09-15','2023-09-18','2023-09-19','2023-09-20','2023-09-21','2023-09-22','2023-09-25','2023-09-26','2023-09-27','2023-09-28','2023-09-29','2023-10-02','2023-10-03','2023-10-04','2023-10-05','2023-10-06','2023-10-09','2023-10-10','2023-10-11','2023-10-12','2023-10-13','2023-10-16','2023-10-17','2023-10-18','2023-10-19','2023-10-20','2023-10-23','2023-10-24','2023-10-25','2023-10-26','2023-10-27','2023-10-30','2023-10-31','2023-11-01','2023-11-02','2023-11-03','2023-11-06','2023-11-07','2023-11-08','2023-11-09','2023-11-10','2023-11-13','2023-11-14','2023-11-15','2023-11-16','2023-11-17','2023-11-20','2023-11-21','2023-11-22','2023-11-24','2023-11-27','2023-11-28','2023-11-29','2023-11-30','2023-12-01','2023-12-04','2023-12-05','2023-12-06','2023-12-07','2023-12-08','2023-12-11','2023-12-12','2023-12-13','2023-12-14','2023-12-15','2023-12-18','2023-12-19','2023-12-20','2023-12-21','2023-12-22','2023-12-26','2023-12-27','2023-12-28','2023-12-29','2024-01-02','2024-01-03','2024-01-04','2024-01-05','2024-01-08','2024-01-09','2024-01-10','2024-01-11','2024-01-12','2024-01-16','2024-01-17','2024-01-18','2024-01-19','2024-01-22','2024-01-23','2024-01-24','2024-01-25','2024-01-26','2024-01-29','2024-01-30','2024-01-31','2024-02-01','2024-02-02','2024-02-05','2024-02-06','2024-02-07','2024-02-08','2024-02-09','2024-02-12','2024-02-13','2024-02-14','2024-02-15','2024-02-16','2024-02-20','2024-02-21','2024-02-22','2024-02-23','2024-02-26','2024-02-27','2024-02-28','2024-02-29','2024-03-01','2024-03-04','2024-03-05','2024-03-06','2024-03-07','2024-03-08','2024-03-11','2024-03-12','2024-03-13','2024-03-14','2024-03-15','2024-03-18','2024-03-19','2024-03-20','2024-03-21','2024-03-22','2024-03-25','2024-03-26','2024-03-27','2024-03-28','2024-04-01','2024-04-02','2024-04-03','2024-04-04','2024-04-05','2024-04-08','2024-04-09','2024-04-10','2024-04-11','2024-04-12','2024-04-15','2024-04-16','2024-04-17','2024-04-18','2024-04-19','2024-04-22','2024-04-23','2024-04-24','2024-04-25','2024-04-26','2024-04-29','2024-04-30','2024-05-01','2024-05-02','2024-05-03','2024-05-06','2024-05-07','2024-05-08','2024-05-09','2024-05-10','2024-05-13','2024-05-14','2024-05-15','2024-05-16','2024-05-17','2024-05-20','2024-05-21','2024-05-22','2024-05-23','2024-05-24','2024-05-28','2024-05-29','2024-05-30','2024-05-31','2024-06-03','2024-06-04','2024-06-05','2024-06-06','2024-06-07','2024-06-10','2024-06-11','2024-06-12','2024-06-13','2024-06-14','2024-06-17','2024-06-18','2024-06-20','2024-06-21','2024-06-24','2024-06-25','2024-06-26','2024-06-27','2024-06-28','2024-07-01','2024-07-02','2024-07-03','2024-07-05','2024-07-08','2024-07-09','2024-07-10','2024-07-11','2024-07-12','2024-07-15','2024-07-16','2024-07-17','2024-07-18','2024-07-19','2024-07-22','2024-07-23','2024-07-24','2024-07-25','2024-07-26','2024-07-29','2024-07-30','2024-07-31','2024-08-01','2024-08-02','2024-08-05','2024-08-06','2024-08-07','2024-08-08','2024-08-09','2024-08-12','2024-08-13','2024-08-14','2024-08-15','2024-08-16','2024-08-19','2024-08-20','2024-08-21','2024-08-22','2024-08-23','2024-08-26','2024-08-27','2024-08-28','2024-08-29','2024-08-30','2024-09-03','2024-09-04','2024-09-05','2024-09-06','2024-09-09','2024-09-10','2024-09-11','2024-09-12','2024-09-13','2024-09-16','2024-09-17','2024-09-18','2024-09-19','2024-09-20','2024-09-23','2024-09-24','2024-09-25','2024-09-26','2024-09-27','2024-09-30','2024-10-01','2024-10-02','2024-10-03','2024-10-04','2024-10-07','2024-10-08','2024-10-09','2024-10-10','2024-10-11','2024-10-14','2024-10-15','2024-10-16','2024-10-17','2024-10-18','2024-10-21','2024-10-22','2024-10-23','2024-10-24','2024-10-25','2024-10-28','2024-10-29','2024-10-30','2024-10-31','2024-11-01','2024-11-04','2024-11-05','2024-11-06','2024-11-07','2024-11-08','2024-11-11','2024-11-12','2024-11-13','2024-11-14','2024-11-15','2024-11-18','2024-11-19','2024-11-20','2024-11-21','2024-11-22','2024-11-25','2024-11-26','2024-11-27','2024-11-29','2024-12-02','2024-12-03','2024-12-04','2024-12-05','2024-12-06','2024-12-09','2024-12-10','2024-12-11','2024-12-12','2024-12-13','2024-12-16','2024-12-17','2024-12-18','2024-12-19','2024-12-20','2024-12-23','2024-12-24','2024-12-26','2024-12-27','2024-12-30','2024-12-31','2025-01-02','2025-01-03','2025-01-06','2025-01-07','2025-01-08','2025-01-10','2025-01-13','2025-01-14','2025-01-15','2025-01-16','2025-01-17','2025-01-21','2025-01-22','2025-01-23','2025-01-24','2025-01-27','2025-01-28','2025-01-29','2025-01-30','2025-01-31','2025-02-03','2025-02-04','2025-02-05','2025-02-06','2025-02-07','2025-02-10','2025-02-11','2025-02-12','2025-02-13','2025-02-14','2025-02-18']
t_j=[ datetime.strptime( t_j[j], "%Y-%m-%d" ).date() for j in range(len(t_j))]
y_t=['21','20.77','21.44','22.03','21.25','21.09','19.78','19.04','19.86','20.62','20.55','21.16','21.23','19.93','19.87','18.79','16.88','15.67','16.33','17.71','15.88','16.08','15.88','16.93','19.5','19.11','19.58','19.24','19.29','18.32','18.87','19.85','19.95','20.55','20.31','20.41','22.51','24.37','24.43','24.47','25','24.54','24.92','24.82','26.48','26.74','27.1','26.93','26.96','26.55','25.72','26.93','27.38','28.19','26.56','27.26','27.39','26.02','26.04','26.12','27.14','28.04','27.2','28.1','27.25','26.26','26.32','24.73','23.92','23.95','24','24.78','23.95','23.22','23.43','23.29','23.56','24.33','25.08','24.31','24.07','24.74','24.38','24.25','23.94','24.49','25.45','25.68','25.71','25.73','25.87','26.13','26.81','26.81','26.27','26.61','27.02','27.72','27.59','27.11','26.13','27.05','27.09','26.69','27.34','28.12','28.23','28.52','27.99','28.44','29.91','29.59','28.78','28.25','28.91','28.72','29.75','29.84','29.5','28.87','28.72','26.25','26.18','27.14','26.99','30.13','30.04','28.98','29.95','29.31','28.97','28.75','29.37','28.58','29.2','29.65','29.33','29.61','29.26','29.05','29.2','30.46','30.11','29.75','29.93','29.99','29.99','29.14','28.89','28.19','28.42','27.14','27.65','27.7','27.76','28.28','28.11','28.26','27.89','28.37','27.06','26.95','27.27','28.05','28.04','27.96','28.31','27.75','27.57','27.02','27.07','27.37','26.89','26.81','26.63','27.09','25.92','26.17','26.73','27.16','27.78','27.42','29.32','28.87','26.9','25.94','25.79','25.91','25.89','25.86','25.37','24.46','24.18','24.28','23.24','22.79','22.83','22.69','23.14','23.83','23.96','23.61','23.5','23.95','23.9','23.36','23.09','22.9','22.44','22.87','23.07','23.59','23.27','23.02','22.52','22.83','22.9','22.84','22.91','23.24','22.78','24.62','24.28','24.5','24.77','23.97','23.52','23.54','23.6','23.68','23.14','23.39','23.83','23.68','23.47','23.44','23.28','21.99','22.36','22.37','22.54','22.34','22.34','21.99','22.33','22.46','22.43','22.51','22.19','22.15','21.7','20.83','20.32','19.78','20.53','20.16','20.1','19.45','18.67','18.97','19.86','19.5','19.54','19.84','19.5','19.99','20.12','20.31','20.32','20.77','20.71','21.13','20.86','20.96','20.62','20.24','20.16','19.73','20','20.16','19.51','19.8','20.37','20.63','20.88','20.67','21.16','21.21','20.99','21.1','21.11','21.98','22.23','22.11','22.4','22.74','22.41','22.21','22.16','21.64','21.76','21.35','21.25','22.23','22.01','22.25','23.55','23.92','23.93','23.95','23.53','23.58','24.02','25.27','25.11','24.59','24.81','24.7','24.68','24.58','24.58','23.86','24.08','24.26','24.17','23.21','23.46','23.41','23.12','23.01','23.71','23.23','23.15','22.79','22.45','21.03','20.75','21.15','21.08','20.96','20.97','20.89','20.7','20.53','20.68','20.58','20.79','21.29','21.19','20.72','21.13','20.83','21.14','21.33','21.27','20.86','20.54','20.72','20.9','20.74','20.63','20.82','20.99','21.34','21.81','21.77','21.57','21.72','21.65','21.26','20.8','20.56','19.84','20.31','20.05','20.27','20.19','19.97','19.46','19.13','19.23','20.15','20.12','19.85','19.73','20.24','19.81','20.07','19.81','19.87','20.3','19.85','19.86','19.69','19.35','19.47','19.5','19.36','18.51','18.36','18.65','18.49','18.45','18.23','18.03','17.95','18.1','17.58','18.05','17.99','18.23','18.15','18.45','18.59','18.52','18.44','18.66','19.23','19.57','19.13','19.07','19.31','19.42','19.37','19.48','19.55','19.43','19.39','18.94','18.37','18.48','18.47','18.62','18.75','19.6','19.48','19.62','20.46','20.65','20.75','20.9','20.6','20.99','20.56','20.25','19.79','19.43','19.39','19.38','19.15','18.99','18.39','18.18','18.22','18.49','18.56','18.6','18.17','17.95','17.97','17.86','17.65','18.44','18.39','18.33','18.39','18.53','18.57','18.41','18.37','18.4','18.82','19','18.54','18.73','18.57','18.02','18.1','18.62','18.86','19.2','18.83','18.68','18.56','20.17','19.91','19.34','19.37','19.49','19.03','18.71','18.56','19.15','19.09','19.22','19.06','19.19','19.58','19.82','19.73','19.3','20.68','21.09','20.68','22.23','23.23','23.14','22.76','22.83','22.54','22.56','22.57','23.46','23.15','23.57','24.2','24.86','25.36','24.33','24.49','24.35','23.64','23.77','23.61','24.08','23.87','24.3','24.23','24.57','24.5','24.54','24.23','24.53','24.85','24.53','24.94','24.86','24.46','24.61','24.9','25.41','25.4','25.44','25.58','25.2','25.38','25.01','25.25','24.22','23.73','22.73','22.37','22.24','22.43','22.31','22.16','22.41','23.19','22.56','22.42','21.49','21.29','21.23','20.34','20.43','20.52','20.47','19.75','20.72','20.76','20.73','21.31','21.25','20.63','20.6','20.49','20.35','20.97','20.68','20.51','20.74','20.77','20.09','21.03','20.12','19.47','19.56','20.03','19.55','19.58','19.4','18.55','18.78','19.04','18.55','18.39','17.69','18.06','17.42','17.22','17.25','17.18','16.98','16.44','16.49','15.77','15.66','15.82','16.03','15.45','15.5','15.33','14.91','15.27','15.41','15.63','15.74','15.72','15.52','15.2','15.78','15.6','16.2','16.44','16.53','16.4','16.74','16.67','16.85','16.45','16.41','16.07','16.17','16.21','16.35','16.37','15.48','15.35','14.99','14.85','14.59','14.9','14.83','15.36','15.61','15.92','15.96','15.55','15.6','15.14','15.3','15.5','15.05','15.18','15.07','14.48','14.19','14.1','14.95','15.1','15.5','15.92','16.05','15.9','16.15','15.48','15.27','15.01','15.06','14.67','14.16','14.61','14.79','14.45','14.51','15.01','15.05','15.26','15.7','15.48','15.31','15.03','15.14','14.34','13.1','14.2','14.57','15.61','15.35','16.43','16.44','16.19','16','15.79','15.57','15.62','15.35','16.01','16.23','16.02','15.5','15.87','16.32','17.09','16.98','16.52','16.45','16.86','16.8','16.58','16.73','17.44','17.39','16.65','16.9','16.55','17.28','17.5','17.3','17.39','17.77','17.21','17.25','17.18','17.83','18.88','18.78','19.04','18.97','19.39','19.22','19.43','19.64','18.75','18.38','18.72','18.97','19.23','19.42','19.79','19.69','19.67','19.48','19.55','19.95','19.4','18.32','18.35','18.5','18.34','17.81','17.93','17.93','17.81','17.17','16.95','16.74','16.59','16.21','16.07','16','15.97','16.12','16.31','16.38','16.52','16.3','15.77','15.62','15.57','15.91','17.02','17.07','17.48','17.4','18.12','18.24','17.69','18','18.3','18.52','18.5','18.66','18.54','18.6','18.57','18.79','19.62','19.68','19.73','19.46','19.42','19.63','20.05','19.67','19.37','19.42','19.24','19.2','19.07','19.07','19.19','18.99','19.17','19.04','19','19.79','19.98','20.43','20.2','19.85','19.78','19.77','19.03','19.18','19.35','18.76','18.43','17.81','17.86','17.79','17.75','17.48','17.09','16.99','16.78','16.88','17.45','17.31','17.19','17.13','16.98','17.15','17','16.9','16.81','16.78','16.49','16.71','16.05','16.13','16.41','16.46','16.57','16.66','16.6','16.63','16.93','17.09','16.7','16.22','16.34','16.53','16.71','17.48','17.6','17.35','17.36','17.65','17.65','17.28','17.4','17.38','17.53','17.25','16.78','16.98','17.29','17.03','16.54','16.31','16.49','16.5','16.45','16.48','16.66','16.91','16.67','16.02','15.77','15.66','15.61','15.71','15.84','16.11','15.97','15.91','16.19','16.34','16.35','16.21','16.12','15.92','15.84','15.79','15.78','16.07','16.05','16.12','16.33','16.64','16.68','16.33','16.42','16.03','15.79','15.55','15.21','14.64','14.56','14.55','14.23','14.19','14.2','14.37','14.58','14.85','14.96','15.22','15.14','15.74','15.81','16.42','16.54','16.61','16.89','16.55','16.56','16.58','16.3','16.33','16.11','15.98','16.03','16.05','16.48','16.29','15.76','15.17','15.3','15.22','15.13','15.71','15.61','15.88','15.71','15.79','16.19','16.24','16.23','16.57','17.44','17.39','17.59','17.73','17.53','17.42','17.36','17.22','16.79','16.67','16.34','17.43','17.65','17.51','17.73','18.09','17.77','17.97','18.17','18.17','18.47','18.09','18.09','17.88','17.33','17.35','17.49','17.69','17.4','17.34','17.15','17.64','15.96','15.56','15.53','15.61','15.59','15.99','15.53','15.59','15.53','15.7','15.68','15.6','15.89','15.41','15.17','15.03','14.93','14.82','14.67','14.73','14.15','14.06','14.6','14.65','14.73','14.75','14.39','14.74','14.65','14.51','14.33','14.66','14.94','15.46','15.53','15.71','15.9','15.76','15.87','15.64','15.98','15.76','15.76','15.63','15.25','15.68','15.81','15.52','15.57','15.52','16.21','16.64','16.86','17.18','17.42','17.39','17.86','17.7','18.01','17.9','18.04','17.86','17.37','16.48','16.7','16.98','17.1','16.42','16.69','16.52','17.08','17.09','17.32','16.64','16.36','16.55','16.46','16.67','16.74','16.72','17.03','16.93','16.94','17.15','17.42','17.52','17.89','17.92','17.8','17.29','16.94','17.02','17.44','16.92','17.11','17.09','17.42','16.49','16.7','17.32','16.15','16.45','16.18','16.4','16.07','16.03','16.14','16.41','16.96','16.56','16.72','16.59','16.8','16.84','16.68','16.56','16.62','17.3','17.44','17.38','17.44','17.78','18.13','18.52','18.36','19.25','18.88','18.54','18.39','18.36','18.57','18.46','17.72','17.79','17.97','18.14','18.51','18.24','17.91','17.18','17.39','16.81','17.34','17.41','18.99','19.13','19.16','19.05','19.71','20.21','20.41','20.62','20.24','20.5','20.47','20.45','19.95','20.32','20.19','19.6','19.32','19.46','19','19.28','19.6','19.76','20.58','20.84','20.52','20.48','20.06','20.23','20.38','20.21','20.43','20.7','21.01','20.23','19.89','20.3','20.18','19.85','20.11','19.88','19.66','19.66','20.07','19.99','19.98','20.21','19.94','20.34','20.92','20.86','21.19','20.8','20.32','19.61','19.98','20.28','20.05','19.32','19.05','18.9','18.88','18.43','18.48','18.4','17.4','17.02','16.79','16.85','16.65','17.2','17.72','17.89','18.12','18.22','17.68','17.56','17.57','17.49','17.21','17.61','17.37','16.95','16.77','17.08','17.14','17.63','17.17','16.78','16.35','16.24','15.51','15.23','15.46','15.59','15.71','15.74','15.68','15.46','15.5','15.97','15.84','15.5','15.57','15.84','15.72','15.45','15.58','15.84','15.75','16','15.98','15.77','15.82','16.13','16.06','15.96','15.96','16.57','16.37','16.56','16.68','17.15','17.18','17.04','17.44','17.07','18.17','18.44','17.94','18.36']
y_t=[float(y_t[j]) for j in range(len(y_t))]
y_t=np.flip(y_t)
else:
# Now, let's retrieve the data of the past x years using yfinance
Years="".join([str(No_Years_Data),'y']) # Set the number of years ! Adjust
y_t = yf.Ticker(Ticker_stock).history(period=Years) # Ticker Symbokl ! Adjust !
# Another way to dl data
# y_t = yf.download(Ticker_stock, start="2015-01-01", end="2023-12-31")
# Let's take a look at the called data
#display(y_t.tail())
# Change to list
# https://stackoverflow.com/questions/39597553/from-datetimeindex-to-list-of-times
t_j=y_t.index.tolist()
t_j=[t_j[j].strftime("%Y-%m-%d") for j in range(len(t_j))]
# change from numpy array to list
y_t=y_t["Close"].tolist()
# # Plotting the data y_t with its date t
# for k in range(No_Years_Data*4):
# Window=int(math.floor(len(t)/No_Years_Data)/4)
# St=k*Window; Ed=St+Window; print(St,Ed)
# plt.figure(); plt.plot(t[St:Ed],y_t[St:Ed]); plt.xticks(rotation=90); plt.show()
## Linear Interpolation of data
# https://stackoverflow.com/questions/2315032/how-do-i-find-missing-dates-in-a-list-of-sorted-dates
# https://stackoverflow.com/questions/13019719/get-business-days-between-start-and-end-date-using-pandas
t_raw=t_j.copy()
t_intpl = pd.date_range(start=t_raw[0], end=t_raw[-1], freq=BDay()) # Interpolating only for business days
y_raw=y_t.copy()
y_intpl = np.interp(pd.to_datetime(t_intpl).astype(int), pd.to_datetime(t_raw).astype(int), y_raw)
t_j=t_intpl.copy(); y_t=y_intpl.copy()
t_j=t_j.tolist()
t_j=[t_j[j].strftime("%Y-%m-%d") for j in range(len(t_j))]
t_j=np.array(t_j)
return t_j, y_t
' ######### Calling dataset to analyse ######### '
# Example of Barrick Gold Corp
# Parameter Adjustment
# Set Ticker Symbol
# e.g. Ticker="^GSPC" for S&P 500, Ticker="NDAQ" for NASDAQ, Ticker="^N225" for Nikkei
# Ticker="AMZN" for Amazon.com Inc., "AAPL" for Apple Inc., Ticker="NTDOY" for NINTENDO
Ticker_stock="GOLD" # Ticker symbok for a stock
Ticker_stock=0 # In case Yahoo Finance API does not work
# Set the number of years to download data
No_Years_Data=5;
# Output
t_j, y_t = Dataset(Ticker_stock, 2)
N=len(y_t); j=list(range(0,N)) # Generating the index label
y_j=y_t.copy() # Copying the main variable with the different subscript name
plt.figure();plt.plot(t_j, y_t, color='lightsteelblue'); plt.show(block=False)
' ######### Defining the function for displaying animation ######### '
def MoveStepByStep(f_j,Title):
N_=len(f_j)
plt.figure()
plt.rcParams["animation.html"] = "jshtml"
plt.rcParams['figure.dpi'] = 150
plt.ioff()
fig, ax = plt.subplots()
count_ = count(); NoFrames=30; StepSize=math.floor(N_/NoFrames);
plt.figure();
def animate(a):
counts=next(count_); Show=counts*StepSize
ax.cla()
ax.set_title(Title);
ax.plot(j[0:Show],f_j[0:Show], label=Title, color='darkseagreen')
ax.plot(p,f_p, "*", color='Magenta', label="Lower Peaks", markersize=9);
ax.set_xlim(0,N_)
ax.set_ylim(min(f_j)*0.9,max(f_j)*1.1)
Animation=animation.FuncAnimation(fig, animate, frames=NoFrames)
display(Animation)
# Separating figures
plt.show(block=False)
' ######### Defining the algebraic symbols for SymPy ######### '
print('The algorithm for PCHIP and Cubic Spline Interpolation refers to')
print('C. Moler, “Numerical computing with matlab.” SIAM, Philadelphia, 2004.')
# SymPy: Defininig symbols
_zero=smp.symbols('0'); _N=smp.symbols('N')
_j=smp.symbols('j',real=True, positive=True) # Time points
_p=smp.symbols('p',real=True, positive=True) # Interpolation bases
_t_p_1=smp.symbols('t_{p-1}',real=True); _t_p=smp.symbols('t_{p}',real=True); _t_p1=smp.symbols('t_{p+1}',real=True)
_t_j=smp.symbols('t_{j}',real=True,positive=True);
_omega_p=smp.symbols(r'\omega_{p}',real=True,positive=True); _omega_p_1=smp.symbols(r'\omega_{p-1}',real=True,positive=True); _
_omega=smp.symbols(r'\omega',real=True,positive=True);
_eta=smp.symbols(r'\eta',real=True,positive=True);
_y_p_1=smp.symbols('y_{p-1}',real=True); _y_p=smp.symbols('y_{p}',real=True); _y_p1=smp.symbols('y_{p+1}',real=True)
_y_j=smp.symbols('y_j',real=True);
_f_p_1=smp.symbols('f_{p-1}',real=True); _f_p=smp.symbols('f_{p}',real=True); _f_p1=smp.symbols('f_{p+1}',real=True)
_f_j=smp.symbols('f_j',real=True);
_fp_p=smp.symbols('f(p+)',real=True);_fn_p=smp.symbols('f(p-)',real=True)
_f_t=smp.symbols("f(t_j)",real=True);
_d1f_t=smp.symbols("f'(t_j)",real=True);_d2f_t=smp.symbols("f''(t_j)",real=True);
_d2fp_t=smp.symbols("f''(t_{p}+)",real=True);_d2fn_t=smp.symbols("f''(t_{p}-)",real=True);
_d2fn_t1=smp.symbols("f''(t_{p+1}-)",real=True);
_delta_p=smp.symbols(r'\delta_p',real=True); _delta_p_1=smp.symbols(r'\delta_{p-1}',real=True);
_delta_p1=smp.symbols(r'\delta_{p+1}',real=True); _delta_p_2=smp.symbols(r'\delta_{p-2}',real=True);
_d_p_1=smp.symbols('d_{p-1}',real=True);_d_p=smp.symbols('d_p',real=True);_d_p1=smp.symbols('d_{p+1}',real=True);
_d_j=smp.symbols('d_j',real=True)
_d1f_p=smp.symbols("f'_p",real=True);_d1fp_p=smp.symbols("f'(p+)",real=True);_d1fn_p=smp.symbols("f'(p-)",real=True)
_d2f_p=smp.symbols("f''_p",real=True);_d2fp_p=smp.symbols("f''(p+)",real=True);_d2fn_p=smp.symbols("f''(p-)",real=True)
display(smp.Eq(_omega,smp.Eq(_omega_p,_t_p1-_t_p)))
display(smp.Eq(_omega_p_1,_t_p-_t_p_1))
display(smp.Eq(_eta,_t_j-_t_p))
display(smp.Eq(_f_p,_y_p))
display(smp.Eq(_delta_p,(smp.Eq((_f_p1-_f_p)/(_t_p1-_t_p),(_f_p1-_f_p)/_omega_p))))
display(smp.Eq(_delta_p_1,(smp.Eq((_f_p-_f_p_1)/(_t_p-_t_p_1),(_f_p-_f_p_1)/_omega_p_1))))
display(smp.LessThan(_f_p, _f_j),smp.LessThan(_f_j,_f_p1))
' ######### Enveloping the lower peaks with the different interpolation methods ######### '
# Finding the lower peaks
p, _ = find_peaks(-y_j, prominence=1)
t_p=t_j[p]; y_p=y_j[p] # Saving values at the peak points
# Adding the first point
p=np.append(0,p); y_p=np.append(y_j[0],y_p); t_p=np.append(t_j[0],t_p);
plt.figure();
plt.plot(y_j, color='lightsteelblue', label="Original");
plt.plot(p, y_p, "*", color='Magenta', label="Lower Peaks", markersize=9);
plt.legend(loc="best")
plt.show(); plt.show(block=False)
# Adding the variable name for the lower envelope
f_p=y_p.copy()
f_j_list=[] # Storing the interpolation showcase.
Title_list=[] # Storing the interpolation methods' name
# Defininig function to output the lower envelope
def PlotLowerEnvelope(Title,p,y_j,f_j):
plt.figure();
plt.title(Title)
plt.plot(y_j, color='lightsteelblue', label="Original");
plt.plot(p, y_p, "*", color='Magenta', label="Lower Peaks", markersize=9);
plt.plot(f_j, color='darkgreen', label=Title);
plt.legend(loc="best")
plt.show(); plt.show(block=False)
# Linear interpolation (SymbPy)
display("For the lienar intepolation")
display(smp.Eq((_f_j-_f_p),(_f_p1-_f_p)/(_t_p1-_t_p)*(_t_j-_t_p)))
display(smp.Eq(_f_j,smp.Eq((_f_p1-_f_p)/(_t_p1-_t_p)*(_t_j-_t_p)+_f_p,(_f_p+_delta_p*_eta))))
# Linear interpolation
f_j_Linear = np.interp(pd.to_datetime(t_j).astype(int), pd.to_datetime(t_p).astype(int), f_p)
Title='Linear Interpolation'
f_j_list.append(f_j_Linear); Title_list.append(Title)
PlotLowerEnvelope(Title,p,y_j,f_j_Linear)
MoveStepByStep(f_j_Linear,Title)
# SymbPy: Piecewise Cubic Hermite Interpolating Polynomial (PCHIP)
display("For Piecewise Cubic Hermite Interpolating Polynomial (PCHIP)")
display(smp.Eq(_d_p, _d1f_p))
# SymbPy: PCHIP Derivatives
display("If δ of each side has a opposite sign from each other:"
,smp.Eq(_d_p,0))
display("If δ of both sides have a same sign but a same length of ω of both sides:"
,smp.Eq(1/_d_p,(1/2)*(1/_delta_p_1+1/_delta_p)))
_w_1=smp.symbols('w_1',real=True);_w_2=smp.symbols('w_2',real=True);
display("If δ of both sides have a same sign but a same length of ω of both sides:"
,smp.Eq(_w_1,2*_omega_p+_omega_p_1)
,smp.Eq(_w_2,_omega_p+2*_omega_p_1)
,smp.Eq((_w_1+_w_2)/_d_p,(_w_1/_delta_p_1+_w_2/_delta_p)))
# SymbPy: PCHIP Interpolated value
_f_j_interpolated=(((_omega*_eta**2 -2*_eta**2)/_omega**3)*_f_p1)+(((_omega**3-3*_omega*_eta**2+2*_eta**2)/_omega**3)*_f_p)+((_eta**2*(_eta-_omega))/_omega**2)*_d_p1+(_eta*(_eta-_omega)**2/_omega**2)*_d_p
display(smp.Eq(_f_j, _f_j_interpolated))
# Piecewise Cubic Hermite Interpolating Polynomial (PCHIP)
f_j_PCHIP = pchip_interpolate(p, f_p,j)
Title='PCHIP'
f_j_list.append(f_j_PCHIP); Title_list.append(Title)
PlotLowerEnvelope(Title,p,y_j,f_j_PCHIP)
MoveStepByStep(f_j_PCHIP,Title)
# SymbPy: Cubic Spline Interpolation
display("For Cubic Spline Interpolation ")
display(smp.Eq(_y_p,smp.Eq(_fp_p,_fn_p)))
display(smp.Eq(_d1fp_p,_d1fn_p)),display(smp.Eq(_d2fp_p,_d2fn_p))
display(smp.Eq(_d_p, _d1f_p))
display("Differentiate f in terms of fj in η",smp.Eq(_eta,_t_j-_t_p))
display(smp.Eq(_f_t,_f_j_interpolated))
_d1f_j_interpolate=(((6*_omega*_eta -6*_eta**2)/_omega**3)*_f_p1)+(((6*_eta**2-6*_eta*_omega)/_omega**3)*_f_p)+((3*_eta**2-2*_eta*_omega)/_omega**2)*_d_p1+((3*_eta**2-4*_eta*_omega+_omega**2)/_omega**2)*_d_p
display(smp.Eq(_d1f_t,_d1f_j_interpolate))
_d2f_j_interpolate1=(((6*_omega -12*_eta)/_omega**3)*_f_p1)+(((12*_eta-6*_omega)/_omega**3)*_f_p)+((6*_eta-2*_omega)/_omega**2)*_d_p1+((6*_eta-4*_omega)/_omega**2)*_d_p
_d2f_j_interpolate2=(((6*_omega-12*_eta)/_omega**2)*_delta_p)+((6*_eta-2*_omega)/_omega**2)*_d_p1+((6*_eta-4*_omega)/_omega**2)*_d_p
display(smp.Eq(_d2f_t,_d2f_j_interpolate1))
display(smp.Eq(_d2f_t,_d2f_j_interpolate2))
_d2fp_t_interpolate=(6*_omega_p-2*_d_p1-4*_d_p)/_omega_p
display("Because",smp.Eq(_eta,_zero),"as",smp.Eq(_f_j,_f_p), "at the point p,",
smp.Eq(_d2fp_t,_d2fp_t_interpolate))
_d2fn_t1_interpolate=(-6*_omega_p+4*_d_p1+2*_d_p)/_omega_p
display("At",_d2fn_t1, ",",smp.Eq(_eta,_omega_p))
display(smp.Eq(_d2fn_t1, _d2fn_t1_interpolate))
_d2fn_t_interpolate=(-6*_omega_p_1+4*_d_p+2*_d_p_1)/_omega_p_1
display("Then",smp.Eq(_d2fn_t, _d2fn_t_interpolate))
display("Thus, fulfil the following condition:",smp.Eq(_d2fp_t, _d2fn_t))
display("A Natural Cubic Spline also fulil this condition in addition:",smp.Eq(_d2fp_t, smp.Eq(_d2fn_t,_zero)))
# Cubic Spline Interpolation
f_j_CSP = CubicSpline(p, f_p, bc_type='natural'); f_j_CSP=f_j_CSP(j)
Title='Cubic Spline'
f_j_list.append(f_j_CSP); Title_list.append(Title)
PlotLowerEnvelope(Title,p,y_j,f_j_CSP)
MoveStepByStep(f_j_CSP,Title)
# SymPy: Akima
display(smp.Eq(_w_1, smp.Abs(_delta_p1-_delta_p)))
display(smp.Eq(_w_2, smp.Abs(_delta_p_1-_delta_p_2)))
display(smp.Eq(_d_p,(_w_1/(_w_1+_w_2)*_delta_p_1)+(_w_2/(_w_1+_w_2)*_delta_p)))
# Akima Spline Interpolation
f_j_Akima = Akima1DInterpolator(p, f_p)(j)
Title='Akima Spline'
f_j_list.append(f_j_Akima); Title_list.append(Title)
PlotLowerEnvelope(Title,p,y_j,f_j_Akima)
MoveStepByStep(f_j_Akima,Title)
# Comparing these interpolation methods
N_cut=math.floor(N*1)
plt.rcParams["figure.figsize"] = (9,5)
plt.figure();
plt.title("Comparing these interpolation methods")
plt.plot(y_j[0:N_cut], color='lightsteelblue', label="Original");
plt.plot(p, y_p[0:N_cut], "*", color='Magenta', label="Lower Peaks", markersize=9);
for k in range(len(f_j_list)):
plt.plot(f_j_list[k][0:N_cut], "--", label=Title_list[k]);
plt.legend(loc="best")
plt.show(); plt.show(block=False)
# Showing derivatives of these functions
N_cut=math.floor(N*0.4)
NoDerivs=2
plt.figure()
fig, axs = plt.subplots(len(f_j_list), NoDerivs+1)
plt.tight_layout()
for k in range(len(f_j_list)):
f_j=f_j_list[k];d_f_j=f_j[0:N_cut].copy();
for l in range(NoDerivs+1):
SubPlotTitle = "".join([Title_list[k]," Diff(",str(l),")"])
axs[k, l].plot(d_f_j,".",color='darkgreen');
axs[k, l].set_title(label=SubPlotTitle)
d_f_j=np.diff(d_f_j)
plt.show(block=False)
Comparison
Continuity provided by the derivatives