Skip to content

Utility functions

Combine two arrays weighted by a factor favoring arr_1. The first array is to be the estimated real distribution. And the other is to be the personality vector of the agent.

Parameters:

Name Type Description Default
arr_1 array

The first array to be combined (real distribution).

required
arr_2 array

The second array to be combined (personality vector).

required
factor float

The factor to weigh the two arrays.

required

Returns:

Name Type Description
result array

The normalized weighted linear combination.

Example

TODO

Source code in democracy_sim/participation_agent.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def combine_and_normalize(arr_1: np.array, arr_2: np.array, factor: float):
    """
    Combine two arrays weighted by a factor favoring arr_1.
    The first array is to be the estimated real distribution.
    And the other is to be the personality vector of the agent.

    Args:
        arr_1: The first array to be combined (real distribution).
        arr_2: The second array to be combined (personality vector).
        factor: The factor to weigh the two arrays.

    Returns:
        result (np.array): The normalized weighted linear combination.

    Example:
        TODO
    """
    # Ensure f is between 0 and 1 TODO: remove this on simulations to speed up
    if not (0 <= factor <= 1):
        raise ValueError("Factor f must be between 0 and 1")
    # Linear combination
    res = factor * arr_1 + (1 - factor) * arr_2
    # Normalize/scale result s. t. it resembles a distribution vector (sum=1)
    total = sum(res)
    # assert total == 1.0, f"Sum of result is {total} and not 1.0"
    return res / total