Beispiel #1
0
def single_residual(actual, expected):
    """
    Calculates the difference between the actual value and the expected value

    Parameters
    ----------
    actual : int or float
        Value actually provided by an initial data set
    expected : int or float
        Value predicted to occur by a generated model at the same input to the one that coincided with the actual value

    Raises
    ------
    TypeError
        Arguments must be integers or floats

    Returns
    -------
    residual : float
        Difference between the actual value and the expected value

    See Also
    --------
    :func:`~regressions.statistics.deviations.single_deviation`, :func:`~regressions.statistics.correlation.correlation_coefficient`

    Notes
    -----
    - Observed value: :math:`y`
    - Predicted value: :math:`\\hat{y}`
    - Residual: :math:`e = y - \\hat{y}`
    - |residual|

    Examples
    --------
    Import `single_residual` function from `regressions` library
        >>> from regressions.statistics.residuals import single_residual
    Determine the residual between an actual value of 7.8 and an expected value of 9.2
        >>> residual_small = single_residual(7.8, 9.2)
        >>> print(residual_small)
        -1.3999999999999995
    Determine the residual between an actual value of 6.1 and an expected value of 19.8
        >>> residual_large = single_residual(6.1, 19.8)
        >>> print(residual_large)
        -13.700000000000001
    """
    # Handle input errors
    scalar_value(actual, 'first')
    scalar_value(expected, 'second')

    # Calculate difference between inputs
    difference = actual - expected

    # Convert difference to float
    result = float(difference)
    return result
def single_deviation(actual, mean): 
    """
    Calculates the difference between the actual value from a data set and the mean of that data set

    Parameters
    ----------
    actual : int or float
        Value actually provided by an initial data set
    mean : int or float
        Average value of the data set

    Raises
    ------
    TypeError
        Arguments must be integers or floats

    Returns
    -------
    deviation : float
        Difference between the actual value and the mean of the data set

    See Also
    --------
    :func:`~regressions.statistics.mean.mean_value`, :func:`~regressions.statistics.residuals.single_residual`, :func:`~regressions.statistics.correlation.correlation_coefficient`

    Notes
    -----
    - Observed value: :math:`y`
    - Mean of all observed values: :math:`\\bar{y}`
    - Deviation: :math:`d = y - \\bar{y}`
    - |deviation|

    Examples
    --------
    Import `single_deviation` function from `regressions` library
        >>> from regressions.statistics.deviations import single_deviation
    Determine the deviation for an actual value of 7.8 and a mean of 13.75
        >>> deviation_small = single_deviation(7.8, 13.75)
        >>> print(deviation_small)
        -5.95
    Determine the deviation between an actual value of 6.1 and an mean of -19.7
        >>> deviation_large = single_deviation(6.1, -19.7)
        >>> print(deviation_large)
        25.799999999999997
    """
    # Handle input errors
    scalar_value(actual, 'first')
    scalar_value(mean, 'second')

    # Calcuate difference between actual and mean
    difference = actual - mean

    # Convert difference to float
    result = float(difference)
    return result
def scalar_product_matrix(matrix, scalar):
    """
    Calculates the product of a matrix and a scalar

    Parameters
    ----------
    matrix : list of lists of int or float
        List of lists of numbers representing a matrix
    scalar : int or float
        Number representing a scalar

    Raises
    ------
    TypeError
        First argument must be 2-dimensional lists
    TypeError
        Elements nested within first argument must be integers or floats
    TypeError
        Second argument must be an integer or a float

    Returns
    -------
    matrix : list of lists of int or float
        List of lists in which each inner element is the product of the corresponding element from the input matrix and the scalar value

    See Also
    --------
    :func:`~regressions.vectors.multiplication.scalar_product_vector`, :func:`~regressions.matrices.addition.matrix_sum`

    Notes
    -----
    - Matrix: :math:`\\mathbf{A} = \\begin{bmatrix} a_{1,1} & a_{1,2} & \\cdots & a_{1,n} \\\\ a_{2,1} & a_{2,2} & \\cdots & a_{2,n} \\\\ \\cdots & \\cdots & \\cdots & \\cdots \\\\ a_{m,1} & a_{m,2} & \\cdots & a_{m,n} \\end{bmatrix}`
    - Scalar: :math:`c`
    - Scalar product: :math:`c\\cdot{\\mathbf{A}} = \\begin{bmatrix} c\\cdot{a_{1,1}} & c\\cdot{a_{1,2}} & \\cdots & c\\cdot{a_{1,n}} \\\\ c\\cdot{a_{2,1}} & c\\cdot{a_{2,2}} & \\cdots & c\\cdot{a_{2,n}} \\\\ \\cdots & \\cdots & \\cdots & \\cdots \\\\ c\\cdot{a_{m,1}} & c\\cdot{a_{m,2}} & \\cdots & c\\cdot{a_{m,n}} \\end{bmatrix}`
    - |matrix_scalar_multiplication|

    Examples
    --------
    Import `scalar_product_matrix` function from `regressions` library
        >>> from regressions.matrices.multiplication import scalar_product_matrix
    Multiply [[1, 2, 3], [4, 5, 6]] and -2
        >>> matrix_2x3 = scalar_product_matrix([[1, 2, 3], [4, 5, 6]], -2)
        >>> print(matrix_2x3)
        [[-2, -4, -6], [-8, -10, -12]]
    Multiply [[5, -7], [-3, 8]] and 3
        >>> matrix_2x2 = scalar_product_matrix([[5, -7], [-3, 8]], 3)
        >>> print(matrix_2x2)
        [[15, -21], [-9, 24]]
    """
    # Handle input errors
    matrix_of_scalars(matrix, 'first')
    scalar_value(scalar, 'second')

    # Create list to return
    result = []

    # Iterate over outer lists of input
    for m in range(len(matrix)):
        # Create new lists inside list to return
        result.append([])

        # Iterate over inner lists of input
        for n in range(len(matrix[0])):
            # Store products in inner lists of return
            result[m].append(matrix[m][n] * scalar)
    
    # Return result
    return result
Beispiel #4
0
 def test_scalar_array_raises(self):
     with self.assertRaises(Exception) as context:
         scalar_value(choices)
     self.assertEqual(type(context.exception), TypeError)
     self.assertEqual(str(context.exception),
                      'Argument must be an integer or a float')
Beispiel #5
0
 def test_scalar_position(self):
     scalar_position = scalar_value(good_float, 'second')
     self.assertEqual(scalar_position,
                      'Second argument is an integer or a float')
Beispiel #6
0
 def test_scalar_float(self):
     scalar_float = scalar_value(good_float)
     self.assertEqual(scalar_float, 'Argument is an integer or a float')
Beispiel #7
0
 def test_scalar_integer(self):
     scalar_integer = scalar_value(good_integer)
     self.assertEqual(scalar_integer, 'Argument is an integer or a float')
def scalar_product_vector(vector, scalar):
    """
    Calculates the product of a vector and a scalar

    Parameters
    ----------
    vector : list of int or float
        List of numbers representing a vector
    scalar : int or float
        Number representing a scalar

    Raises
    ------
    TypeError
        First argument must be a 1-dimensional list
    TypeError
        Elements of first argument must be integers or floats
    TypeError
        Second argument must be an integer or a float

    Returns
    -------
    product : list of int or float
        List of numbers in which each element is the product of the scalar factor and the corresponding element from the input vector

    See Also
    --------
    :func:`~regressions.matrices.multiplication.scalar_product_matrix`, :func:`~regressions.vectors.addition.vector_sum`

    Notes
    -----
    - Vector: :math:`\\mathbf{a} = \\langle a_1, a_2, \\cdots, a_n \\rangle`
    - Scalar: :math:`c`
    - Scalar product: :math:`c\\cdot{\\mathbf{a}} = \\langle c\\cdot{a_1}, c\\cdot{a_2}, \\cdots, c\\cdot{a_n} \\rangle`
    - |scalar_multiplication|

    Examples
    --------
    Import `scalar_product_vector` function from `regressions` library
        >>> from regressions.vectors.multiplication import scalar_product_vector
    Multiply [1, 2, 3] and -2
        >>> product_3d = scalar_product_vector([1, 2, 3], -2)
        >>> print(product_3d)
        [-2, -4, -6]
    Multiply [-5, 12] and 3
        >>> product_2d = scalar_product_vector([-5, 12], 3)
        >>> print(product_2d)
        [-15, 36]
    """
    # Handle input errors
    vector_of_scalars(vector, 'first')
    scalar_value(scalar, 'second')

    # Create list to return
    result = []

    # Iterate over input
    for element in vector:
        # Store products in list to return
        result.append(element * scalar)

    # Return result
    return result