Find the best function that fits a time series
pip install numpy
pip install matplotlib
pip install scipy
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
- fit_time_series Fit a time series to the best-fitting mathematical equation
Arguments:
time
: Array of time values.
data
: Array of corresponding data values.Returns: The best-fitting function
best_func
, optimal parametersbest_params
, and covariance matrixcov_matrix
.
- jacobian Compute the Jacobian matrix of a given function
Arguments:
func
: Function for which to calculate the Jacobian.
x
: Array of input values.
params
: Additional parameters required by the function.Returns: array: Jacobian matrix
jac
.
- compute_bias Compute the bias between the predicted values and the actual data.
Arguments:
data
: Function for which to calculate the Jacobian.
predicted
: Array of predicted values.Returns: float value (Bias value).
- compute_rmse Compute the Root Mean Squared Error (RMSE) between the predicted values and the actual data.
Arguments:
data
: Function for which to calculate the Jacobian.
predicted
: Array of predicted values.Returns: float value (RMSE value).
- compute_scatter_index Compute the scatter index between the predicted values and the actual data.
Arguments:
data
: Function for which to calculate the Jacobian.
predicted
: Array of predicted values.Returns: float value (Scatter index value).
- compute_r_squared Compute the coefficient of determination (R-squared) between the predicted values and the actual data.
Arguments:
data
: Function for which to calculate the Jacobian.
predicted
: Array of predicted values.Returns: float value (R-squared value).
- plot_data_and_fit Plot the original data along with the best-fitting function.
Arguments:
time
: Array of time values.
data
: Array of corresponding data values.
func
: The best-fitting function.
params
: The optimal parameters.Returns: none.
- read_data Read the data from a file with columns separated by commas.
Arguments:
filename
: Path to the data file (including filename).Returns: Time values
time
and data values as numpy arraysvariables
.
- format_equation_with_coefficients Formats the best-fit equation with substituted coefficients as a string.
Arguments:
best_func
: The best-fitting function.
best_params
: The optimal parameters.Returns:
equation_with_coefficients
: The formatted equation with substituted coefficients (string).
time = np.array([1, 1.5, 2, 3, 4, 5])
data = np.array([1.5, 3, 8, 20, 48, 120])
best_func, best_params, cov_matrix = fit_time_series(time, data)
predicted_data = best_func(time, *best_params)
bias = calculate_bias(data, predicted_data)
rmse = calculate_rmse(data, predicted_data)
scatter_index = calculate_scatter_index(data, predicted_data)
r_squared = calculate_r_squared(data, predicted_data)
print("Bias:", bias)
print("RMSE:", rmse)
print("Scatter Index:", scatter_index)
print("R-squared:", r_squared)
plot_data_and_fit(time, data, best_func, best_params)
filename = 'data.csv'
time, data = read_data(filename)
# Select column 3 from 'data.csv' (var3)
var3 = data[:, 2]
best_func, best_params, cov_matrix = fit_time_series(time, var3)
print("Best-fitting function:", best_func.__name__)
print("Optimal parameters:", best_params)
print("Covariance matrix:", cov_matrix)
predicted_data = best_func(time, *best_params)
bias = calculate_bias(var3, predicted_data)
rmse = calculate_rmse(var3, predicted_data)
scatter_index = calculate_scatter_index(var3, predicted_data)
r_squared = calculate_r_squared(var3, predicted_data)
print("Bias:", bias)
print("RMSE:", rmse)
print("Scatter Index:", scatter_index)
print("R-squared:", r_squared)
plot_data_and_fit(time, var3, best_func, best_params)
equation_string = format_equation_with_coefficients(best_func, best_params)
print(equation_string)