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_cubic_averages(self): cubic_averages = average_values('cubic', cubic_function, cubic_integral_object['evaluation'], 10, 20, coefficients, precision) self.assertEqual( cubic_averages, { 'average_value_derivative': 1495.0, 'mean_values_derivative': [15.2665], 'average_value_integral': 8282.0, 'mean_values_integral': [15.5188] })
def test_linear_averages(self): linear_averages = average_values('linear', linear_function, linear_integral_object['evaluation'], 10, 20, coefficients[:2], precision) self.assertEqual( linear_averages, { 'average_value_derivative': 2.0, 'mean_values_derivative': ['All'], 'average_value_integral': 33.0, 'mean_values_integral': [15.0] })
def test_logistic_averages(self): logistic_averages = average_values( 'logistic', logistic_function, logistic_integral_object['evaluation'], 10, 20, coefficients[:3], precision) self.assertEqual( logistic_averages, { 'average_value_derivative': 0.0001, 'mean_values_derivative': [None], 'average_value_integral': 2.0, 'mean_values_integral': [None] })
def test_logarithmic_averages(self): logarithmic_averages = average_values( 'logarithmic', logarithmic_function, logarithmic_integral_object['evaluation'], 10, 20, coefficients[:2], precision) self.assertEqual( logarithmic_averages, { 'average_value_derivative': 0.1386, 'mean_values_derivative': [14.43], 'average_value_integral': 8.3778, 'mean_values_integral': [14.7155] })
def test_exponential_averages(self): exponential_averages = average_values( 'exponential', exponential_function, exponential_integral_object['evaluation'], 10, 20, coefficients[:2], precision) self.assertEqual( exponential_averages, { 'average_value_derivative': 697345070.4, 'mean_values_derivative': [17.8185], 'average_value_integral': 634750837.5729, 'mean_values_integral': [17.8185] })
def test_hyperbolic_averages(self): hyperbolic_averages = average_values( 'hyperbolic', hyperbolic_function, hyperbolic_integral_object['evaluation'], 10, 20, coefficients[:2], precision) self.assertEqual( hyperbolic_averages, { 'average_value_derivative': -0.01, 'mean_values_derivative': [14.1421], 'average_value_integral': 3.1386, 'mean_values_integral': [14.43] })
def test_quadratic_averages(self): quadratic_averages = average_values( 'quadratic', quadratic_function, quadratic_integral_object['evaluation'], 10, 20, coefficients[:3], precision) self.assertEqual( quadratic_averages, { 'average_value_derivative': 63.0, 'mean_values_derivative': [15.0], 'average_value_integral': 516.6667, 'mean_values_integral': [15.2624] })
def test_sinusoidal_averages(self): sinusoidal_averages = average_values( 'sinusoidal', sinusoidal_function, sinusoidal_integral_object['evaluation'], 10, 20, coefficients, precision) self.assertEqual( sinusoidal_averages, { 'average_value_derivative': 0.0401, 'mean_values_derivative': [None], 'average_value_integral': 6.9143, 'mean_values_integral': [10.2503, '4.9857 + 2.0944k', '6.0615 + 2.0944k'] })
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