principia_materia.mathematics.errortail module
- principia_materia.mathematics.errortail.fit_errortail(delta, datapoints, power=2, pick_min=3, pick_max=None, consecutive_picks=False, penalty=<function penalty_linear_mse>, separate_complex=False)
Fit error tail for finite difference calculations.
Either pick n and pick N scheme can be used with various penalty function.
- Parameters:
delta (array of floats, shape(n, )) – The delta values of the finite difference.
datapoints (array of numbers (complex or float), shape(n, ) or shape(n, m)) – The target function values evaluated at each delta. If the input is 2-dimensional, each column is computed separately.
power (int, optional, default to 2) – The power of the error tail. For forward and backward only FD, the error tail is linear. For central FD, the error tail is quadratic.
pick_min (int, optional, default to 3) – The minimum number of picks for delta selection.
pick_max (int, optional, default to None) – The maximum number of picks for delta selection. If None, the pick n scheme is used with n=pick_min. If not None, the pick N schcme is used with N=[pick_min, pick_max].
consecutive_picks (bool, optional, default to False) – Whether to pick consecutive deltas in the picking process.
penalty (callable, optional, default to penalty_linear_mse) – The penalty function to determine the best fit.
separate_complex (bool, optional, default to False) – Whether to fit real and imaginary part of the complex data points separetely.
- Returns:
fit_result (array of numbers (complex or float, same of type of datapoints), shape(m, 2)) – The coefficients of the fitted error tail.
final_pick (list of arrays of int or complex, length of m) – The indices of the picked points.
penalty_value (array of numbers (complex or float, same of type of datapoints), shape(m, )) – The result of the penalty function.
- principia_materia.mathematics.errortail.generate_picks(total, pick, consecutive=False)
Generator for indices to pick with specified pick scheme.
- Parameters:
total (int) – The total number of points to pick from.
pick (int) – The number of points to pick.
consecutive (bool, optional, detault to False) – Whether the points need to be picked consecutively.
- principia_materia.mathematics.errortail.get_errortail(deltas, values, fdtype='c', pick_min=3, pick_max=None, consecutive=False, penalty=<function penalty_linear_mse>, separate_complex=True, return_xcoef=False, return_pick=False, return_penalty=False)
Compute error tail for finite difference results.
In this function, contrary to the
fit_errortail
function, the order of the errortail is determined by the type of the finite difference instead.- Parameters:
delta (array of floats, shape(n, )) – The delta values of the finite difference.
values (array of numbers (complex or float), shape(n, ) or shape(n, m, ...)) – The target function values evaluated at each delta. If the input dimention is higher than 1, each entry from the second dimention and beyond is computed separately.
fdtype (str, choice of ["c", "b", "f"], default to "c") – The type of finite difference calculation, “c” for central, “b” for backward and “f” for forward. For forward and backward only FD, the error tail is linear. For central FD, the error tail is quadratic.
pick_min (int, optional, default to 3) – The minimum number of picks for delta selection.
pick_max (int, optional, default to None) – The maximum number of picks for delta selection. If None, the pick n scheme is used with n=pick_min. If not None, the pick N schcme is used with N=[pick_min, pick_max].
consecutive_picks (bool, optional, default to False) – Whether to pick consecutive deltas in the picking process.
penalty (callable, optional, default to penalty_linear_mse) – The penalty function to determine the best fit.
separate_complex (bool, optional, default to False) – Whether to fit real and imaginary part of the complex data points separetely.
return_xcoef (bool, optional, default to False) – Whether to return the coefficient of the x^order term.
return_pick (bool, optional, default to False) – Whether to return the indices of the picked points.
return_penalty (bool, optional, default to False) – Whether to return the result of the penalty function.
- Returns:
result (array of numbers (complex or float, same of type of values), shape(m, …)) – The intercept of the error tail.
xcoef (array of numbers (complex or float, same of type of values), shape(m, …)) – The coefficient of the x^order term, can be used to plot the fitted error tail.
pick (list of arrays of int or complex, length of m) – The indices of the picked points.
penalty (array of numbers (complex or float, same of type of values), shape(m, …)) – The result of the penalty function.
- principia_materia.mathematics.errortail.get_errortail_yaml_wrapper(title='Errortail Dataset', iscomplex=False)
- principia_materia.mathematics.errortail.least_square(A, b, return_R=False, return_mse=False, return_S=False, return_stdx=False)
Compute least square of Ax=b
The function computes the least square solution for the equation \(A \cdot x=b\), as well as metrics to evaluate the quality of the solution when the input A is not square and full rank, and the solution is not exact.
- Parameters:
A (array of numbers, shape(N, m)) – Matrix A.
b (array of numbers, shape(N, ) or shape(N, l)) – Vector b, if b is 2 dimensional, least square is computed for each column of b.
return_R (bool) – Whether to return residuals.
return_mse (bool) – Whether to return mean square error.
return_S (bool) – Whether to return covariance matrix.
return_stdx (bool) – Whether to return standard error.
- Returns:
x (array of numbers, shape(l, m)) – Least square solution.
R (array of numbers, shape(l, )) – Residuals of the least square.
mse (array of numbers, shape(l, )) – Mean square error of the least square.
S (array of numbers, shape(l, m, m)) – Covariance matrix of the least square.
stdx (array of numbers, shape(l, m)) – Standard error of the least square.
- principia_materia.mathematics.errortail.penalty_linear_mse(n, R, mse, S, stdx)
Compute linear penalty from mse
- Parameters:
n (int) – Number of points to fit with
R (float or complex) – Residule of least square
mse (float or complex) – Mean squared error of least square
S (float or complex) – Covariance of least square
stdx (float or complex) – Standard error of least square
- Returns:
penalty – The penalty
- Return type:
float or complex
- principia_materia.mathematics.errortail.penalty_sqrt_mse(n, R, mse, S, stdx)
Compute square root penalty from mse
- Parameters:
n (int) – Number of points to fit with
R (float or complex) – Residule of least square
mse (float or complex) – Mean squared error of least square
S (float or complex) – Covariance of least square
stdx (float or complex) – Standard error of least square
- Returns:
penalty – The penalty
- Return type:
float or complex
- principia_materia.mathematics.errortail.penalty_square_mse(n, R, mse, S, stdx)
Compute squared penalty from mse
- Parameters:
n (int) – Number of points to fit with
R (float or complex) – Residule of least square
mse (float or complex) – Mean squared error of least square
S (float or complex) – Covariance of least square
stdx (float or complex) – Standard error of least square
- Returns:
penalty – The penalty
- Return type:
float or complex