def exponential_roots(first_constant, second_constant, precision = 4): """ Calculates the roots of an exponential function Parameters ---------- first_constant : int or float Constant multiple of the original exponential function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Base rate of variable of the original exponential function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First two arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- roots : list of float List of the x-coordinates of all of the x-intercepts of the original function; if the function never crosses the x-axis, then it will return a list of `None` See Also -------- :func:`~regressions.analyses.equations.exponential.exponential_equation`, :func:`~regressions.analyses.derivatives.exponential.exponential_derivatives`, :func:`~regressions.analyses.integrals.exponential.exponential_integral`, :func:`~regressions.models.exponential.exponential_model` Notes ----- - Standard form of a exponential function: :math:`f(x) = a\\cdot{b^x}` - Exponential formula: :math:`x = \\varnothing` Examples -------- Import `exponential_roots` function from `regressions` library >>> from regressions.analyses.roots.exponential import exponential_roots Calculate the roots of an exponential function with coefficients 2 and 3 >>> roots_first = exponential_roots(2, 3) >>> print(roots_first) [None] Calculate the roots of an exponential function with coefficients -2 and 3 >>> roots_second = exponential_roots(-2, 3) >>> print(roots_second) [None] Calculate the roots of an exponential function with all inputs set to 0 >>> roots_zeroes = exponential_roots(0, 0) >>> print(roots_zeroes) [None] """ # Handle input errors two_scalars(first_constant, second_constant) positive_integer(precision) coefficients = no_zeroes([first_constant, second_constant], precision) # Create list to return result = [] # Determine root root = None # Return result result.append(root) return result
def hyperbolic_integral(first_constant, second_constant, precision=4): """ Generates the integral of a hyperbolic function Parameters ---------- first_constant : int or float Coefficient of the reciprocal variable of the original hyperbolic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Coefficient of the constant term of the original hyperbolic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First two arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- integral['constants'] : list of float Coefficients of the resultant integral integral['evaluation'] : func Function for evaluating the resultant integral at any float or integer argument; if zero inputted as argument, it will be converted to a small, non-zero decimal value (e.g., 0.0001) See Also -------- :func:`~regressions.analyses.equations.hyperbolic.hyperbolic_equation`, :func:`~regressions.analyses.derivatives.hyperbolic.hyperbolic_derivatives`, :func:`~regressions.analyses.roots.hyperbolic.hyperbolic_roots`, :func:`~regressions.models.hyperbolic.hyperbolic_model` Notes ----- - Standard form of a hyperbolic function: :math:`f(x) = a\\cdot{\\frac{1}{x}} + b` - Integral of a hyperbolic function: :math:`F(x) = a\\cdot{\\ln|x|} + b\\cdot{x}` - |indefinite_integral| - |integration_formulas| Examples -------- Import `sinusoidal_hyperbolic` function from `regressions` library >>> from regressions.analyses.hyperbolics.sinusoidal import sinusoidal_hyperbolic Generate the integral of a hyperbolic function with coefficients 2 and 3, then display its coefficients >>> integral_constants = hyperbolic_integral(2, 3) >>> print(integral_constants['constants']) [2.0, 3.0] Generate the integral of a hyperbolic function with coefficients -2 and 3, then evaluate its integral at 10 >>> integral_evaluation = hyperbolic_integral(-2, 3) >>> print(integral_evaluation['evaluation'](10)) 25.3948 Generate the integral of a hyperbolic function with all inputs set to 0, then display its coefficients >>> integral_zeroes = hyperbolic_integral(0, 0) >>> print(integral_zeroes['constants']) [0.0001, 0.0001] """ # Handle input errors two_scalars(first_constant, second_constant) positive_integer(precision) coefficients = no_zeroes([first_constant, second_constant], precision) # Create constants constants = [coefficients[0], coefficients[1]] # Create evaluation def hyperbolic_evaluation(variable): # Circumvent logarithm of zero if variable == 0: variable = 10**(-precision) evaluation = constants[0] * log( abs(variable)) + constants[1] * variable rounded_evaluation = rounded_value(evaluation, precision) return rounded_evaluation # Package constants and evaluation in single dictionary results = {'constants': constants, 'evaluation': hyperbolic_evaluation} return results
def linear_model(data, precision = 4): """ Generates a linear regression model from a given data set Parameters ---------- data : list of lists of int or float List of lists of numbers representing a collection of coordinate pairs; it must include at least 10 pairs precision : int, default=4 Maximum number of digits that can appear after the decimal place of the results Raises ------ TypeError First argument must be a 2-dimensional list TypeError Elements nested within first argument must be integers or floats ValueError First argument must contain at least 10 elements ValueError Last argument must be a positive integer Returns ------- model['constants'] : list of float Coefficients of the resultant linear model; the first element is the coefficient of the linear term, and the second element is the coefficient of the constant term model['evaluations']['equation'] : func Function that evaluates the equation of the linear model at a given numeric input (e.g., model['evaluations']['equation'](10) would evaluate the equation of the linear model when the independent variable is 10) model['evaluations']['derivative'] : func Function that evaluates the first derivative of the linear model at a given numeric input (e.g., model['evaluations']['derivative'](10) would evaluate the first derivative of the linear model when the independent variable is 10) model['evaluations']['integral'] : func Function that evaluates the integral of the linear model at a given numeric input (e.g., model['evaluations']['integral'](10) would evaluate the integral of the linear model when the independent variable is 10) model['points']['roots'] : list of lists of float List of lists of numbers representing the coordinate pairs of all the x-intercepts of the linear model (will contain exactly one point) model['points']['maxima'] : list of lists of float List of lists of numbers representing the coordinate pairs of all the maxima of the linear model (will always be `None`) model['points']['minima'] : list of lists of float List of lists of numbers representing the coordinate pairs of all the minima of the linear model (will always be `None`) model['points']['inflections'] : list of lists of float List of lists of numbers representing the coordinate pairs of all the inflection points of the linear model (will always be `None`) model['accumulations']['range'] : float Total area under the curve represented by the linear model between the smallest independent coordinate originally provided and the largest independent coordinate originally provided (i.e., over the range) model['accumulations']['iqr'] : float Total area under the curve represented by the linear model between the first and third quartiles of all the independent coordinates originally provided (i.e., over the interquartile range) model['averages']['range']['average_value_derivative'] : float Average rate of change of the curve represented by the linear model between the smallest independent coordinate originally provided and the largest independent coordinate originally provided model['averages']['range']['mean_values_derivative'] : list of float All points between the smallest independent coordinate originally provided and the largest independent coordinate originally provided where their instantaneous rate of change equals the function's average rate of change over that interval model['averages']['range']['average_value_integral'] : float Average value of the curve represented by the linear model between the smallest independent coordinate originally provided and the largest independent coordinate originally provided model['averages']['range']['mean_values_integral'] : list of float All points between the smallest independent coordinate originally provided and the largest independent coordinate originally provided where their value equals the function's average value over that interval model['averages']['iqr']['average_value_derivative'] : float Average rate of change of the curve represented by the linear model between the first and third quartiles of all the independent coordinates originally provided model['averages']['iqr']['mean_values_derivative'] : list of float All points between the first and third quartiles of all the independent coordinates originally provided where their instantaneous rate of change equals the function's average rate of change over that interval model['averages']['iqr']['average_value_integral'] : float Average value of the curve represented by the linear model between the first and third quartiles of all the independent coordinates originally provided model['averages']['iqr']['mean_values_integral'] : list of float All points between the first and third quartiles of all the independent coordinates originally provided where their value equals the function's average value over that interval model['correlation'] : float Correlation coefficient indicating how well the model fits the original data set (values range between 0.0, implying no fit, and 1.0, implying a perfect fit) See Also -------- :func:`~regressions.analyses.equations.linear.linear_equation`, :func:`~regressions.analyses.derivatives.linear.linear_derivatives`, :func:`~regressions.analyses.integrals.linear.linear_integral`, :func:`~regressions.analyses.roots.linear.linear_roots`, :func:`~regressions.statistics.correlation.correlation_coefficient`, :func:`~regressions.execute.run_all` Notes ----- - Provided ordered pairs for the data set: :math:`p_i = \\{ (p_{1,x}, p_{1,y}), (p_{2,x}, p_{2,y}), \\cdots, (p_{n,x}, p_{n,y}) \\}` - Provided values for the independent variable: :math:`X_i = \\{ p_{1,x}, p_{2,x}, \\cdots, p_{n,x} \\}` - Provided values for the dependent variable: :math:`Y_i = \\{ p_{1,y}, p_{2,y}, \\cdots, p_{n,y} \\}` - Minimum value of the provided values for the independent variable: :math:`X_{min} \\leq p_{j,x}, \\forall p_{j,x} \\in X_i` - Maximum value of the provided values for the independent variable: :math:`X_{max} \\geq p_{j,x}, \\forall p_{j,x} \\in X_i` - First quartile of the provided values for the independent variable: :math:`X_{Q1}` - Third quartile of the provided values for the independent variable: :math:`X_{Q3}` - Mean of all provided values for the dependent variable: :math:`\\bar{y} = \\frac{1}{n}\\cdot{\\sum\\limits_{i=1}^n Y_i}` - Resultant values for the coefficients of the linear model: :math:`C_i = \\{ a, b \\}` - Standard form for the equation of the linear model: :math:`f(x) = a\\cdot{x} + b` - First derivative of the linear model: :math:`f'(x) = a` - Second derivative of the linear model: :math:`f''(x) = 0` - Integral of the linear model: :math:`F(x) = \\frac{a}{2}\\cdot{x^2} + b\\cdot{x}` - Potential x-values of the roots of the linear model: :math:`x_{intercepts} = \\{ -\\frac{b}{a} \\}` - Potential x-values of the maxima of the linear model: :math:`x_{maxima} = \\{ \\varnothing \\}` - Potential x-values of the minima of the linear model: :math:`x_{minima} = \\{ \\varnothing \\}` - Potential x-values of the inflection points of the linear model: :math:`x_{inflections} = \\{ \\varnothing \\}` - Accumulatation of the linear model over its range: :math:`A_{range} = \\int_{X_{min}}^{X_{max}} f(x) \\,dx` - Accumulatation of the linear model over its interquartile range: :math:`A_{iqr} = \\int_{X_{Q1}}^{X_{Q3}} f(x) \\,dx` - Average rate of change of the linear model over its range: :math:`m_{range} = \\frac{f(X_{max}) - f(X_{min})}{X_{max} - X_{min}}` - Potential x-values at which the linear model's instantaneous rate of change equals its average rate of change over its range: :math:`x_{m,range} = \\{ [X_{min}, X_{max}] \\}` - Average value of the linear model over its range: :math:`v_{range} = \\frac{1}{X_{max} - X_{min}}\\cdot{A_{range}}` - Potential x-values at which the linear model's value equals its average value over its range: :math:`x_{v,range} = \\{ -\\frac{b - v_{range}}{a} \\}` - Average rate of change of the linear model over its interquartile range: :math:`m_{iqr} = \\frac{f(X_{Q3}) - f(X_{Q1})}{X_{Q3} - X_{Q1}}` - Potential x-values at which the linear model's instantaneous rate of change equals its average rate of change over its interquartile range: :math:`x_{m,iqr} = \\{ [X_{Q1}, X_{Q3}] \\}` - Average value of the linear model over its interquartile range: :math:`v_{iqr} = \\frac{1}{X_{Q3} - X_{Q1}}\\cdot{A_{iqr}}` - Potential x-values at which the linear model's value equals its average value over its interquartile range: :math:`x_{v,iqr} = \\{ -\\frac{b - v_{iqr}}{a} \\}` - Predicted values based on the linear model: :math:`\\hat{y}_i = \\{ \\hat{y}_1, \\hat{y}_2, \\cdots, \\hat{y}_n \\}` - Residuals of the dependent variable: :math:`e_i = \\{ p_{1,y} - \\hat{y}_1, p_{2,y} - \\hat{y}_2, \\cdots, p_{n,y} - \\hat{y}_n \\}` - Deviations of the dependent variable: :math:`d_i = \\{ p_{1,y} - \\bar{y}, p_{2,y} - \\bar{y}, \\cdots, p_{n,y} - \\bar{y} \\}` - Sum of squares of residuals: :math:`SS_{res} = \\sum\\limits_{i=1}^n e_i^2` - Sum of squares of deviations: :math:`SS_{dev} = \\sum\\limits_{i=1}^n d_i^2` - Correlation coefficient for the linear model: :math:`r = \\sqrt{1 - \\frac{SS_{res}}{SS_{dev}}}` - |regression_analysis| Examples -------- Import `linear_model` function from `regressions` library >>> from regressions.models.linear import linear_model Generate a linear regression model for the data set [[1, 30], [2, 27], [3, 24], [4, 21], [5, 18], [6, 15], [7, 12], [8, 9], [9, 6], [10, 3]], then print its coefficients, roots, total accumulation over its interquartile range, and correlation >>> model_perfect = linear_model([[1, 30], [2, 27], [3, 24], [4, 21], [5, 18], [6, 15], [7, 12], [8, 9], [9, 6], [10, 3]]) >>> print(model_perfect['constants']) [-3.0, 33.0] >>> print(model_perfect['points']['roots']) [[11.0, 0.0]] >>> print(model_perfect['accumulations']['iqr']) 82.5 >>> print(model_perfect['correlation']) 1.0 Generate a linear regression model for the data set [[1, 32], [2, 25], [3, 14], [4, 23], [5, 39], [6, 45], [7, 42], [8, 49], [9, 36], [10, 33]], then print its coefficients, inflections, total accumulation over its range, and correlation >>> model_agnostic = linear_model([[1, 32], [2, 25], [3, 14], [4, 23], [5, 39], [6, 45], [7, 42], [8, 49], [9, 36], [10, 33]]) >>> print(model_agnostic['constants']) [1.9636, 23.0] >>> print(model_agnostic['points']['inflections']) [None] >>> print(model_agnostic['accumulations']['range']) 304.1982 >>> print(model_agnostic['correlation']) 0.5516 """ # Handle input errors matrix_of_scalars(data, 'first') long_vector(data) positive_integer(precision) # Store independent and dependent variable values separately independent_variable = single_dimension(data, 1) dependent_variable = single_dimension(data, 2) # Create matrices for independent and dependent variables independent_matrix = [] dependent_matrix = column_conversion(dependent_variable) # Iterate over inputted data for element in independent_variable: # Store linear and constant evaluations of original independent elements together as lists within independent matrix independent_matrix.append([element, 1]) # Solve system of equations solution = system_solution(independent_matrix, dependent_matrix, precision) # Eliminate zeroes from solution coefficients = no_zeroes(solution, precision) # Generate evaluations for function, derivatives, and integral equation = linear_equation(*coefficients, precision) derivative = linear_derivatives(*coefficients, precision)['first']['evaluation'] integral = linear_integral(*coefficients, precision)['evaluation'] # Determine key points of graph points = key_coordinates('linear', coefficients, precision) # Generate values for lower and upper bounds 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'] # Calculate accumulations accumulated_range = accumulated_area('linear', coefficients, min_value, max_value, precision) accumulated_iqr = accumulated_area('linear', coefficients, q1, q3, precision) # Determine average values and their points averages_range = average_values('linear', coefficients, min_value, max_value, precision) averages_iqr = average_values('linear', coefficients, q1, q3, precision) # Create list of predicted outputs predicted = [] for element in independent_variable: predicted.append(equation(element)) # Calculate correlation coefficient for model accuracy = correlation_coefficient(dependent_variable, predicted, precision) # Package preceding results in multiple dictionaries evaluations = { 'equation': equation, 'derivative': 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 } # Package all dictionaries in single dictionary to return result = { 'constants': coefficients, 'evaluations': evaluations, 'points': points, 'accumulations': accumulations, 'averages': averages, 'correlation': accuracy } return result
def quadratic_derivatives(first_constant, second_constant, third_constant, precision=4): """ Calculates the first and second derivatives of a quadratic function Parameters ---------- first_constant : int or float Coefficient of the quadratic term of the original quadratic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Coefficient of the linear term of the original quadratic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) third_constant : int or float Coefficient of the constant term of the original quadratic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First three arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- derivatives['first']['constants'] : list of float Coefficients of the resultant first derivative derivatives['first']['evaluation'] : func Function for evaluating the resultant first derivative at any float or integer argument derivatives['second']['constants'] : list of float Coefficients of the resultant second derivative derivatives['second']['evaluation'] : func Function for evaluating the resultant second derivative at any float or integer argument See Also -------- :func:`~regressions.analyses.equations.quadratic.quadratic_equation`, :func:`~regressions.analyses.integrals.quadratic.quadratic_integral`, :func:`~regressions.analyses.roots.quadratic.quadratic_roots`, :func:`~regressions.models.quadratic.quadratic_model` Notes ----- - Standard form of a quadratic function: :math:`f(x) = a\\cdot{x^2} + b\\cdot{x} + c` - First derivative of a quadratic function: :math:`f'(x) = 2a\\cdot{x} + b` - Second derivative of a quadratic function: :math:`f''(x) = 2a` - |differentiation_formulas| Examples -------- Import `quadratic_derivatives` function from `regressions` library >>> from regressions.analyses.derivatives.quadratic import quadratic_derivatives Generate the derivatives of a quadratic function with coefficients 2, 3, and 5, then display the coefficients of its first and second derivatives >>> derivatives_constants = quadratic_derivatives(2, 3, 5) >>> print(derivatives_constants['first']['constants']) [4.0, 3.0] >>> print(derivatives_constants['second']['constants']) [4.0] Generate the derivatives of a quadratic function with coefficients 7, -5, and 3, then evaluate its first and second derivatives at 10 >>> derivatives_evaluation = quadratic_derivatives(7, -5, 3) >>> print(derivatives_evaluation['first']['evaluation'](10)) 135.0 >>> print(derivatives_evaluation['second']['evaluation'](10)) 14.0 Generate the derivatives of a quadratic function with all inputs set to 0, then display the coefficients of its first and second derivatives >>> derivatives_zeroes = quadratic_derivatives(0, 0, 0) >>> print(derivatives_zeroes['first']['constants']) [0.0002, 0.0001] >>> print(derivatives_zeroes['second']['constants']) [0.0002] """ # Handle input errors three_scalars(first_constant, second_constant, third_constant) positive_integer(precision) coefficients = no_zeroes([first_constant, second_constant, third_constant], precision) # Create first derivative first_constants = [2 * coefficients[0], coefficients[1]] def first_derivative(variable): evaluation = first_constants[0] * variable + first_constants[1] rounded_evaluation = rounded_value(evaluation, precision) return rounded_evaluation first_dictionary = { 'constants': first_constants, 'evaluation': first_derivative } # Create second derivative second_constants = [first_constants[0]] def second_derivative(variable): evaluation = second_constants[0] rounded_evaluation = rounded_value(evaluation, precision) return rounded_evaluation second_dictionary = { 'constants': second_constants, 'evaluation': second_derivative } # Package both derivatives in single dictionary results = {'first': first_dictionary, 'second': second_dictionary} return results
def hyperbolic_derivatives(first_constant, second_constant, precision=4): """ Calculates the first and second derivatives of a hyperbolic function Parameters ---------- first_constant : int or float Coefficient of the reciprocal variable of the original hyperbolic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Coefficient of the constant term of the original hyperbolic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First two arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- derivatives['first']['constants'] : list of float Coefficients of the resultant first derivative derivatives['first']['evaluation'] : func Function for evaluating the resultant first derivative at any float or integer argument; if zero inputted as argument, it will be converted to a small, non-zero decimal value (e.g., 0.0001) derivatives['second']['constants'] : list of float Coefficients of the resultant second derivative derivatives['second']['evaluation'] : func Function for evaluating the resultant second derivative at any float or integer argument; if zero inputted as argument, it will be converted to a small, non-zero decimal value (e.g., 0.0001) See Also -------- :func:`~regressions.analyses.equations.hyperbolic.hyperbolic_equation`, :func:`~regressions.analyses.integrals.hyperbolic.hyperbolic_integral`, :func:`~regressions.analyses.roots.hyperbolic.hyperbolic_roots`, :func:`~regressions.models.hyperbolic.hyperbolic_model` Notes ----- - Standard form of a hyperbolic function: :math:`f(x) = a\\cdot{\\frac{1}{x}} + b` - First derivative of a hyperbolic function: :math:`f'(x) = -a\\cdot{\\frac{1}{x^2}}` - Second derivative of a hyperbolic function: :math:`f''(x) = 2a\\cdot{\\frac{1}{x^3}}` - |differentiation_formulas| Examples -------- Import `hyperbolic_derivatives` function from `regressions` library >>> from regressions.analyses.derivatives.hyperbolic import hyperbolic_derivatives Generate the derivatives of a hyperbolic function with coefficients 2 and 3, then display the coefficients of its first and second derivatives >>> derivatives_constants = hyperbolic_derivatives(2, 3) >>> print(derivatives_constants['first']['constants']) [-2.0] >>> print(derivatives_constants['second']['constants']) [4.0] Generate the derivatives of a hyperbolic function with coefficients -2 and 3, then evaluate its first and second derivatives at 10 >>> derivatives_evaluation = hyperbolic_derivatives(-2, 3) >>> print(derivatives_evaluation['first']['evaluation'](10)) 0.02 >>> print(derivatives_evaluation['second']['evaluation'](10)) -0.004 Generate the derivatives of a hyperbolic function with all inputs set to 0, then display the coefficients of its first and second derivatives >>> derivatives_zeroes = hyperbolic_derivatives(0, 0) >>> print(derivatives_zeroes['first']['constants']) [-0.0001] >>> print(derivatives_zeroes['second']['constants']) [0.0002] """ # Handle input errors two_scalars(first_constant, second_constant) positive_integer(precision) coefficients = no_zeroes([first_constant, second_constant], precision) # Create first derivative first_constants = [-1 * coefficients[0]] def first_derivative(variable): # Circumvent division by zero if variable == 0: variable = 10**(-precision) evaluation = first_constants[0] / variable**2 rounded_evaluation = rounded_value(evaluation, precision) return rounded_evaluation first_dictionary = { 'constants': first_constants, 'evaluation': first_derivative } # Create second derivative second_constants = [-2 * first_constants[0]] def second_derivative(variable): # Circumvent division by zero if variable == 0: variable = 10**(-precision) evaluation = second_constants[0] / variable**3 rounded_evaluation = rounded_value(evaluation, precision) return rounded_evaluation second_dictionary = { 'constants': second_constants, 'evaluation': second_derivative } # Package both derivatives in single dictionary results = {'first': first_dictionary, 'second': second_dictionary} return results
def hyperbolic_roots(first_constant, second_constant, precision=4): """ Calculates the roots of a hyperbolic function Parameters ---------- first_constant : int or float Coefficient of the reciprocal variable of the original hyperbolic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Coefficient of the constant term of the original hyperbolic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First two arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- roots : list of float List of the x-coordinates of all of the x-intercepts of the original function; if the function never crosses the x-axis, then it will return a list of `None` See Also -------- :func:`~regressions.analyses.equations.hyperbolic.hyperbolic_equation`, :func:`~regressions.analyses.derivatives.hyperbolic.hyperbolic_derivatives`, :func:`~regressions.analyses.integrals.hyperbolic.hyperbolic_integral`, :func:`~regressions.models.hyperbolic.hyperbolic_model` Notes ----- - Standard form of a hyperbolic function: :math:`f(x) = a\\cdot{\\frac{1}{x}} + b` - Hyperbolic formula: :math:`x = -\\frac{a}{b}` Examples -------- Import `hyperbolic_roots` function from `regressions` library >>> from regressions.analyses.roots.hyperbolic import hyperbolic_roots Calculate the roots of a hyperbolic function with coefficients 2 and 3 >>> roots_first = hyperbolic_roots(2, 3) >>> print(roots_first) [-0.6667] Calculate the roots of a hyperbolic function with coefficients -2 and 3 >>> roots_second = hyperbolic_roots(-2, 3) >>> print(roots_second) [0.6667] Calculate the roots of a hyperbolic function with all inputs set to 0 >>> roots_zeroes = hyperbolic_roots(0, 0) >>> print(roots_zeroes) [-1.0] """ # Handle input errors two_scalars(first_constant, second_constant) positive_integer(precision) coefficients = no_zeroes([first_constant, second_constant], precision) # Create list to return result = [] # Determine root root = -1 * coefficients[0] / coefficients[1] # Round root rounded_root = rounded_value(root, precision) # Return result result.append(rounded_root) return result
def sinusoidal_integral(first_constant, second_constant, third_constant, fourth_constant, precision=4): """ Generates the integral of a sinusoidal function Parameters ---------- first_constant : int or float Vertical stretch factor of the original sine function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Horizontal stretch factor of the original sine function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) third_constant : int or float Horizontal shift of the original sine function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) fourth_constant : int or float Vertical shift of the original sine function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First four arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- integral['constants'] : list of float Coefficients of the resultant integral integral['evaluation'] : func Function for evaluating the resultant integral at any float or integer argument See Also -------- :func:`~regressions.analyses.equations.sinusoidal.sinusoidal_equation`, :func:`~regressions.analyses.derivatives.sinusoidal.sinusoidal_derivatives`, :func:`~regressions.analyses.roots.sinusoidal.sinusoidal_roots`, :func:`~regressions.models.sinusoidal.sinusoidal_model` Notes ----- - Standard form of a sinusoidal function: :math:`f(x) = a\\cdot{\\sin(b\\cdot(x - c))} + d` - Integral of a sinusoidal function: :math:`F(x) = -\\frac{a}{b}\\cdot{\\cos(b\\cdot(x - c))} + d\\cdot{x}` - |indefinite_integral| - |integration_formulas| - |substitution_rule| Examples -------- Import `sinusoidal_integral` function from `regressions` library >>> from regressions.analyses.integrals.sinusoidal import sinusoidal_integral Generate the integral of a sinusoidal function with coefficients 2, 3, 5, and 7, then display its coefficients >>> integral_constants = sinusoidal_integral(2, 3, 5, 7) >>> print(integral_constants['constants']) [-0.6667, 3.0, 5.0, 7.0] Generate the integral of a sinusoidal function with coefficients 7, -5, -3, and 2, then evaluate its integral at 10 >>> integral_evaluation = sinusoidal_integral(7, -5, -3, 2) >>> print(integral_evaluation['evaluation'](10)) 19.2126 Generate the integral of a sinusoidal function with all inputs set to 0, then display its coefficients >>> integral_zeroes = sinusoidal_integral(0, 0, 0, 0) >>> print(integral_zeroes['constants']) [-1.0, 0.0001, 0.0001, 0.0001] """ # Handle input errors four_scalars(first_constant, second_constant, third_constant, fourth_constant) positive_integer(precision) coefficients = no_zeroes( [first_constant, second_constant, third_constant, fourth_constant], precision) # Create constants integral_coefficients = [ -1 * coefficients[0] / coefficients[1], coefficients[1], coefficients[2], coefficients[3] ] constants = rounded_list(integral_coefficients, precision) # Create evaluation def sinusoidal_evaluation(variable): evaluation = constants[0] * cos( constants[1] * (variable - constants[2])) + constants[3] * variable rounded_evaluation = rounded_value(evaluation, precision) return rounded_evaluation # Package constants and evaluation in single dictionary results = {'constants': constants, 'evaluation': sinusoidal_evaluation} return results
def test_no_zeroes_last0(self): no_zeroes_last0 = no_zeroes(last_zero_list) self.assertEqual(no_zeroes_last0, [1.0, 2.0, 0.0001])
def test_no_zeroes_all0_precision(self): no_zeroes_all0 = no_zeroes(all_zeroes_list, 6) self.assertEqual(no_zeroes_all0, [0.000001, 0.000001, 0.000001])
def test_no_zeroes_all0(self): no_zeroes_all0 = no_zeroes(all_zeroes_list) self.assertEqual(no_zeroes_all0, [0.0001, 0.0001, 0.0001])
def test_no_zeroes_first0(self): no_zeroes_first0 = no_zeroes(first_zero_list) self.assertEqual(no_zeroes_first0, [0.0001, 1.0, 2.0])
def test_no_zeroes_no0(self): no_zeroes_no0 = no_zeroes(no_zeroes_list) self.assertEqual(no_zeroes_no0, [1.0, 2.0, 3.0])
def cubic_roots(first_constant, second_constant, third_constant, fourth_constant, precision=4): """ Calculates the roots of a cubic function Parameters ---------- first_constant : int or float Coefficient of the cubic term of the original cubic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Coefficient of the quadratic term of the original cubic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) third_constant : int or float Coefficient of the linear term of the original cubic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) fourth_constant : int or float Coefficient of the constant term of the original cubic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First four arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- roots : list of float List of the x-coordinates of all of the x-intercepts of the original function See Also -------- :func:`~regressions.analyses.equations.cubic.cubic_equation`, :func:`~regressions.analyses.derivatives.cubic.cubic_derivatives`, :func:`~regressions.analyses.integrals.cubic.cubic_integral`, :func:`~regressions.models.cubic.cubic_model` Notes ----- - Standard form of a cubic function: :math:`f(x) = a\\cdot{x^3} + b\\cdot{x^2} + c\\cdot{x} + d` - Cubic formula: :math:`x_k = -\\frac{1}{3a}\\cdot(b + \\xi^k\\cdot{\\eta} + \\frac{\\Delta_0}{\\xi^k\\cdot{\\eta}})` - :math:`\\Delta_0 = b^2 - 3ac` - :math:`\\Delta_1 = 2b^3 - 9abc +27a^2d` - :math:`\\xi = \\frac{-1 + \\sqrt{-3}}{2}` - :math:`\\eta = \\sqrt[3]{\\frac{\\Delta_1 \\pm \\sqrt{\\Delta_1^2 - 4\\Delta_0^3}}{2}}` - :math:`k \\in \\{ 0, 1, 2 \\}` - |cubic_formula| Examples -------- Import `cubic_roots` function from `regressions` library >>> from regressions.analyses.roots.cubic import cubic_roots Calculate the roots of a cubic function with coefficients 2, 3, 5, and 7 >>> roots_first = cubic_roots(2, 3, 5, 7) >>> print(roots_first) [-1.4455] Calculate the roots of a cubic function with coefficients 7, -5, -3, and 2 >>> roots_second = cubic_roots(7, -5, -3, 2) >>> print(roots_second) [-0.6431, 0.551, 0.8064] Calculate the roots of a cubic function with all inputs set to 0 >>> roots_zeroes = cubic_roots(0, 0, 0, 0) >>> print(roots_zeroes) [-1.0] """ # Handle input errors four_scalars(first_constant, second_constant, third_constant, fourth_constant) positive_integer(precision) coefficients = no_zeroes( [first_constant, second_constant, third_constant, fourth_constant], precision) # Create intermediary variables xi = (-1 + (-3)**(1 / 2)) / 2 delta_first = coefficients[1]**2 - 3 * coefficients[0] * coefficients[2] delta_second = 2 * coefficients[1]**3 - 9 * coefficients[0] * coefficients[ 1] * coefficients[2] + 27 * coefficients[0]**2 * coefficients[3] discriminant = delta_second**2 - 4 * delta_first**3 eta_first = ((delta_second + discriminant**(1 / 2)) / 2)**(1 / 3) eta_second = ((delta_second - discriminant**(1 / 2)) / 2)**(1 / 3) eta = 0 if eta_first == 0: eta = eta_second else: eta = eta_first # Create roots roots = [] first_root = (-1 / (3 * coefficients[0])) * (coefficients[1] + eta * xi**0 + delta_first / (eta * xi**0)) second_root = (-1 / (3 * coefficients[0])) * (coefficients[1] + eta * xi**1 + delta_first / (eta * xi**1)) third_root = (-1 / (3 * coefficients[0])) * (coefficients[1] + eta * xi**2 + delta_first / (eta * xi**2)) # Identify real and imaginary components of complex roots first_real = first_root.real second_real = second_root.real third_real = third_root.real first_imag = first_root.imag second_imag = second_root.imag third_imag = third_root.imag # Determine magnitudes of imaginary components size_first_imag = (first_imag**2)**(1 / 2) size_second_imag = (second_imag**2)**(1 / 2) size_third_imag = (third_imag**2)**(1 / 2) # Eliminate roots with large imaginary components if size_first_imag < 10**(-precision): first_root = first_real roots.append(first_root) if size_second_imag < 10**(-precision): second_root = second_real roots.append(second_root) if size_third_imag < 10**(-precision): third_root = third_real roots.append(third_root) # Eliminate duplicate roots unique_roots = list(set(roots)) # Sort unique roots sorted_roots = sorted_list(unique_roots) # Round roots rounded_roots = rounded_list(sorted_roots, precision) # Return result result = rounded_roots return result
def logistic_derivatives(first_constant, second_constant, third_constant, precision=4): """ Calculates the first and second derivatives of a logistic function Parameters ---------- first_constant : int or float Carrying capacity of the original logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Growth rate of the original logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) third_constant : int or float Value of the sigmoid's midpoint of the original logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First three arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- derivatives['first']['constants'] : list of float Coefficients of the resultant first derivative derivatives['first']['evaluation'] : func Function for evaluating the resultant first derivative at any float or integer argument derivatives['second']['constants'] : list of float Coefficients of the resultant second derivative derivatives['second']['evaluation'] : func Function for evaluating the resultant second derivative at any float or integer argument See Also -------- :func:`~regressions.analyses.equations.logistic.logistic_equation`, :func:`~regressions.analyses.integrals.logistic.logistic_integral`, :func:`~regressions.analyses.roots.logistic.logistic_roots`, :func:`~regressions.models.logistic.logistic_model` Notes ----- - Standard form of a logistic function: :math:`f(x) = \\frac{a}{1 + \\text{e}^{-b\\cdot(x - c)}}` - First derivative of a logistic function: :math:`f'(x) = \\frac{ab\\cdot{\\text{e}^{-b\\cdot(x - c)}}}{(1 + \\text{e}^{-b\\cdot(x - c)})^2}` - Second derivative of a logistic function: :math:`f''(x) = \\frac{2ab^2\\cdot{\\text{e}^{-2b\\cdot(x - c)}}}{(1 + \\text{e}^{-b\\cdot(x - c)})^3} - \\frac{ab^2\\cdot{\\text{e}^{-b\\cdot(x - c)}}}{(1 + \\text{e}^{-b\\cdot(x - c)})^2}` - |differentiation_formulas| - |chain_rule| - |exponential| Examples -------- Import `logistic_derivatives` function from `regressions` library >>> from regressions.analyses.derivatives.logistic import logistic_derivatives Generate the derivatives of a logistic function with coefficients 2, 3, and 5, then display the coefficients of its first and second derivatives >>> derivatives_constants = logistic_derivatives(2, 3, 5) >>> print(derivatives_constants['first']['constants']) [6.0, 3.0, 5.0] >>> print(derivatives_constants['second']['constants']) [18.0, 3.0, 5.0] Generate the derivatives of a logistic function with coefficients 100, 5, and 11, then evaluate its first and second derivatives at 10 >>> derivatives_evaluation = logistic_derivatives(100, 5, 11) >>> print(derivatives_evaluation['first']['evaluation'](10)) 3.324 >>> print(derivatives_evaluation['second']['evaluation'](10)) 16.3977 Generate the derivatives of a logistic function with all inputs set to 0, then display the coefficients of its first and second derivatives >>> derivatives_zeroes = logistic_derivatives(0, 0, 0) >>> print(derivatives_zeroes['first']['constants']) [0.0001, 0.0001, 0.0001] >>> print(derivatives_zeroes['second']['constants']) [0.0001, 0.0001, 0.0001] """ # Handle input errors three_scalars(first_constant, second_constant, third_constant) positive_integer(precision) coefficients = no_zeroes([first_constant, second_constant, third_constant], precision) # Create first derivative first_coefficients = [ coefficients[0] * coefficients[1], coefficients[1], coefficients[2] ] first_constants = rounded_list(first_coefficients, precision) def first_derivative(variable): exponential = exp(-1 * first_constants[1] * (variable - first_constants[2])) evaluation = first_constants[0] * exponential * (1 + exponential)**(-2) rounded_evaluation = rounded_value(evaluation, precision) return rounded_evaluation first_dictionary = { 'constants': first_constants, 'evaluation': first_derivative } # Create second derivative second_coefficients = [ first_constants[0] * first_constants[1], first_constants[1], first_constants[2] ] second_constants = rounded_list(second_coefficients, precision) def second_derivative(variable): exponential = exp(-1 * second_constants[1] * (variable - second_constants[2])) evaluation = second_constants[0] * exponential * (1 + exponential)**( -2) * (2 * exponential / (1 + exponential) - 1) rounded_evaluation = rounded_value(evaluation, precision) return rounded_evaluation second_dictionary = { 'constants': second_constants, 'evaluation': second_derivative } # Package both derivatives in single dictionary results = {'first': first_dictionary, 'second': second_dictionary} return results
def logistic_integral(first_constant, second_constant, third_constant, precision=4): """ Generates the integral of a logistic function Parameters ---------- first_constant : int or float Carrying capacity of the original logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Growth rate of the original logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) third_constant : int or float Value of the sigmoid's midpoint of the original logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First three arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- integral['constants'] : list of float Coefficients of the resultant integral integral['evaluation'] : func Function for evaluating the resultant integral at any float or integer argument See Also -------- :func:`~regressions.analyses.equations.logistic.logistic_equation`, :func:`~regressions.analyses.derivatives.logistic.logistic_derivatives`, :func:`~regressions.analyses.roots.logistic.logistic_roots`, :func:`~regressions.models.logistic.logistic_model` Notes ----- - Standard form of a logistic function: :math:`f(x) = \\frac{a}{1 + \\text{e}^{-b\\cdot(x - c)}}` - Integral of a logistic function: :math:`F(x) = \\frac{a}{b}\\cdot{\\ln|\\text{e}^{b\\cdot(x - c)} + 1|}` - |indefinite_integral| - |integration_formulas| - |substitution_rule| Examples -------- Import `logistic_integral` function from `regressions` library >>> from regressions.analyses.integrals.logistic import logistic_integral Generate the integral of a logistic function with coefficients 2, 3, and 5, then display its coefficients >>> integral_constants = logistic_integral(2, 3, 5) >>> print(integral_constants['constants']) [0.6667, 3.0, 5.0] Generate the integral of a logistic function with coefficients 100, 5, and 11, then evaluate its integral at 10 >>> integral_evaluation = logistic_integral(100, 5, 11) >>> print(integral_evaluation['evaluation'](10)) 0.1343 Generate the integral of a logistic function with all inputs set to 0, then display its coefficients >>> integral_zeroes = logistic_integral(0, 0, 0) >>> print(integral_zeroes['constants']) [1.0, 0.0001, 0.0001] """ # Handle input errors three_scalars(first_constant, second_constant, third_constant) positive_integer(precision) coefficients = no_zeroes([first_constant, second_constant, third_constant], precision) # Create constants integral_coefficients = [ coefficients[0] / coefficients[1], coefficients[1], coefficients[2] ] constants = rounded_list(integral_coefficients, precision) # Create evaluation def logistic_evaluation(variable): evaluation = constants[0] * log( abs(exp(constants[1] * (variable - constants[2])) + 1)) rounded_evaluation = rounded_value(evaluation, precision) return rounded_evaluation # Package constants and evaluation in single dictionary results = {'constants': constants, 'evaluation': logistic_evaluation} return results
def sinusoidal_roots(first_constant, second_constant, third_constant, fourth_constant, precision = 4): """ Calculates the roots of a sinusoidal function Parameters ---------- first_constant : int or float Vertical stretch factor of the original sine function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Horizontal stretch factor of the original sine function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) third_constant : int or float Horizontal shift of the original sine function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) fourth_constant : int or float Vertical shift of the original sine function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First four arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- roots : list of float or str List of the x-coordinates of the initial x-intercepts within two periods of the original function in float format, along with the general forms in string format that can be used to determine all other x-intercepts by plugging in any integer value for 'k' and evaluating; if the function never crosses the x-axis, then it will return a list of `None` See Also -------- :func:`~regressions.analyses.equations.sinusoidal.sinusoidal_equation`, :func:`~regressions.analyses.derivatives.sinusoidal.sinusoidal_derivatives`, :func:`~regressions.analyses.integrals.sinusoidal.sinusoidal_integral`, :func:`~regressions.models.sinusoidal.sinusoidal_model` Notes ----- - Standard form of a sinusoidal function: :math:`f(x) = a\\cdot{\\sin(b\\cdot(x - c))} + d` - Sinusoidal formula: :math:`x_0 = c + \\frac{1}{b}\\cdot{\\sin^{-1}(-\\frac{d}{a})} + \\frac{2\\pi}{b}\\cdot{k}` - :math:`\\text{if} -1 < -\\frac{d}{a} < 0 \\text{ or } 0 < -\\frac{d}{a} < 1, x_1 = c + \\frac{\\pi}{b} - \\frac{1}{b}\\cdot{\\sin^{-1}(-\\frac{d}{a})} + \\frac{2\\pi}{b}\\cdot{k}` - :math:`\\text{if} -\\frac{d}{a} = 0, x_1 = c - \\frac{\\pi}{b} + \\frac{2\\pi}{b}\\cdot{k}` - :math:`k \\in \\mathbb{Z}` Examples -------- Import `sinusoidal_roots` function from `regressions` library >>> from regressions.analyses.roots.sinusoidal import sinusoidal_roots Calculate the roots of a sinusoidal function with coefficients 2, 3, 5, and 7 >>> roots_first = sinusoidal_roots(2, 3, 5, 7) >>> print(roots_first) [None] Calculate the roots of a sinusoidal function with coefficients 7, -5, -3, and 2 >>> roots_second = sinusoidal_roots(7, -5, -3, 2) >>> print(roots_second) [-8.7128, -7.9686, -7.4562, -6.712, -6.1995, -5.4553, -4.9429, -4.1987, -3.6863, -2.942, '-3.6863 + 1.2566k', '-2.942 + 1.2566k'] Calculate the roots of a sinusoidal function with all inputs set to 0 >>> roots_zeroes = sinusoidal_roots(0, 0, 0, 0) >>> print(roots_zeroes) [-15707.9632, 47123.8899, 109955.743, 172787.596, 235619.4491, '-15707.9632 + 62831.8531k'] """ # Handle input errors four_scalars(first_constant, second_constant, third_constant, fourth_constant) positive_integer(precision) coefficients = no_zeroes([first_constant, second_constant, third_constant, fourth_constant], precision) # Create list for roots roots = [] # Identify key ratio ratio = -1 * coefficients[3] / coefficients[0] # Handle no roots if ratio > 1 or ratio < -1: roots = [None] # Handle multiple roots else: # Create intermediary variables radians = asin(ratio) periodic_radians = radians / coefficients[1] # Determine pertinent values periodic_unit = 2 * pi / coefficients[1] initial_value = coefficients[2] + periodic_radians roots = generate_elements(initial_value, periodic_unit, precision) # Handle roots that bounce on the x-axis if ratio == 1 or ratio == -1: pass # Handle roots that cross the x-axis else: # Determine supplementary values alternative_initial_value = coefficients[2] + pi / coefficients[1] - periodic_radians generated_elements = generate_elements(alternative_initial_value, periodic_unit, precision) # Add additional results to roots list roots.extend(generated_elements) # Separate numerical roots, string roots, and None results separated_roots = separate_elements(roots) numerical_roots = separated_roots['numerical'] other_roots = separated_roots['other'] # Sort numerical roots sorted_roots = sorted_list(numerical_roots) # Round numerical roots rounded_roots = rounded_list(sorted_roots, precision) # Sort other_roots sorted_other_roots = sorted_strings(other_roots) # Combine numerical and non-numerical roots result = rounded_roots + sorted_other_roots return result
def logistic_roots(first_constant, second_constant, third_constant, precision=4): """ Calculates the roots of a logistic function Parameters ---------- first_constant : int or float Carrying capacity of the original logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Growth rate of the original logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) third_constant : int or float Value of the sigmoid's midpoint of the original logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First three arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- roots : list of float List of the x-coordinates of all of the x-intercepts of the original function; if the function never crosses the x-axis, then it will return a list of `None` See Also -------- :func:`~regressions.analyses.equations.logistic.logistic_equation`, :func:`~regressions.analyses.derivatives.logistic.logistic_derivatives`, :func:`~regressions.analyses.integrals.logistic.logistic_integral`, :func:`~regressions.models.logistic.logistic_model` Notes ----- - Standard form of a logistic function: :math:`f(x) = \\frac{a}{1 + \\text{e}^{-b\\cdot(x - c)}}` - Logistic formula: :math:`x = \\varnothing` Examples -------- Import `logistic_roots` function from `regressions` library >>> from regressions.analyses.roots.logistic import logistic_roots Calculate the roots of a logistic function with coefficients 2, 3, and 5 >>> roots_first = logistic_roots(2, 3, 5) >>> print(roots_first) [None] Calculate the roots of a logistic function with coefficients 100, 5, and 11 >>> roots_second = logistic_roots(100, 5, 11) >>> print(roots_second) [None] Calculate the roots of a logistic function with all inputs set to 0 >>> roots_zeroes = logistic_roots(0, 0, 0) >>> print(roots_zeroes) [None] """ # Handle input errors three_scalars(first_constant, second_constant, third_constant) positive_integer(precision) coefficients = no_zeroes([first_constant, second_constant, third_constant], precision) # Create list to return result = [] # Determine root root = None # Return result result.append(root) return result
def exponential_derivatives(first_constant, second_constant, precision=4): """ Calculates the first and second derivatives of an exponential function Parameters ---------- first_constant : int or float Constant multiple of the original exponential function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Base rate of variable of the original exponential function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First two arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- derivatives['first']['constants'] : list of float Coefficients of the resultant first derivative derivatives['first']['evaluation'] : func Function for evaluating the resultant first derivative at any float or integer argument derivatives['second']['constants'] : list of float Coefficients of the resultant second derivative derivatives['second']['evaluation'] : func Function for evaluating the resultant second derivative at any float or integer argument See Also -------- :func:`~regressions.analyses.equations.exponential.exponential_equation`, :func:`~regressions.analyses.integrals.exponential.exponential_integral`, :func:`~regressions.analyses.roots.exponential.exponential_roots`, :func:`~regressions.models.exponential.exponential_model` Notes ----- - Standard form of an exponential function: :math:`f(x) = a\\cdot{b^x}` - First derivative of an exponential function: :math:`f'(x) = a\\cdot{\\ln{b}\\cdot{b^x}}` - Second derivative of an exponential function: :math:`f''(x) = a\\cdot{\\ln^2{b}\\cdot{b^x}}` - |differentiation_formulas| - |exponential| Examples -------- Import `exponential_derivatives` function from `regressions` library >>> from regressions.analyses.derivatives.exponential import exponential_derivatives Generate the derivatives of an exponential function with coefficients 2 and 3, then display the coefficients of its first and second derivatives >>> derivatives_constants = exponential_derivatives(2, 3) >>> print(derivatives_constants['first']['constants']) [2.1972, 3.0] >>> print(derivatives_constants['second']['constants']) [2.4139, 3.0] Generate the derivatives of an exponential function with coefficients -2 and 3, then evaluate its first and second derivatives at 10 >>> derivatives_evaluation = exponential_derivatives(-2, 3) >>> print(derivatives_evaluation['first']['evaluation'](10)) -129742.4628 >>> print(derivatives_evaluation['second']['evaluation'](10)) -142538.3811 Generate the derivatives of an exponential function with all inputs set to 0, then display the coefficients of its first and second derivatives >>> derivatives_zeroes = exponential_derivatives(0, 0) >>> print(derivatives_zeroes['first']['constants']) [-0.0009, 0.0001] >>> print(derivatives_zeroes['second']['constants']) [0.0083, 0.0001] """ # Handle input errors two_scalars(first_constant, second_constant) positive_integer(precision) coefficients = no_zeroes([first_constant, second_constant], precision) # Create first derivative first_coefficients = [ coefficients[0] * log(abs(coefficients[1])), coefficients[1] ] first_constants = rounded_list(first_coefficients, precision) def first_derivative(variable): evaluation = first_constants[0] * first_constants[1]**variable rounded_evaluation = rounded_value(evaluation, precision) return rounded_evaluation first_dictionary = { 'constants': first_constants, 'evaluation': first_derivative } # Create second derivative second_coefficients = [ first_constants[0] * log(abs(first_constants[1])), first_constants[1] ] second_constants = rounded_list(second_coefficients, precision) def second_derivative(variable): evaluation = second_constants[0] * second_constants[1]**variable rounded_evaluation = rounded_value(evaluation, precision) return rounded_evaluation second_dictionary = { 'constants': second_constants, 'evaluation': second_derivative } # Package both derivatives in single dictionary results = {'first': first_dictionary, 'second': second_dictionary} return results
def exponential_integral(first_constant, second_constant, precision = 4): """ Generates the integral of an exponential function Parameters ---------- first_constant : int or float Constant multiple of the original exponential function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Base rate of variable of the original exponential function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001); if one, it will be converted to a small, near-one decimal value (e.g., 1.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First two arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- integral['constants'] : list of float Coefficients of the resultant integral integral['evaluation'] : func Function for evaluating the resultant integral at any float or integer argument See Also -------- :func:`~regressions.analyses.equations.exponential.exponential_equation`, :func:`~regressions.analyses.derivatives.exponential.exponential_derivatives`, :func:`~regressions.analyses.roots.exponential.exponential_roots`, :func:`~regressions.models.exponential.exponential_model` Notes ----- - Standard form of an exponential function: :math:`f(x) = a\\cdot{b^x}` - Integral of an exponential function: :math:`F(x) = \\frac{a}{\\ln{b}}\\cdot{b^x}` - |indefinite_integral| - |integration_formulas| Examples -------- Import `exponential_integral` function from `regressions` library >>> from regressions.analyses.integrals.exponential import exponential_integral Generate the integral of an exponential function with coefficients 2 and 3, then display its coefficients >>> integral_constants = exponential_integral(2, 3) >>> print(integral_constants['constants']) [1.8205, 3.0] Generate the integral of an exponential function with coefficients -2 and 3, then evaluate its integral at 10 >>> integral_evaluation = exponential_integral(-2, 3) >>> print(integral_evaluation['evaluation'](10)) -107498.7045 Generate the integral of an exponential function with all inputs set to 0, then display its coefficients >>> integral_zeroes = exponential_integral(0, 0) >>> print(integral_zeroes['constants']) [-0.0001, 0.0001] """ # Handle input errors two_scalars(first_constant, second_constant) positive_integer(precision) coefficients = no_zeroes([first_constant, second_constant], precision) # Circumvent division by zero if coefficients[1] == 1: coefficients[1] = 1 + 10**(-precision) # Create constants integral_coefficients = [coefficients[0] / log(abs(coefficients[1])), coefficients[1]] constants = rounded_list(integral_coefficients, precision) # Create evaluation def exponential_evaluation(variable): evaluation = constants[0] * constants[1]**variable rounded_evaluation = rounded_value(evaluation, precision) return rounded_evaluation # Package constants and evaluation in single dictionary results = { 'constants': constants, 'evaluation': exponential_evaluation } return results
def linear_integral(first_constant, second_constant, precision = 4): """ Generates the integral of a linear function Parameters ---------- first_constant : int or float Coefficient of the linear term of the original linear function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Coefficient of the constant term of the original linear function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First two arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- integral['constants'] : list of float Coefficients of the resultant integral integral['evaluation'] : func Function for evaluating the resultant integral at any float or integer argument See Also -------- :func:`~regressions.analyses.equations.linear.linear_equation`, :func:`~regressions.analyses.derivatives.linear.linear_derivatives`, :func:`~regressions.analyses.roots.linear.linear_roots`, :func:`~regressions.models.linear.linear_model` Notes ----- - Standard form of a linear function: :math:`f(x) = a\\cdot{x} + b` - Integral of a linear function: :math:`F(x) = \\frac{a}{2}\\cdot{x^2} + b\\cdot{x}` - |indefinite_integral| - |integration_formulas| Examples -------- Import `linear_integral` function from `regressions` library >>> from regressions.analyses.integrals.linear import linear_integral Generate the integral of a linear function with coefficients 2 and 3, then display its coefficients >>> integral_constants = linear_integral(2, 3) >>> print(integral_constants['constants']) [1.0, 3.0] Generate the integral of a linear function with coefficients -2 and 3, then evaluate its integral at 10 >>> integral_evaluation = linear_integral(-2, 3) >>> print(integral_evaluation['evaluation'](10)) -70.0 Generate the integral of a linear function with all inputs set to 0, then display its coefficients >>> integral_zeroes = linear_integral(0, 0) >>> print(integral_zeroes['constants']) [0.0001, 0.0001] """ # Handle input errors two_scalars(first_constant, second_constant) positive_integer(precision) coefficients = no_zeroes([first_constant, second_constant], precision) # Create constants integral_coefficients = [(1/2) * coefficients[0], coefficients[1]] constants = rounded_list(integral_coefficients, precision) # Create evaluation def linear_evaluation(variable): evaluation = constants[0] * variable**2 + constants[1] * variable rounded_evaluation = rounded_value(evaluation, precision) return rounded_evaluation # Package constants and evaluation in single dictionary results = { 'constants': constants, 'evaluation': linear_evaluation } return results
def cubic_equation(first_constant, second_constant, third_constant, fourth_constant, precision=4): """ Generates a cubic function to provide evaluations at variable inputs Parameters ---------- first_constant : int or float Coefficient of the cubic term of the resultant cubic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Coefficient of the quadratic term of the resultant cubic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) third_constant : int or float Coefficient of the linear term of the resultant cubic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) fourth_constant : int or float Coefficient of the constant term of the resultant cubic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First four arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- evaluation : func Function for evaluating a cubic equation when passed any integer or float argument See Also -------- :func:`~regressions.analyses.derivatives.cubic.cubic_derivatives`, :func:`~regressions.analyses.integrals.cubic.cubic_integral`, :func:`~regressions.analyses.roots.cubic.cubic_roots`, :func:`~regressions.models.cubic.cubic_model` Notes ----- - Standard form of a cubic function: :math:`f(x) = a\\cdot{x^3} + b\\cdot{x^2} + c\\cdot{x} + d` - |cubic_functions| Examples -------- Import `cubic_equation` function from `regressions` library >>> from regressions.analyses.equations.cubic import cubic_equation Create a cubic function with coefficients 2, 3, 5, and 7, then evaluate it at 10 >>> evaluation_first = cubic_equation(2, 3, 5, 7) >>> print(evaluation_first(10)) 2357.0 Create a cubic function with coefficients 7, -5, -3, and 2, then evaluate it at 10 >>> evaluation_second = cubic_equation(7, -5, -3, 2) >>> print(evaluation_second(10)) 6472.0 Create a cubic function with all inputs set to 0, then evaluate it at 10 >>> evaluation_zero = cubic_equation(0, 0, 0, 0) >>> print(evaluation_zero(10)) 0.1111 """ # Handle input errors four_scalars(first_constant, second_constant, third_constant, fourth_constant) positive_integer(precision) coefficients = no_zeroes( [first_constant, second_constant, third_constant, fourth_constant], precision) # Create evaluation def cubic_evaluation(variable): evaluation = coefficients[0] * variable**3 + coefficients[ 1] * variable**2 + coefficients[2] * variable + coefficients[3] result = rounded_value(evaluation, precision) return result return cubic_evaluation
def exponential_equation(first_constant, second_constant, precision=4): """ Generates an exponential function to provide evaluations at variable inputs Parameters ---------- first_constant : int or float Constant multiple of the resultant exponential function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Base rate of variable of the resultant exponential function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First two arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- evaluation : func Function for evaluating an exponential equation when passed any integer or float argument See Also -------- :func:`~regressions.analyses.derivatives.exponential.exponential_derivatives`, :func:`~regressions.analyses.integrals.exponential.exponential_integral`, :func:`~regressions.analyses.roots.exponential.exponential_roots`, :func:`~regressions.models.exponential.exponential_model` Notes ----- - Standard form of an exponential function: :math:`f(x) = a\\cdot{b^x}` - |exponential_functions| Examples -------- Import `exponential_equation` function from `regressions` library >>> from regressions.analyses.equations.exponential import exponential_equation Create an exponential function with coefficients 2 and 3, then evaluate it at 10 >>> evaluation_first = exponential_equation(2, 3) >>> print(evaluation_first(10)) 118098.0 Create an exponential function with coefficients -2 and 3, then evaluate it at 10 >>> evaluation_second = exponential_equation(-2, 3) >>> print(evaluation_second(10)) -118098.0 Create an exponential function with all inputs set to 0, then evaluate it at 10 >>> evaluation_zero = exponential_equation(0, 0) >>> print(evaluation_zero(10)) 0.0001 """ # Handle input errors two_scalars(first_constant, second_constant) positive_integer(precision) coefficients = no_zeroes([first_constant, second_constant], precision) # Create evaluation def exponential_evaluation(variable): evaluation = coefficients[0] * coefficients[1]**variable result = rounded_value(evaluation, precision) return result return exponential_evaluation
def quadratic_roots(first_constant, second_constant, third_constant, precision=4): """ Calculates the roots of a quadratic function Parameters ---------- first_constant : int or float Coefficient of the quadratic term of the original quadratic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Coefficient of the linear term of the original quadratic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) third_constant : int or float Coefficient of the constant term of the original quadratic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First three arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- roots : list of float List of the x-coordinates of all of the x-intercepts of the original function; if the function never crosses the x-axis, then it will return a list of `None` See Also -------- :func:`~regressions.analyses.equations.quadratic.quadratic_equation`, :func:`~regressions.analyses.derivatives.quadratic.quadratic_derivatives`, :func:`~regressions.analyses.integrals.quadratic.quadratic_integral`, :func:`~regressions.models.quadratic.quadratic_model` Notes ----- - Standard form of a quadratic function: :math:`f(x) = a\\cdot{x^2} + b\\cdot{x} + c` - Quadratic formula: :math:`x = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}` - |quadratic_formula| Examples -------- Import `quadratic_roots` function from `regressions` library >>> from regressions.analyses.roots.quadratic import quadratic_roots Calculate the roots of a quadratic function with coefficients 2, 7, and 5 >>> roots_first = quadratic_roots(2, 7, 5) >>> print(roots_first) [-2.5, -1.0] Calculate the roots of a quadratic function with coefficients 2, -5, and 3 >>> roots_second = quadratic_roots(2, -5, 3) >>> print(roots_second) [1.0, 1.5] Calculate the roots of a quadratic function with all inputs set to 0 >>> roots_zeroes = quadratic_roots(0, 0, 0) >>> print(roots_zeroes) [None] """ # Handle input errors three_scalars(first_constant, second_constant, third_constant) positive_integer(precision) coefficients = no_zeroes([first_constant, second_constant, third_constant], precision) # Create intermediary list and list to return roots = [] result = [] # Create intermediary variable discriminant = coefficients[1]**2 - 4 * coefficients[0] * coefficients[2] # Create roots first_root = (-1 * coefficients[1] + discriminant**(1 / 2)) / (2 * coefficients[0]) second_root = (-1 * coefficients[1] - discriminant**(1 / 2)) / (2 * coefficients[0]) # Eliminate duplicate roots if first_root == second_root: roots.append(first_root) # Eliminate complex roots else: if not isinstance(first_root, complex): roots.append(first_root) if not isinstance(second_root, complex): roots.append(second_root) # Handle no roots if not roots: roots.append(None) # Sort roots sorted_roots = sorted_list(roots) # Round roots rounded_roots = rounded_list(sorted_roots, precision) # Return result result.extend(rounded_roots) return result
def cubic_integral(first_constant, second_constant, third_constant, fourth_constant, precision=4): """ Generates the integral of a cubic function Parameters ---------- first_constant : int or float Coefficient of the cubic term of the original cubic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Coefficient of the quadratic term of the original cubic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) third_constant : int or float Coefficient of the linear term of the original cubic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) fourth_constant : int or float Coefficient of the constant term of the original cubic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First four arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- integral['constants'] : list of float Coefficients of the resultant integral integral['evaluation'] : func Function for evaluating the resultant integral at any float or integer argument See Also -------- :func:`~regressions.analyses.equations.cubic.cubic_equation`, :func:`~regressions.analyses.derivatives.cubic.cubic_derivatives`, :func:`~regressions.analyses.roots.cubic.cubic_roots`, :func:`~regressions.models.cubic.cubic_model` Notes ----- - Standard form of a cubic function: :math:`f(x) = a\\cdot{x^3} + b\\cdot{x^2} + c\\cdot{x} + d` - Integral of a cubic function: :math:`F(x) = \\frac{a}{4}\\cdot{x^4} + \\frac{b}{3}\\cdot{x^3} + \\frac{c}{2}\\cdot{x^2} + d\\cdot{x}` - |indefinite_integral| - |integration_formulas| Examples -------- Import `cubic_integral` function from `regressions` library >>> from regressions.analyses.integrals.cubic import cubic_integral Generate the integral of a cubic function with coefficients 2, 3, 5, and 7, then display its coefficients >>> integral_constants = cubic_integral(2, 3, 5, 7) >>> print(integral_constants['constants']) [0.5, 1.0, 2.5, 7.0] Generate the integral of a cubic function with coefficients 7, -5, -3, and 2, then evaluate its integral at 10 >>> integral_evaluation = cubic_integral(7, -5, -3, 2) >>> print(integral_evaluation['evaluation'](10)) 15703.3 Generate the integral of a cubic function with all inputs set to 0, then display its coefficients >>> integral_zeroes = cubic_integral(0, 0, 0, 0) >>> print(integral_zeroes['constants']) [0.0001, 0.0001, 0.0001, 0.0001] """ # Handle input errors four_scalars(first_constant, second_constant, third_constant, fourth_constant) positive_integer(precision) coefficients = no_zeroes( [first_constant, second_constant, third_constant, fourth_constant], precision) # Generate constants integral_coefficients = [(1 / 4) * coefficients[0], (1 / 3) * coefficients[1], (1 / 2) * coefficients[2], coefficients[3]] constants = rounded_list(integral_coefficients, precision) # Create evaluation def cubic_evaluation(variable): evaluation = constants[0] * variable**4 + constants[ 1] * variable**3 + constants[2] * variable**2 + constants[ 3] * variable rounded_evaluation = rounded_value(evaluation, precision) return rounded_evaluation # Package constants and evaluation in single dictionary results = {'constants': constants, 'evaluation': cubic_evaluation} return results
def sinusoidal_derivatives(first_constant, second_constant, third_constant, fourth_constant, precision=4): """ Calculates the first and second derivatives of a sinusoidal function Parameters ---------- first_constant : int or float Vertical stretch factor of the original sine function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Horizontal stretch factor of the original sine function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) third_constant : int or float Horizontal shift of the original sine function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) fourth_constant : int or float Vertical shift of the original sine function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First four arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- derivatives['first']['constants'] : list of float Coefficients of the resultant first derivative derivatives['first']['evaluation'] : func Function for evaluating the resultant first derivative at any float or integer argument derivatives['second']['constants'] : list of float Coefficients of the resultant second derivative derivatives['second']['evaluation'] : func Function for evaluating the resultant second derivative at any float or integer argument See Also -------- :func:`~regressions.analyses.equations.sinusoidal.sinusoidal_equation`, :func:`~regressions.analyses.integrals.sinusoidal.sinusoidal_integral`, :func:`~regressions.analyses.roots.sinusoidal.sinusoidal_roots`, :func:`~regressions.models.sinusoidal.sinusoidal_model` Notes ----- - Standard form of a sinusoidal function: :math:`f(x) = a\\cdot{\\sin(b\\cdot(x - c))} + d` - First derivative of a sinusoidal function: :math:`f'(x) = ab\\cdot{\\cos(b\\cdot(x - c))}` - Second derivative of a sinusoidal function: :math:`f''(x) = -ab^2\\cdot{\\sin(b\\cdot(x - c))}` - |differentiation_formulas| - |chain_rule| - |trigonometric| Examples -------- Import `sinusoidal_derivatives` function from `regressions` library >>> from regressions.analyses.derivatives.sinusoidal import sinusoidal_derivatives Generate the derivatives of a sinusoidal function with coefficients 2, 3, 5, and 7, then display the coefficients of its first and second derivatives >>> derivatives_constants = sinusoidal_derivatives(2, 3, 5, 7) >>> print(derivatives_constants['first']['constants']) [6.0, 3.0, 5.0] >>> print(derivatives_constants['second']['constants']) [-18.0, 3.0, 5.0] Generate the derivatives of a sinusoidal function with coefficients 7, -5, -3, and 2, then evaluate its first and second derivatives at 10 >>> derivatives_evaluation = sinusoidal_derivatives(7, -5, -3, 2) >>> print(derivatives_evaluation['first']['evaluation'](10)) 19.6859 >>> print(derivatives_evaluation['second']['evaluation'](10)) 144.695 Generate the derivatives of a sinusoidal function with all inputs set to 0, then display the coefficients of its first and second derivatives >>> derivatives_zeroes = sinusoidal_derivatives(0, 0, 0, 0) >>> print(derivatives_zeroes['first']['constants']) [0.0001, 0.0001, 0.0001] >>> print(derivatives_zeroes['second']['constants']) [-0.0001, 0.0001, 0.0001] """ # Handle input errors four_scalars(first_constant, second_constant, third_constant, fourth_constant) positive_integer(precision) coefficients = no_zeroes( [first_constant, second_constant, third_constant, fourth_constant], precision) # Create first derivative first_coefficients = [ coefficients[0] * coefficients[1], coefficients[1], coefficients[2] ] first_constants = rounded_list(first_coefficients, precision) def first_derivative(variable): evaluation = first_constants[0] * cos(first_constants[1] * (variable - first_constants[2])) rounded_evaluation = rounded_value(evaluation, precision) return rounded_evaluation first_dictionary = { 'constants': first_constants, 'evaluation': first_derivative } # Create second derivative second_coefficients = [ -1 * first_constants[0] * first_constants[1], first_constants[1], first_constants[2] ] second_constants = rounded_list(second_coefficients, precision) def second_derivative(variable): evaluation = second_constants[0] * sin( second_constants[1] * (variable - second_constants[2])) rounded_evaluation = rounded_value(evaluation, precision) return rounded_evaluation second_dictionary = { 'constants': second_constants, 'evaluation': second_derivative } # Package both derivatives in single dictionary results = {'first': first_dictionary, 'second': second_dictionary} return results
def logarithmic_equation(first_constant, second_constant, precision = 4): """ Generates a logarithmic function to provide evaluations at variable inputs Parameters ---------- first_constant : int or float Coefficient of the logarithmic term of the resultant logarithmic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Coefficient of the constant term of the resultant logarithmic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First two arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- evaluation : func Function for evaluating a logarithmic equation when passed any integer or float argument; if zero inputted as argument, it will be converted to a small, non-zero decimal value (e.g., 0.0001) See Also -------- :func:`~regressions.analyses.derivatives.logarithmic.logarithmic_derivatives`, :func:`~regressions.analyses.integrals.logarithmic.logarithmic_integral`, :func:`~regressions.analyses.roots.logarithmic.logarithmic_roots`, :func:`~regressions.models.logarithmic.logarithmic_model` Notes ----- - Standard form of a logarithmic function: :math:`f(x) = a\\cdot{\\ln{x}} + b` - |logarithmic_functions| Examples -------- Import `logarithmic_equation` function from `regressions` library >>> from regressions.analyses.equations.logarithmic import logarithmic_equation Create a logarithmic function with coefficients 2 and 3, then evaluate it at 10 >>> evaluation_first = logarithmic_equation(2, 3) >>> print(evaluation_first(10)) 7.6052 Create a logarithmic function with coefficients -2 and 3, then evaluate it at 10 >>> evaluation_second = logarithmic_equation(-2, 3) >>> print(evaluation_second(10)) -1.6052 Create a logarithmic function with all inputs set to 0, then evaluate it at 10 >>> evaluation_zero = logarithmic_equation(0, 0) >>> print(evaluation_zero(10)) 0.0003 """ # Handle input errors two_scalars(first_constant, second_constant) positive_integer(precision) coefficients = no_zeroes([first_constant, second_constant], precision) # Create evaluation def logarithmic_evaluation(variable): # Circumvent logarithm of zero if variable == 0: variable = 10**(-precision) evaluation = coefficients[0] * log(abs(variable)) + coefficients[1] result = rounded_value(evaluation, precision) return result return logarithmic_evaluation
def sinusoidal_equation(first_constant, second_constant, third_constant, fourth_constant, precision = 4): """ Generates a sinusoidal function to provide evaluations at variable inputs Parameters ---------- first_constant : int or float Vertical stretch factor of the resultant sine function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Horizontal stretch factor of the resultant sine function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) third_constant : int or float Horizontal shift of the resultant sine function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) fourth_constant : int or float Vertical shift of the resultant sine function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First four arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- evaluation : func Function for evaluating a sinusoidal equation when passed any integer or float argument See Also -------- :func:`~regressions.analyses.derivatives.sinusoidal.sinusoidal_derivatives`, :func:`~regressions.analyses.integrals.sinusoidal.sinusoidal_integral`, :func:`~regressions.analyses.roots.sinusoidal.sinusoidal_roots`, :func:`~regressions.models.sinusoidal.sinusoidal_model` Notes ----- - Standard form of a sinusoidal function: :math:`f(x) = a\\cdot{\\sin(b\\cdot(x - c))} + d` - Period of function: :math:`\\frac{2\\pi}{|b|}` - Amplitude of function: :math:`|a|` - |sine_functions| Examples -------- Import `sinusoidal_equation` function from `regressions` library >>> from regressions.analyses.equations.sinusoidal import sinusoidal_equation Create a sinusoidal function with coefficients 2, 3, 5, and 7, then evaluate it at 10 >>> evaluation_first = sinusoidal_equation(2, 3, 5, 7) >>> print(evaluation_first(10)) 8.3006 Create a sinusoidal function with coefficients 7, -5, -3, and 2, then evaluate it at 10 >>> evaluation_second = sinusoidal_equation(7, -5, -3, 2) >>> print(evaluation_second(10)) -3.7878 Create a sinusoidal function with all inputs set to 0, then evaluate it at 10 >>> evaluation_zero = sinusoidal_equation(0, 0, 0, 0) >>> print(evaluation_zero(10)) 0.0001 """ # Handle input errors four_scalars(first_constant, second_constant, third_constant, fourth_constant) positive_integer(precision) coefficients = no_zeroes([first_constant, second_constant, third_constant, fourth_constant], precision) # Create evaluation def sinusoidal_evaluation(variable): evaluation = coefficients[0] * sin(coefficients[1] * (variable - coefficients[2])) + coefficients[3] result = rounded_value(evaluation, precision) return result return sinusoidal_evaluation
def logistic_equation(first_constant, second_constant, third_constant, precision = 4): """ Generates a logistic function to provide evaluations at variable inputs Parameters ---------- first_constant : int or float Carrying capacity of the resultant logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) second_constant : int or float Growth rate of the resultant logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) third_constant : int or float Value of the sigmoid's midpoint of the resultant logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001) precision : int, default=4 Maximum number of digits that can appear after the decimal place of the resultant roots Raises ------ TypeError First three arguments must be integers or floats ValueError Last argument must be a positive integer Returns ------- evaluation : func Function for evaluating a logistic equation when passed any integer or float argument See Also -------- :func:`~regressions.analyses.derivatives.logistic.logistic_derivatives`, :func:`~regressions.analyses.integrals.logistic.logistic_integral`, :func:`~regressions.analyses.roots.logistic.logistic_roots`, :func:`~regressions.models.logistic.logistic_model` Notes ----- - Standard form of a logistic function: :math:`f(x) = \\frac{a}{1 + \\text{e}^{-b\\cdot(x - c)}}` - |logistic_functions| Examples -------- Import `logistic_equation` function from `regressions` library >>> from regressions.analyses.equations.logistic import logistic_equation Create a logistic function with coefficients 2, 3, and 5, then evaluate it at 10 >>> evaluation_first = logistic_equation(2, 3, 5) >>> print(evaluation_first(10)) 2.0 Create a logistic function with coefficients 100, 5, and 11, then evaluate it at 10 >>> evaluation_second = logistic_equation(100, 5, 11) >>> print(evaluation_second(10)) 0.6693 Create a logistic function with all inputs set to 0, then evaluate it at 10 >>> evaluation_zero = logistic_equation(0, 0, 0) >>> print(evaluation_zero(10)) 0.0001 """ # Handle input errors three_scalars(first_constant, second_constant, third_constant) positive_integer(precision) coefficients = no_zeroes([first_constant, second_constant, third_constant], precision) # Create evaluation def logistic_evaluation(variable): evaluation = coefficients[0] * (1 + exp(-1 * coefficients[1] * (variable - coefficients[2])))**(-1) result = rounded_value(evaluation, precision) return result return logistic_evaluation
def logistic_model(data, precision=4): """ Generates a logistic regression model from a given data set Parameters ---------- data : list of lists of int or float List of lists of numbers representing a collection of coordinate pairs; it must include at least 10 pairs precision : int, default=4 Maximum number of digits that can appear after the decimal place of the results Raises ------ TypeError First argument must be a 2-dimensional list TypeError Elements nested within first argument must be integers or floats ValueError First argument must contain at least 10 elements ValueError Last argument must be a positive integer Returns ------- model['constants'] : list of float Coefficients of the resultant logistic model; the first element is the carrying capacity, the second element is the growth rate, and the third element is the sigmoid's midpoint model['evaluations']['equation'] : func Function that evaluates the equation of the logistic model at a given numeric input (e.g., model['evaluations']['equation'](10) would evaluate the equation of the logistic model when the independent variable is 10) model['evaluations']['derivative'] : func Function that evaluates the first derivative of the logistic model at a given numeric input (e.g., model['evaluations']['derivative'](10) would evaluate the first derivative of the logistic model when the independent variable is 10) model['evaluations']['integral'] : func Function that evaluates the integral of the logistic model at a given numeric input (e.g., model['evaluations']['integral'](10) would evaluate the integral of the logistic model when the independent variable is 10) model['points']['roots'] : list of lists of float List of lists of numbers representing the coordinate pairs of all the x-intercepts of the logistic model (will always be `None`) model['points']['maxima'] : list of lists of float List of lists of numbers representing the coordinate pairs of all the maxima of the logistic model (will always be `None`) model['points']['minima'] : list of lists of float List of lists of numbers representing the coordinate pairs of all the minima of the logistic model (will always be `None`) model['points']['inflections'] : list of lists of float List of lists of numbers representing the coordinate pairs of all the inflection points of the logistic model (will contain exactly one point) model['accumulations']['range'] : float Total area under the curve represented by the logistic model between the smallest independent coordinate originally provided and the largest independent coordinate originally provided (i.e., over the range) model['accumulations']['iqr'] : float Total area under the curve represented by the logistic model between the first and third quartiles of all the independent coordinates originally provided (i.e., over the interquartile range) model['averages']['range']['average_value_derivative'] : float Average rate of change of the curve represented by the logistic model between the smallest independent coordinate originally provided and the largest independent coordinate originally provided model['averages']['range']['mean_values_derivative'] : list of float All points between the smallest independent coordinate originally provided and the largest independent coordinate originally provided where their instantaneous rate of change equals the function's average rate of change over that interval model['averages']['range']['average_value_integral'] : float Average value of the curve represented by the logistic model between the smallest independent coordinate originally provided and the largest independent coordinate originally provided model['averages']['range']['mean_values_integral'] : list of float All points between the smallest independent coordinate originally provided and the largest independent coordinate originally provided where their value equals the function's average value over that interval model['averages']['iqr']['average_value_derivative'] : float Average rate of change of the curve represented by the logistic model between the first and third quartiles of all the independent coordinates originally provided model['averages']['iqr']['mean_values_derivative'] : list of float All points between the first and third quartiles of all the independent coordinates originally provided where their instantaneous rate of change equals the function's average rate of change over that interval model['averages']['iqr']['average_value_integral'] : float Average value of the curve represented by the logistic model between the first and third quartiles of all the independent coordinates originally provided model['averages']['iqr']['mean_values_integral'] : list of float All points between the first and third quartiles of all the independent coordinates originally provided where their value equals the function's average value over that interval model['correlation'] : float Correlation coefficient indicating how well the model fits the original data set (values range between 0.0, implying no fit, and 1.0, implying a perfect fit) See Also -------- :func:`~regressions.analyses.equations.logistic.logistic_equation`, :func:`~regressions.analyses.derivatives.logistic.logistic_derivatives`, :func:`~regressions.analyses.integrals.logistic.logistic_integral`, :func:`~regressions.analyses.roots.logistic.logistic_roots`, :func:`~regressions.statistics.correlation.correlation_coefficient`, :func:`~regressions.execute.run_all` Notes ----- - Provided ordered pairs for the data set: :math:`p_i = \\{ (p_{1,x}, p_{1,y}), (p_{2,x}, p_{2,y}), \\cdots, (p_{n,x}, p_{n,y}) \\}` - Provided values for the independent variable: :math:`X_i = \\{ p_{1,x}, p_{2,x}, \\cdots, p_{n,x} \\}` - Provided values for the dependent variable: :math:`Y_i = \\{ p_{1,y}, p_{2,y}, \\cdots, p_{n,y} \\}` - Minimum value of the provided values for the independent variable: :math:`X_{min} \\leq p_{j,x}, \\forall p_{j,x} \\in X_i` - Maximum value of the provided values for the independent variable: :math:`X_{max} \\geq p_{j,x}, \\forall p_{j,x} \\in X_i` - First quartile of the provided values for the independent variable: :math:`X_{Q1}` - Third quartile of the provided values for the independent variable: :math:`X_{Q3}` - Mean of all provided values for the dependent variable: :math:`\\bar{y} = \\frac{1}{n}\\cdot{\\sum\\limits_{i=1}^n Y_i}` - Resultant values for the coefficients of the logistic model: :math:`C_i = \\{ a, b, c \\}` - Standard form for the equation of the logistic model: :math:`f(x) = \\frac{a}{1 + \\text{e}^{-b\\cdot(x - c)}}` - First derivative of the logistic model: :math:`f'(x) = \\frac{ab\\cdot{\\text{e}^{-b\\cdot(x - c)}}}{(1 + \\text{e}^{-b\\cdot(x - c)})^2}` - Second derivative of the logistic model: :math:`f''(x) = \\frac{2ab^2\\cdot{\\text{e}^{-2b\\cdot(x - c)}}}{(1 + \\text{e}^{-b\\cdot(x - c)})^3} - \\frac{ab^2\\cdot{\\text{e}^{-b\\cdot(x - c)}}}{(1 + \\text{e}^{-b\\cdot(x - c)})^2}` - Integral of the logistic model: :math:`F(x) = \\frac{a}{b}\\cdot{\\ln|\\text{e}^{b\\cdot(x - c)} + 1|}` - Potential x-values of the roots of the logistic model: :math:`x_{intercepts} = \\{ \\varnothing \\}` - Potential x-values of the maxima of the logistic model: :math:`x_{maxima} = \\{ \\varnothing \\}` - Potential x-values of the minima of the logistic model: :math:`x_{minima} = \\{ \\varnothing \\}` - Potential x-values of the inflection points of the logistic model: :math:`x_{inflections} = \\{ c \\}` - Accumulatation of the logistic model over its range: :math:`A_{range} = \\int_{X_{min}}^{X_{max}} f(x) \\,dx` - Accumulatation of the logistic model over its interquartile range: :math:`A_{iqr} = \\int_{X_{Q1}}^{X_{Q3}} f(x) \\,dx` - Average rate of change of the logistic model over its range: :math:`m_{range} = \\frac{f(X_{max}) - f(X_{min})}{X_{max} - X_{min}}` - Potential x-values at which the logistic model's instantaneous rate of change equals its average rate of change over its range: :math:`x_{m,range} = \\{ c + \\frac{1}{b}\\cdot{\\ln(2m_{range})} - \\frac{1}{b}\\cdot{\\ln\\left(ab - 2m_{range} - \\sqrt{(2m_{range} - ab)^2 - 4m_{range}^2}\\right)}, \\\\ c + \\frac{1}{b}\\cdot{\\ln(2m_{range})} - \\frac{1}{b}\\cdot{\\ln\\left(ab - 2m_{range} + \\sqrt{(2m_{range} - ab)^2 - 4m_{range}^2}\\right)} \\}` - Average value of the logistic model over its range: :math:`v_{range} = \\frac{1}{X_{max} - X_{min}}\\cdot{A_{range}}` - Potential x-values at which the logistic model's value equals its average value over its range: :math:`x_{v,range} = \\{ c - \\frac{1}{b}\\cdot{\\ln(\\frac{a}{v_{range}} - 1)} \\}` - Average rate of change of the logistic model over its interquartile range: :math:`m_{iqr} = \\frac{f(X_{Q3}) - f(X_{Q1})}{X_{Q3} - X_{Q1}}` - Potential x-values at which the logistic model's instantaneous rate of change equals its average rate of change over its interquartile range: :math:`x_{m,iqr} = \\{ c + \\frac{1}{b}\\cdot{\\ln(2m_{iqr})} - \\frac{1}{b}\\cdot{\\ln\\left(ab - 2m_{iqr} - \\sqrt{(2m_{iqr} - ab)^2 - 4m_{iqr}^2}\\right)}, \\\\ c + \\frac{1}{b}\\cdot{\\ln(2m_{iqr})} - \\frac{1}{b}\\cdot{\\ln\\left(ab - 2m_{iqr} + \\sqrt{(2m_{iqr} - ab)^2 - 4m_{iqr}^2}\\right)} \\}` - Average value of the logistic model over its interquartile range: :math:`v_{iqr} = \\frac{1}{X_{Q3} - X_{Q1}}\\cdot{A_{iqr}}` - Potential x-values at which the logistic model's value equals its average value over its interquartile range: :math:`x_{v,iqr} = \\{ c - \\frac{1}{b}\\cdot{\\ln(\\frac{a}{v_{iqr}} - 1)} \\}` - Predicted values based on the logistic model: :math:`\\hat{y}_i = \\{ \\hat{y}_1, \\hat{y}_2, \\cdots, \\hat{y}_n \\}` - Residuals of the dependent variable: :math:`e_i = \\{ p_{1,y} - \\hat{y}_1, p_{2,y} - \\hat{y}_2, \\cdots, p_{n,y} - \\hat{y}_n \\}` - Deviations of the dependent variable: :math:`d_i = \\{ p_{1,y} - \\bar{y}, p_{2,y} - \\bar{y}, \\cdots, p_{n,y} - \\bar{y} \\}` - Sum of squares of residuals: :math:`SS_{res} = \\sum\\limits_{i=1}^n e_i^2` - Sum of squares of deviations: :math:`SS_{dev} = \\sum\\limits_{i=1}^n d_i^2` - Correlation coefficient for the logistic model: :math:`r = \\sqrt{1 - \\frac{SS_{res}}{SS_{dev}}}` - |regression_analysis| Examples -------- Import `logistic_model` function from `regressions` library >>> from regressions.models.logistic import logistic_model Generate a logistic regression model for the data set [[1, 0.0000122], [2, 0.000247], [3, 0.004945], [4, 0.094852], [5, 1.0], [6, 1.905148], [7, 1.995055], [8, 1.999753], [9, 1.999988], [10, 1.999999]], then print its coefficients, roots, total accumulation over its interquartile range, and correlation >>> model_perfect = logistic_model([[1, 0.0000122], [2, 0.000247], [3, 0.004945], [4, 0.094852], [5, 1.0], [6, 1.905148], [7, 1.995055], [8, 1.999753], [9, 1.999988], [10, 1.999999]]) >>> print(model_perfect['constants']) [2.0, 3.0, 5.0] >>> print(model_perfect['points']['roots']) [None] >>> print(model_perfect['accumulations']['iqr']) 5.9987 >>> print(model_perfect['correlation']) 1.0 Generate a logistic regression model for the data set [[1, 32], [2, 25], [3, 14], [4, 23], [5, 39], [6, 45], [7, 42], [8, 49], [9, 36], [10, 33]], then print its coefficients, inflections, total accumulation over its range, and correlation >>> model_agnostic = logistic_model([[1, 32], [2, 25], [3, 14], [4, 23], [5, 39], [6, 45], [7, 42], [8, 49], [9, 36], [10, 33]]) >>> print(model_agnostic['constants']) [43.9838, 0.3076, 0.9747] >>> print(model_agnostic['points']['inflections']) [[0.9747, 21.9919]] >>> print(model_agnostic['accumulations']['range']) 305.9347 >>> print(model_agnostic['correlation']) 0.5875 """ # Handle input errors matrix_of_scalars(data, 'first') long_vector(data) positive_integer(precision) # Store independent and dependent variable values separately independent_variable = single_dimension(data, 1) dependent_variable = single_dimension(data, 2) # Determine key values for bounds halved_data = half_dimension(data, 1) dependent_lower = single_dimension(halved_data['lower'], 2) dependent_upper = single_dimension(halved_data['upper'], 2) mean_lower = mean_value(dependent_lower) mean_upper = mean_value(dependent_upper) dependent_max = max(dependent_variable) dependent_min = min(dependent_variable) dependent_range = dependent_max - dependent_min independent_max = max(independent_variable) independent_min = min(independent_variable) independent_range = independent_max - independent_min independent_avg = (independent_max + independent_min) / 2 # Circumvent error with bounds if dependent_range == 0: dependent_range = 1 if independent_range == 0: independent_range = 1 # Create function to guide model generation def logistic_fit(variable, first_constant, second_constant, third_constant): evaluation = first_constant / (1 + exp(-1 * second_constant * (variable - third_constant))) return evaluation # Create list to store coefficients of generated equation solution = [] # Handle normal case where values appear to increase in the set if mean_upper >= mean_lower: # Generate model parameters, covariance = curve_fit( logistic_fit, independent_variable, dependent_variable, bounds=[(dependent_max - dependent_range, 0, independent_avg - independent_range), (dependent_max + dependent_range, inf, independent_avg + independent_range)]) solution = list(parameters) # Handle case where values do not appear to increase in the set else: # Generate model with inverted negative infinity and zero values parameters, covariance = curve_fit( logistic_fit, independent_variable, dependent_variable, bounds=[(dependent_max - dependent_range, -inf, independent_avg - independent_range), (dependent_max + dependent_range, 0, independent_avg + independent_range)]) solution = list(parameters) # Eliminate zeroes from solution coefficients = no_zeroes(solution, precision) # Generate evaluations for function, derivative, and integral equation = logistic_equation(*coefficients, precision) derivative = logistic_derivatives(*coefficients, precision)['first']['evaluation'] integral = logistic_integral(*coefficients, precision)['evaluation'] # Determine key points of graph points = key_coordinates('logistic', coefficients, precision) # Generate values for lower and upper bounds 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'] # Calculate accumulations accumulated_range = accumulated_area('logistic', coefficients, min_value, max_value, precision) accumulated_iqr = accumulated_area('logistic', coefficients, q1, q3, precision) # Determine average values and their points averages_range = average_values('logistic', coefficients, min_value, max_value, precision) averages_iqr = average_values('logistic', coefficients, q1, q3, precision) # Create list of predicted outputs predicted = [] for element in independent_variable: predicted.append(equation(element)) # Calculate correlation coefficient for model accuracy = correlation_coefficient(dependent_variable, predicted, precision) # Package preceding results in multiple dictionaries evaluations = { 'equation': equation, 'derivative': 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} # Package all dictionaries in single dictionary to return result = { 'constants': coefficients, 'evaluations': evaluations, 'points': points, 'accumulations': accumulations, 'averages': averages, 'correlation': accuracy } return result
def sinusoidal_model(data, precision=4): """ Generates a sinusoidal regression model from a given data set Parameters ---------- data : list of lists of int or float List of lists of numbers representing a collection of coordinate pairs; it must include at least 10 pairs precision : int, default=4 Maximum number of digits that can appear after the decimal place of the results Raises ------ TypeError First argument must be a 2-dimensional list TypeError Elements nested within first argument must be integers or floats ValueError First argument must contain at least 10 elements ValueError Last argument must be a positive integer Returns ------- model['constants'] : list of float Coefficients of the resultant sinusoidal model; the first element is the vertical stretch factor, the second element is the horizontal stretch factor, the third element is the horizontal shift, and the fourth element is the vertical shift model['evaluations']['equation'] : func Function that evaluates the equation of the sinusoidal model at a given numeric input (e.g., model['evaluations']['equation'](10) would evaluate the equation of the sinusoidal model when the independent variable is 10) model['evaluations']['derivative'] : func Function that evaluates the first derivative of the sinusoidal model at a given numeric input (e.g., model['evaluations']['derivative'](10) would evaluate the first derivative of the sinusoidal model when the independent variable is 10) model['evaluations']['integral'] : func Function that evaluates the integral of the sinusoidal model at a given numeric input (e.g., model['evaluations']['integral'](10) would evaluate the integral of the sinusoidal model when the independent variable is 10) model['points']['roots'] : list of lists of float or str List of lists of numbers representing the coordinate pairs of all the x-intercepts of the sinusoidal model (will contain either `None` or an initial set of points within two periods along with general terms for finding the other points) model['points']['maxima'] : list of lists of float or str List of lists of numbers representing the coordinate pairs of all the maxima of the sinusoidal model (will contain an initial set of points within two periods along with a general term for finding the other points) model['points']['minima'] : list of lists of float or str List of lists of numbers representing the coordinate pairs of all the minima of the sinusoidal model (will contain an initial set of points within two periods along with a general term for finding the other points) model['points']['inflections'] : list of lists of float or str List of lists of numbers representing the coordinate pairs of all the inflection points of the sinusoidal model (will contain an initial set of points within two periods along with a general term for finding the other points) model['accumulations']['range'] : float Total area under the curve represented by the sinusoidal model between the smallest independent coordinate originally provided and the largest independent coordinate originally provided (i.e., over the range) model['accumulations']['iqr'] : float Total area under the curve represented by the sinusoidal model between the first and third quartiles of all the independent coordinates originally provided (i.e., over the interquartile range) model['averages']['range']['average_value_derivative'] : float Average rate of change of the curve represented by the sinusoidal model between the smallest independent coordinate originally provided and the largest independent coordinate originally provided model['averages']['range']['mean_values_derivative'] : list of float or str All points between the smallest independent coordinate originally provided and the largest independent coordinate originally provided where their instantaneous rate of change equals the function's average rate of change over that interval model['averages']['range']['average_value_integral'] : float Average value of the curve represented by the sinusoidal model between the smallest independent coordinate originally provided and the largest independent coordinate originally provided model['averages']['range']['mean_values_integral'] : list of float or str All points between the smallest independent coordinate originally provided and the largest independent coordinate originally provided where their value equals the function's average value over that interval model['averages']['iqr']['average_value_derivative'] : float Average rate of change of the curve represented by the sinusoidal model between the first and third quartiles of all the independent coordinates originally provided model['averages']['iqr']['mean_values_derivative'] : list of float or str All points between the first and third quartiles of all the independent coordinates originally provided where their instantaneous rate of change equals the function's average rate of change over that interval model['averages']['iqr']['average_value_integral'] : float Average value of the curve represented by the sinusoidal model between the first and third quartiles of all the independent coordinates originally provided model['averages']['iqr']['mean_values_integral'] : list of float or str All points between the first and third quartiles of all the independent coordinates originally provided where their value equals the function's average value over that interval model['correlation'] : float Correlation coefficient indicating how well the model fits the original data set (values range between 0.0, implying no fit, and 1.0, implying a perfect fit) See Also -------- :func:`~regressions.analyses.equations.sinusoidal.sinusoidal_equation`, :func:`~regressions.analyses.derivatives.sinusoidal.sinusoidal_derivatives`, :func:`~regressions.analyses.integrals.sinusoidal.sinusoidal_integral`, :func:`~regressions.analyses.roots.sinusoidal.sinusoidal_roots`, :func:`~regressions.statistics.correlation.correlation_coefficient`, :func:`~regressions.execute.run_all` Notes ----- - Provided ordered pairs for the data set: :math:`p_i = \\{ (p_{1,x}, p_{1,y}), (p_{2,x}, p_{2,y}), \\cdots, (p_{n,x}, p_{n,y}) \\}` - Provided values for the independent variable: :math:`X_i = \\{ p_{1,x}, p_{2,x}, \\cdots, p_{n,x} \\}` - Provided values for the dependent variable: :math:`Y_i = \\{ p_{1,y}, p_{2,y}, \\cdots, p_{n,y} \\}` - Minimum value of the provided values for the independent variable: :math:`X_{min} \\leq p_{j,x}, \\forall p_{j,x} \\in X_i` - Maximum value of the provided values for the independent variable: :math:`X_{max} \\geq p_{j,x}, \\forall p_{j,x} \\in X_i` - First quartile of the provided values for the independent variable: :math:`X_{Q1}` - Third quartile of the provided values for the independent variable: :math:`X_{Q3}` - Mean of all provided values for the dependent variable: :math:`\\bar{y} = \\frac{1}{n}\\cdot{\\sum\\limits_{i=1}^n Y_i}` - Resultant values for the coefficients of the sinusoidal model: :math:`C_i = \\{ a, b, c, d \\}` - Standard form for the equation of the sinusoidal model: :math:`f(x) = a\\cdot{\\sin(b\\cdot(x - c))} + d` - First derivative of the sinusoidal model: :math:`f'(x) = ab\\cdot{\\cos(b\\cdot(x - c))}` - Second derivative of the sinusoidal model: :math:`f''(x) = -ab^2\\cdot{\\sin(b\\cdot(x - c))}` - Integral of the sinusoidal model: :math:`F(x) = -\\frac{a}{b}\\cdot{\\cos(b\\cdot(x - c))} + d\\cdot{x}` - Potential x-values of the roots of the sinusoidal model: :math:`x_{intercepts} = \\{ c + \\frac{1}{b}\\cdot{\\left(\\sin^{-1}(-\\frac{d}{a}) + 2\\pi\\cdot{k} \\right)}, c + \\frac{1}{b}\\cdot{\\left(-\\sin^{-1}(-\\frac{d}{a}) + \\pi\\cdot(2k - 1) \\right)}, \\\\ c - \\frac{\\pi}{b}\\cdot(2k - 1) \\}` - :math:`k \\in \\mathbb{Z}` - Potential x-values of the maxima of the sinusoidal model: :math:`x_{maxima} = \\{ c + \\frac{\\pi}{b}\\cdot(\\frac{1}{2} + k) \\}` - :math:`k \\in \\mathbb{Z}` - Potential x-values of the minima of the sinusoidal model: :math:`x_{maxima} = \\{ c + \\frac{\\pi}{b}\\cdot(\\frac{1}{2} + k) \\}` - :math:`k \\in \\mathbb{Z}` - Potential x-values of the inflection points of the sinusoidal model: :math:`x_{inflections} = \\{ c + \\frac{\\pi}{b}\\cdot{k} \\}` - :math:`k \\in \\mathbb{Z}` - Accumulatation of the sinusoidal model over its range: :math:`A_{range} = \\int_{X_{min}}^{X_{max}} f(x) \\,dx` - Accumulatation of the sinusoidal model over its interquartile range: :math:`A_{iqr} = \\int_{X_{Q1}}^{X_{Q3}} f(x) \\,dx` - Average rate of change of the sinusoidal model over its range: :math:`m_{range} = \\frac{f(X_{max}) - f(X_{min})}{X_{max} - X_{min}}` - Potential x-values at which the sinusoidal model's instantaneous rate of change equals its average rate of change over its range: :math:`x_{m,range} = \\{ c + \\frac{1}{b}\\cdot{\\left(\\cos^{-1}(\\frac{m_{range}}{ab}) + \\pi\\cdot{k} \\right)}, c + \\frac{1}{b}\\cdot{\\left(-\\cos^{-1}(\\frac{m_{range}}{ab}) + 2\\pi\\cdot{k} \\right)} \\}` - :math:`k \\in \\mathbb{Z}` - Average value of the sinusoidal model over its range: :math:`v_{range} = \\frac{1}{X_{max} - X_{min}}\\cdot{A_{range}}` - Potential x-values at which the sinusoidal model's value equals its average value over its range: :math:`x_{v,range} = \\{ c + \\frac{1}{b}\\cdot{\\left(\\sin^{-1}(-\\frac{d - v_{range}}{a}) + 2\\pi\\cdot{k} \\right)}, c + \\frac{1}{b}\\cdot{\\left(-\\sin^{-1}(-\\frac{d - v_{range}}{a}) + \\pi\\cdot(2k - 1) \\right)}, \\\\ c + \\frac{\\pi}{b}\\cdot(2k - 1) \\}` - :math:`k \\in \\mathbb{Z}` - Average rate of change of the sinusoidal model over its interquartile range: :math:`m_{iqr} = \\frac{f(X_{Q3}) - f(X_{Q1})}{X_{Q3} - X_{Q1}}` - Potential x-values at which the sinusoidal model's instantaneous rate of change equals its average rate of change over its interquartile range: :math:`x_{m,iqr} = \\{ c + \\frac{1}{b}\\cdot{\\left(\\cos^{-1}(\\frac{m_{iqr}}{ab}) + \\pi\\cdot{k} \\right)}, c + \\frac{1}{b}\\cdot{\\left(-\\cos^{-1}(\\frac{m_{iqr}}{ab}) + 2\\pi\\cdot{k} \\right)} \\}` - :math:`k \\in \\mathbb{Z}` - Average value of the sinusoidal model over its interquartile range: :math:`v_{iqr} = \\frac{1}{X_{Q3} - X_{Q1}}\\cdot{A_{iqr}}` - Potential x-values at which the sinusoidal model's value equals its average value over its interquartile range: :math:`x_{v,iqr} = \\{ c + \\frac{1}{b}\\cdot{\\left(\\sin^{-1}(-\\frac{d - v_{iqr}}{a}) + 2\\pi\\cdot{k} \\right)}, c + \\frac{1}{b}\\cdot{\\left(-\\sin^{-1}(-\\frac{d - v_{iqr}}{a}) + \\pi\\cdot(2k - 1) \\right)}, \\\\ c + \\frac{\\pi}{b}\\cdot(2k - 1) \\}` - :math:`k \\in \\mathbb{Z}` - Predicted values based on the sinusoidal model: :math:`\\hat{y}_i = \\{ \\hat{y}_1, \\hat{y}_2, \\cdots, \\hat{y}_n \\}` - Residuals of the dependent variable: :math:`e_i = \\{ p_{1,y} - \\hat{y}_1, p_{2,y} - \\hat{y}_2, \\cdots, p_{n,y} - \\hat{y}_n \\}` - Deviations of the dependent variable: :math:`d_i = \\{ p_{1,y} - \\bar{y}, p_{2,y} - \\bar{y}, \\cdots, p_{n,y} - \\bar{y} \\}` - Sum of squares of residuals: :math:`SS_{res} = \\sum\\limits_{i=1}^n e_i^2` - Sum of squares of deviations: :math:`SS_{dev} = \\sum\\limits_{i=1}^n d_i^2` - Correlation coefficient for the sinusoidal model: :math:`r = \\sqrt{1 - \\frac{SS_{res}}{SS_{dev}}}` - |regression_analysis| Examples -------- Import `sinusoidal_model` function from `regressions` library >>> from regressions.models.sinusoidal import sinusoidal_model Generate a sinusoidal regression model for the data set [[1, 3], [2, 8], [3, 3], [4, -2], [5, 3], [6, 8], [7, 3], [8, -2], [9, 3], [10, 8]], then print its coefficients, roots, total accumulation over its interquartile range, and correlation >>> model_perfect = sinusoidal_model([[1, 3], [2, 8], [3, 3], [4, -2], [5, 3], [6, 8], [7, 3], [8, -2], [9, 3], [10, 8]]) >>> print(model_perfect['constants']) [-5.0, 1.5708, 3.0, 3.0] >>> print(model_perfect['points']['roots']) [[3.4097, 0.0], [4.5903, 0.0], [7.4097, 0.0], [8.5903, 0.0], ['3.4097 + 4.0k', 0.0], ['4.5903 + 4.0k', 0.0]] >>> print(model_perfect['accumulations']['iqr']) 11.8168 >>> print(model_perfect['correlation']) 1.0 Generate a sinusoidal regression model for the data set [[1, 32], [2, 25], [3, 14], [4, 23], [5, 39], [6, 45], [7, 42], [8, 49], [9, 36], [10, 33]], then print its coefficients, inflections, total accumulation over its range, and correlation >>> model_agnostic = sinusoidal_model([[1, 32], [2, 25], [3, 14], [4, 23], [5, 39], [6, 45], [7, 42], [8, 49], [9, 36], [10, 33]]) >>> print(model_agnostic['constants']) [14.0875, 0.7119, -3.7531, 34.2915] >>> print(model_agnostic['points']['inflections']) [[5.0729, 34.2915], [9.4859, 34.2915], [13.8985, 34.2915], [18.3114, 34.2915], ['5.0729 + 4.413k', 34.2915]] >>> print(model_agnostic['accumulations']['range']) 307.8897 >>> print(model_agnostic['correlation']) 0.9264 """ # Handle input errors matrix_of_scalars(data, 'first') long_vector(data) positive_integer(precision) # Store independent and dependent variable values separately independent_variable = single_dimension(data, 1) dependent_variable = single_dimension(data, 2) # Determine key values for bounds independent_max = max(independent_variable) independent_min = min(independent_variable) independent_range = independent_max - independent_min dependent_max = max(dependent_variable) dependent_min = min(dependent_variable) dependent_range = dependent_max - dependent_min # Circumvent errors with bounds if independent_range == 0: independent_range = 1 if dependent_range == 0: dependent_range = 1 dependent_max += 1 # Create function to guide model generation 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 # Create list to store coefficients of generated equation solution = [] # Handle normal case try: # Generate model parameters, covariance = curve_fit( sinusoidal_fit, independent_variable, dependent_variable, bounds=[(-dependent_range, -inf, -independent_range, dependent_min), (dependent_range, inf, independent_range, dependent_max)]) solution = list(parameters) # Narrow bounds in event of runtime error except RuntimeError: # Regenerate model within tighter parameters parameters, covariance = curve_fit( sinusoidal_fit, independent_variable, dependent_variable, bounds=[(dependent_range - 1, -independent_range, -independent_range, dependent_min), (dependent_range + 1, independent_range, independent_range, dependent_max)]) solution = list(parameters) # Eliminate zeroes from solution coefficients = no_zeroes(solution, precision) # Generate evaluations for function, derivative, and integral equation = sinusoidal_equation(*coefficients, precision) derivative = sinusoidal_derivatives(*coefficients, precision)['first']['evaluation'] integral = sinusoidal_integral(*coefficients, precision)['evaluation'] # Determine key points of graph points = key_coordinates('sinusoidal', coefficients, precision) final_roots = shifted_coordinates_within_range(points['roots'], independent_min, independent_max, precision) final_maxima = shifted_coordinates_within_range(points['maxima'], independent_min, independent_max, precision) final_minima = shifted_coordinates_within_range(points['minima'], independent_min, independent_max, precision) final_inflections = shifted_coordinates_within_range( points['inflections'], independent_min, independent_max, precision) # Generate values for lower and upper bounds 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'] # Calculate accumulations accumulated_range = accumulated_area('sinusoidal', coefficients, min_value, max_value, precision) accumulated_iqr = accumulated_area('sinusoidal', coefficients, q1, q3, precision) # Determine average values and their points averages_range = average_values('sinusoidal', coefficients, min_value, max_value, precision) averages_iqr = average_values('sinusoidal', coefficients, q1, q3, precision) # Create list of predicted outputs predicted = [] for element in independent_variable: predicted.append(equation(element)) # Calculate correlation coefficient for model accuracy = correlation_coefficient(dependent_variable, predicted, precision) # Package preceding results in multiple dictionaries evaluations = { 'equation': equation, 'derivative': derivative, 'integral': integral } points = { 'roots': final_roots, 'maxima': final_maxima, 'minima': final_minima, 'inflections': final_inflections } accumulations = {'range': accumulated_range, 'iqr': accumulated_iqr} averages = {'range': averages_range, 'iqr': averages_iqr} # Package all dictionaries in single dictionary to return result = { 'constants': coefficients, 'evaluations': evaluations, 'points': points, 'accumulations': accumulations, 'averages': averages, 'correlation': accuracy } return result