def exponential(data, precision): independent_variable = dimension(data, 1) dependent_variable = dimension(data, 2) independent_matrix = [] dependent_matrix = [] for i in range(len(data)): independent_matrix.append([independent_variable[i], 1]) dependent_matrix.append([log(dependent_variable[i])]) solution = solve(independent_matrix, dependent_matrix, precision) constants = [exp(solution[1]), exp(solution[0])] coefficients = [ rounding(constants[0], precision), rounding(constants[1], precision) ] equation = exponential_equation(*coefficients) derivative = exponential_derivative(*coefficients) integral = exponential_integral(*coefficients)['evaluation'] first_derivative = derivative['first']['evaluation'] second_derivative = derivative['second']['evaluation'] points = key_points('exponential', solution, equation, first_derivative, second_derivative, precision) five_numbers = five_number_summary(independent_variable, precision) min_value = five_numbers['minimum'] max_value = five_numbers['maximum'] q1 = five_numbers['q1'] q3 = five_numbers['q3'] accumulated_range = accumulation(integral, min_value, max_value, precision) accumulated_iqr = accumulation(integral, q1, q3, precision) averages_range = average_values('exponential', equation, integral, min_value, max_value, coefficients, precision) averages_iqr = average_values('exponential', equation, integral, q1, q3, coefficients, precision) predicted = [] for i in range(len(data)): predicted.append(equation(independent_variable[i])) accuracy = correlation(dependent_variable, predicted, precision) evaluations = { 'equation': equation, 'derivative': first_derivative, 'integral': integral } points = { 'roots': points['roots'], 'maxima': points['maxima'], 'minima': points['minima'], 'inflections': points['inflections'] } accumulations = {'range': accumulated_range, 'iqr': accumulated_iqr} averages = {'range': averages_range, 'iqr': averages_iqr} result = { 'constants': coefficients, 'evaluations': evaluations, 'points': points, 'accumulations': accumulations, 'averages': averages, 'correlation': accuracy } return result
def logarithmic(data, precision): independent_variable = dimension(data, 1) dependent_variable = dimension(data, 2) independent_matrix = [] dependent_matrix = column(dependent_variable) for i in range(len(data)): independent_matrix.append([log(independent_variable[i]), 1]) solution = solve(independent_matrix, dependent_matrix, precision) equation = logarithmic_equation(*solution) derivative = logarithmic_derivative(*solution) integral = logarithmic_integral(*solution)['evaluation'] first_derivative = derivative['first']['evaluation'] second_derivative = derivative['second']['evaluation'] points = key_points('logarithmic', solution, equation, first_derivative, second_derivative, precision) five_numbers = five_number_summary(independent_variable, precision) min_value = five_numbers['minimum'] max_value = five_numbers['maximum'] q1 = five_numbers['q1'] q3 = five_numbers['q3'] accumulated_range = accumulation(integral, min_value, max_value, precision) accumulated_iqr = accumulation(integral, q1, q3, precision) averages_range = average_values('logarithmic', equation, integral, min_value, max_value, solution, precision) averages_iqr = average_values('logarithmic', equation, integral, q1, q3, solution, precision) predicted = [] for i in range(len(data)): predicted.append(equation(independent_variable[i])) accuracy = correlation(dependent_variable, predicted, precision) evaluations = { 'equation': equation, 'derivative': first_derivative, 'integral': integral } points = { 'roots': points['roots'], 'maxima': points['maxima'], 'minima': points['minima'], 'inflections': points['inflections'] } accumulations = { 'range': accumulated_range, 'iqr': accumulated_iqr } averages = { 'range': averages_range, 'iqr': averages_iqr } result = { 'constants': solution, 'evaluations': evaluations, 'points': points, 'accumulations': accumulations, 'averages': averages, 'correlation': accuracy } return result
def test_logistic_key_points(self): logistic_key_points = key_points( 'logistic', coefficients[:3], logistic_function, logistic_derivatives_object['first']['evaluation'], logistic_derivatives_object['second']['evaluation'], precision) self.assertEqual( logistic_key_points, { 'roots': [None], 'maxima': [None], 'minima': [None], 'inflections': [[5, 1.0]] })
def test_exponential_key_points(self): exponential_key_points = key_points( 'exponential', coefficients[:2], exponential_function, exponential_derivatives_object['first']['evaluation'], exponential_derivatives_object['second']['evaluation'], precision) self.assertEqual( exponential_key_points, { 'roots': [None], 'maxima': [None], 'minima': [None], 'inflections': [None] })
def test_hyperbolic_key_points(self): hyperbolic_key_points = key_points( 'hyperbolic', coefficients[:2], hyperbolic_function, hyperbolic_derivatives_object['first']['evaluation'], hyperbolic_derivatives_object['second']['evaluation'], precision) self.assertEqual( hyperbolic_key_points, { 'roots': [[-0.6667, 0]], 'maxima': [None], 'minima': [None], 'inflections': [None] })
def test_cubic_key_points(self): cubic_key_points = key_points( 'cubic', coefficients, cubic_function, cubic_derivatives_object['first']['evaluation'], cubic_derivatives_object['second']['evaluation'], precision) self.assertEqual( cubic_key_points, { 'roots': [[-1.4455, 0]], 'maxima': [None], 'minima': [None], 'inflections': [[-0.5, 5.0]] })
def test_quadratic_key_points(self): quadratic_key_points = key_points( 'quadratic', coefficients[:3], quadratic_function, quadratic_derivatives_object['first']['evaluation'], quadratic_derivatives_object['second']['evaluation'], precision) self.assertEqual( quadratic_key_points, { 'roots': [None], 'maxima': [None], 'minima': [[-0.75, 3.875]], 'inflections': [None] })
def test_linear_key_points(self): linear_key_points = key_points( 'linear', coefficients[:2], linear_function, linear_derivatives_object['first']['evaluation'], linear_derivatives_object['second']['evaluation'], precision) self.assertEqual( linear_key_points, { 'roots': [[-1.5, 0]], 'maxima': [None], 'minima': [None], 'inflections': [None] })
def test_sinusoidal_key_points(self): sinusoidal_key_points = key_points( 'sinusoidal', coefficients, sinusoidal_function, sinusoidal_derivatives_object['first']['evaluation'], sinusoidal_derivatives_object['second']['evaluation'], precision) self.assertEqual( sinusoidal_key_points, { 'roots': [None], 'maxima': [[5.5236, 9.0], [7.618, 9.0], [9.7124, 9.0], ['5.5236 + 2.0944k', 9.0]], 'minima': [[6.5708, 5.0], [8.6652, 5.0], ['6.5708 + 2.0944k', 5.0]], 'inflections': [[5, 7.0], [6.0472, 7.0], [7.0944, 7.0], [8.1416, 7.0], [9.1888, 7.0001], ['5 + 1.0472k', 7.0]] })
def sinusoidal(data, precision): independent_variable = dimension(data, 1) dependent_variable = dimension(data, 2) dependent_max = max(dependent_variable) dependent_min = min(dependent_variable) dependent_range = dependent_max - dependent_min solution = [] def sinusoidal_fit(variable, first_constant, second_constant, third_constant, fourth_constant): evaluation = first_constant * sin( second_constant * (variable - third_constant)) + fourth_constant return evaluation parameters, covariance = curve_fit(sinusoidal_fit, independent_variable, dependent_variable, bounds=[(-1 * dependent_range, -inf, -inf, dependent_min), (dependent_range, inf, inf, dependent_max)]) solution = list(parameters) constants = [] for number in solution: constants.append(rounding(number, precision)) equation = sinusoidal_equation(*solution) derivative = sinusoidal_derivative(*solution) integral = sinusoidal_integral(*solution)['evaluation'] first_derivative = derivative['first']['evaluation'] second_derivative = derivative['second']['evaluation'] points = key_points('sinusoidal', solution, equation, first_derivative, second_derivative, precision) five_numbers = five_number_summary(independent_variable, precision) min_value = five_numbers['minimum'] max_value = five_numbers['maximum'] q1 = five_numbers['q1'] q3 = five_numbers['q3'] accumulated_range = accumulation(integral, min_value, max_value, precision) accumulated_iqr = accumulation(integral, q1, q3, precision) averages_range = average_values('sinusoidal', equation, integral, min_value, max_value, solution, precision) averages_iqr = average_values('sinusoidal', equation, integral, q1, q3, solution, precision) predicted = [] for i in range(len(data)): predicted.append(equation(independent_variable[i])) accuracy = correlation(dependent_variable, predicted, precision) evaluations = { 'equation': equation, 'derivative': first_derivative, 'integral': integral } points = { 'roots': points['roots'], 'maxima': points['maxima'], 'minima': points['minima'], 'inflections': points['inflections'] } accumulations = {'range': accumulated_range, 'iqr': accumulated_iqr} averages = {'range': averages_range, 'iqr': averages_iqr} result = { 'constants': constants, 'evaluations': evaluations, 'points': points, 'accumulations': accumulations, 'averages': averages, 'correlation': accuracy } return result
def logistic(data, precision): independent_variable = dimension(data, 1) dependent_variable = dimension(data, 2) halved_data = halve_dimension(data, 1) dependent_lower = dimension(halved_data['lower'], 2) dependent_upper = dimension(halved_data['upper'], 2) mean_lower = mean(dependent_lower) mean_upper = mean(dependent_upper) dependent_max = max(dependent_variable) dependent_min = min(dependent_variable) dependent_range = dependent_max - dependent_min solution = [] def logistic_fit(variable, first_constant, second_constant, third_constant): evaluation = first_constant / (1 + exp(-1 * second_constant * (variable - third_constant))) return evaluation if mean_upper >= mean_lower: parameters, covariance = curve_fit( logistic_fit, independent_variable, dependent_variable, bounds=[(dependent_max - dependent_range, 0, -inf), (dependent_max + dependent_range, inf, inf)]) solution = list(parameters) else: parameters, covariance = curve_fit( logistic_fit, independent_variable, dependent_variable, bounds=[(dependent_max - dependent_range, -inf, -inf), (dependent_max + dependent_range, 0, inf)]) solution = list(parameters) constants = [] for number in solution: constants.append(rounding(number, precision)) equation = logistic_equation(*solution) derivative = logistic_derivative(*solution) integral = logistic_integral(*solution)['evaluation'] first_derivative = derivative['first']['evaluation'] second_derivative = derivative['second']['evaluation'] points = key_points('logistic', solution, equation, first_derivative, second_derivative, precision) five_numbers = five_number_summary(independent_variable, precision) min_value = five_numbers['minimum'] max_value = five_numbers['maximum'] q1 = five_numbers['q1'] q3 = five_numbers['q3'] accumulated_range = accumulation(integral, min_value, max_value, precision) accumulated_iqr = accumulation(integral, q1, q3, precision) averages_range = average_values('logistic', equation, integral, min_value, max_value, solution, precision) averages_iqr = average_values('logistic', equation, integral, q1, q3, solution, precision) predicted = [] for i in range(len(data)): predicted.append(equation(independent_variable[i])) accuracy = correlation(dependent_variable, predicted, precision) evaluations = { 'equation': equation, 'derivative': first_derivative, 'integral': integral } points = { 'roots': points['roots'], 'maxima': points['maxima'], 'minima': points['minima'], 'inflections': points['inflections'] } accumulations = {'range': accumulated_range, 'iqr': accumulated_iqr} averages = {'range': averages_range, 'iqr': averages_iqr} result = { 'constants': constants, 'evaluations': evaluations, 'points': points, 'accumulations': accumulations, 'averages': averages, 'correlation': accuracy } return result