humbertovarona / pCurveFit

Find the best function that fits a time series

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pCurveFit

Find the best function that fits a time series

Version

Release date

License

Programming language

OS

Installation

pip install numpy
pip install matplotlib
pip install scipy

Requirements

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

Functions list

  1. 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 parameters best_params, and covariance matrix cov_matrix.

  1. 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.

  1. 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).

  1. 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).

  1. 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).

  1. 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).

  1. 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.

  1. 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 arrays variables.

  1. 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).

Usage examples

Sample 1

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)

Sample 2

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)

About

Find the best function that fits a time series

License:MIT License


Languages

Language:Python 100.0%