def test_compare_vectors_3_4_raises(self): with self.assertRaises(Exception) as context: compare_vectors(first_vector, longer_vector) self.assertEqual(type(context.exception), ValueError) self.assertEqual( str(context.exception), 'Both arguments must contain the same number of elements')
def component_form(initial_point, terminal_point): """ Calculates the component form for a vector by using two points Parameters ---------- initial_point : list of int or float List of numbers representing a point terminal_point : list of int or float List of numbers representing a point Raises ------ TypeError Arguments must be 1-dimensional lists TypeError Elements of arguments must be integers or floats ValueError Both arguments must contain the same number of elements Returns ------- components : list of int or float List in which each element is the difference of the corresponding elements from the input points (specifically, the change from the initial point to the terminal point) See Also -------- :func:`~regressions.vectors.addition.vector_sum`, :func:`~regressions.vectors.multiplication.scalar_product_vector`, :func:`~regressions.vectors.direction.vector_direction`, :func:`~regressions.vectors.magnitude.vector_magnitude` Notes ----- - Initial point: :math:`A = (a_1, a_2, \\cdots, a_n)` - Terminal point: :math:`B = (b_1, b_2, \\cdots, b_n)` - Component form of vector: :math:`\\overrightarrow{AB} = \\langle b_1 - a_1, b_2 - a_2, \\cdots, b_n - a_n \\rangle` - |component_form| Examples -------- Import `component_form` function from `regressions` library >>> from regressions.vectors.components import component_form Determine the component form of a vector with an initial point of [1, 2, 3] and a terminal point of [4, 5, 6] >>> components_3d = component_form([1, 2, 3], [4, 5, 6]) >>> print(components_3d) [3, 3, 3] Determine the component form of a vector with an initial point of [-5, 12] and a terminal point of [3, -7] >>> components_2d = component_form([-5, 12], [3, -7]) >>> print(components_2d) [8, -19] """ # Handle input errors compare_vectors(initial_point, terminal_point) # Determine difference between terminal point and initial point result = vector_sum(terminal_point, scalar_product_vector(initial_point, -1)) return result
def multiple_residuals(actual_array, expected_array): """ Generates a list of the differences between the actual values from one list and the expected values from another list Parameters ---------- actual_array : list of int or float List containing the actual values observed from a data set expected_array : list of int or float List containing the expected values predicted for a data set Raises ------ TypeError Arguments must be 1-dimensional lists TypeError Elements of arguments must be integers or floats ValueError Both arguments must contain the same number of elements Returns ------- residuals : list of float Differences between the actual values and the expected values See Also -------- :func:`~regressions.statistics.deviations.multiple_deviations`, :func:`~regressions.statistics.correlation.correlation_coefficient` Notes ----- - Observed values: :math:`y_i = \\{ y_1, y_2, \\cdots, y_n \\}` - Predicted values: :math:`\\hat{y}_i = \\{ \\hat{y}_1, \\hat{y}_2, \\cdots, \\hat{y}_n \\}` - Residuals: :math:`e_i = \\{ y_1 - \\hat{y}_1, y_2 - \\hat{y}_2, \\cdots, y_n - \\hat{y}_n \\}` - |residual| Examples -------- Import `multiple_residuals` function from `regressions` library >>> from regressions.statistics.residuals import multiple_residuals Determine the residuals between the actual values [5.6, 8.1, 6.3] and the expected values [6.03, 8.92, 6.12] >>> residuals_short = multiple_residuals([5.6, 8.1, 6.3], [6.03, 8.92, 6.12]) >>> print(residuals_short) [-0.4300000000000006, -0.8200000000000003, 0.17999999999999972] Determine the residuals between the actual values [11.7, 5.6, 8.1, 13.4, 6.3] and the expected values [15.17, 6.03, 8.92, 9.42, 6.12] >>> residuals_long = multiple_residuals([11.7, 5.6, 8.1, 13.4, 6.3], [15.17, 6.03, 8.92, 9.42, 6.12]) >>> print(residuals_long) [-3.4700000000000006, -0.4300000000000006, -0.8200000000000003, 3.9800000000000004, 0.17999999999999972] """ # Handle input errors compare_vectors(actual_array, expected_array) # Create list to return results = [] # Iterate over inputs for i in range(len(actual_array)): # Store residuals of corresponding elements in list to return results.append(single_residual(actual_array[i], expected_array[i])) # Return results return results
def correlation_coefficient(actuals, expecteds, precision=4): """ Calculates the correlation coefficient as a way to predict the strength of a predicted model by comparing the ratio of residuals to deviations, in order to determine a strong or weak relationship Parameters ---------- actuals : list of int or float List containing the actual values observed from a data set expecteds : list of int or float List containing the expected values for a data set based on a predictive model precision : int, default=4 Maximum number of digits that can appear after the decimal place of the result Raises ------ TypeError First and second arguments must be 1-dimensional lists TypeError Elements of first and second arguments must be integers or floats ValueError First and second arguments must contain the same number of elements ValueError Last argument must be a positive integer Returns ------- correlation : float Number indicating statistical strenght of the relationship between two variables; the closer to 1, the stronger; the closer to 0, the weaker See Also -------- :func:`~regressions.statistics.residuals.multiple_residuals`, :func:`~regressions.statistics.deviations.multiple_deviations`, :func:`~regressions.statistics.summation.sum_value` Notes ----- - Observed values: :math:`y_i = \\{ y_1, y_2, \\cdots, y_n \\}` - Predicted values: :math:`\\hat{y}_i = \\{ \\hat{y}_1, \\hat{y}_2, \\cdots, \\hat{y}_n \\}` - Mean of all observed values: :math:`\\bar{y} = \\frac{1}{n}\\cdot{\\sum\\limits_{i=1}^n y_i}` - Residuals: :math:`e_i = \\{ y_1 - \\hat{y}_1, y_2 - \\hat{y}_2, \\cdots, y_n - \\hat{y}_n \\}` - Deviations: :math:`d_i = \\{ y_1 - \\bar{y}, y_2 - \\bar{y}, \\cdots, y_n - \\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: :math:`r = \\sqrt{1 - \\frac{SS_{res}}{SS_{dev}}}` - |determination| Examples -------- Import `correlation_coefficient` function from `regressions` library >>> from regressions.statistics.correlation import correlation_coefficient Calculate the correlation using the provided actual values [8.2, 9.41, 1.23, 34.7] and the predicted values [7.863, 8.9173, 2.0114, 35.8021] >>> correlation_short = correlation_coefficient([8.2, 9.41, 1.23, 34.7], [7.863, 8.9173, 2.0114, 35.8021]) >>> print(correlation_short) 0.9983 Calculate the correlation using the provided actual values [2, 3, 5, 7, 11, 13, 17, 19] and the predicted values [1.0245, 3.7157, 6.1398, 8.1199, 12.7518, 14.9621, 15.2912, 25.3182] >>> correlation_long = correlation_coefficient([2, 3, 5, 7, 11, 13, 17, 19], [1.0245, 3.7157, 6.1398, 8.1199, 12.7518, 14.9621, 15.2912, 25.3182]) >>> print(correlation_long) 0.9011 """ # Handle input errors compare_vectors(actuals, expecteds) positive_integer(precision) # Determine residuals and deviations between actuals and expecteds residuals = multiple_residuals(actuals, expecteds) deviations = multiple_deviations(actuals) # Square residuals and deviations squared_residuals = [] for residual in residuals: squared_residuals.append(residual**2) squared_deviations = [] for deviation in deviations: squared_deviations.append(deviation**2) # Calculate the sums of the squares of the residuals and deviations residual_sum = sum_value(squared_residuals) deviation_sum = sum_value(squared_deviations) # Circumvent division by zero if deviation_sum == 0: deviation_sum = 10**(-precision) # Calculate key ratio ratio = residual_sum / deviation_sum # Handle no solution if ratio > 1: return 0.0 # Handle general case else: result = (1 - ratio)**(1 / 2) return rounded_value(result, precision)
def unite_vectors(vector_one, vector_two): """ Unites two row vectors into a single matrix whose columns coincide with the input vectors Parameters ---------- vector_one : list of int or float List of numbers representing a vector vector_two : list of int or float List of numbers representing a vector Raises ------ TypeError Arguments must be 1-dimensional lists TypeError Elements of arguments must be integers or floats ValueError Both arguments must contain the same number of elements Returns ------- matrix : list of lists of int or float List containing lists; length of outer list will equal lengths of supplied vectors; length of inner lists will equal two See Also -------- :func:`~regressions.vectors.dimension.single_dimension`, :func:`~regressions.statistics.halve.partition` Notes ----- - First vector: :math:`\\langle a_1, a_2, \\cdots, a_n \\rangle` - Second vector: :math:`\\langle b_1, b_2, \\cdots, b_n \\rangle` - Matrix unifying first and second vectors: :math:`\\begin{bmatrix} a_1 & b_1 \\\\ a_2 & b_2 \\\\ \\cdots & \\cdots \\\\ a_n & b_n \\end{bmatrix}` Examples -------- Import `unite_vectors` function from `regressions` library >>> from regressions.vectors.unify import unite_vectors Unite [1, 2, 3] and [4, 5, 6] >>> matrix_3x2 = unite_vectors([1, 2, 3], [4, 5, 6]) >>> print(matrix_2x3) [[1, 4], [2, 5], [3, 6]] Unite [-5, 12] and [3, -7] >>> matrix_2x2 = unite_vectors([-5, 12], [3, -7]) >>> print(matrix_2x2) [[-5, 3], [12, -7]] """ # Handle input errors compare_vectors(vector_one, vector_two) # Create list to return result = [] # Handle no solution if vector_one[0] == None: result.append(None) # Handle general case else: # Iterate over inputs for i in range(len(vector_one)): # Store corresponding elements from inputs as lists within list to return result.append([vector_one[i], vector_two[i]]) # Return result return result
def test_compare_vectors_3_3(self): compare_vectors_3_3 = compare_vectors(first_vector, second_vector) self.assertEqual(compare_vectors_3_3, 'Both arguments contain the same number of elements')
def vector_sum(vector_one, vector_two): """ Calculates the sum of two vectors Parameters ---------- vector_one : list of int or float List of numbers representing a vector vector_two : list of int or float List of numbers representing a vector Raises ------ TypeError Arguments must be 1-dimensional lists TypeError Elements of arguments must be integers or floats ValueError Both arguments must contain the same number of elements Returns ------- vector : list of int or float List in which each element is the sum of the corresponding elements from the input vectors See Also -------- :func:`~regressions.matrices.addition.matrix_sum`, :func:`~regressions.vectors.multiplication.scalar_product_vector` Notes ----- - First vector: :math:`\\mathbf{a} = \\langle a_1, a_2, \\cdots, a_n \\rangle` - Second vector: :math:`\\mathbf{b} = \\langle b_1, b_2, \\cdots, b_n \\rangle` - Sum of vectors: :math:`\\mathbf{a} + \\mathbf{b} = \\langle a_1 + b_1, a_2 + b_2, \\cdots, a_n + b_n \\rangle` - |vector_addition| Examples -------- Import `vector_sum` function from `regressions` library >>> from regressions.vectors.addition import vector_sum Add [1, 2, 3] and [4, 5, 6] >>> vector_3d = vector_sum([1, 2, 3], [4, 5, 6]) >>> print(vector_3d) [5, 7, 9] Add [-5, 12] and [3, -7] >>> vector_2d = vector_sum([-5, 12], [3, -7]) >>> print(vector_2d) [-2, 5] """ # Handle input errors compare_vectors(vector_one, vector_two) # Create list to return result = [] # Iterate over first input for i in range(len(vector_one)): # Store sums of corresponding vector elements in result result.append(vector_one[i] + vector_two[i]) # Return result return result
def dot_product(vector_one, vector_two): """ Calculates the product of two vectors Parameters ---------- vector_one : list of int or float List of numbers representing a vector vector_two : list of int or float List of numbers representing a vector Raises ------ TypeError Arguments must be 1-dimensional lists TypeError Elements of arguments must be integers or floats ValueError Both arguments must contain the same number of elements Returns ------- product : float Number created by summing the products of the corresponding terms from each input vector See Also -------- :func:`~regressions.matrices.multiplication.matrix_product` Notes ----- - First vector: :math:`\\mathbf{a} = \\langle a_1, a_2, \\cdots, a_n \\rangle` - Second vector: :math:`\\mathbf{b} = \\langle b_1, b_2, \\cdots, b_n \\rangle`: - Dot product of vectors: :math:`\\mathbf{a}\\cdot{\\mathbf{b}} = a_1\\cdot{b_1} + a_2\\cdot{b_2} + \\cdots + a_n\\cdot{b_n}` - |dot_product| Examples -------- Import `dot_product` function from `regressions` library >>> from regressions.vectors.multiplication import dot_product Multiply [1, 2, 3] and [4, 5, 6] >>> product_3d = dot_product([1, 2, 3], [4, 5, 6]) >>> print(product_3d) 32.0 Multiply [-5, 12] and [3, -7] >>> product_2d = dot_product([-5, 12], [3, -7]) >>> print(product_2d) -99.0 """ # Handle input errors compare_vectors(vector_one, vector_two) # Create intermediary number result = 0 # Iterate over inputs for i in range(len(vector_one)): # Add products of corresponding elements from inputs to intermediary number result += vector_one[i] * vector_two[i] # Convert number to float floated_result = float(result) return floated_result