[Python] How to calculate daily weights which satisfy certain conditions

Discussão em 'Python' iniciado por Stack, Dezembro 18, 2024.

  1. Stack

    Stack Membro Participativo

    I have the following pandas dataframe which represents the consumption of 7 days (day_0 is today, day_-1 is yesterday etc) of 10 people (ids):

    import pandas as pd
    import numpy as np

    df = pd.DataFrame(np.random.randint(8, 15, size=(10, 7)))
    df.columns = ['day_0', 'day_-1', 'day_-2', 'day_-3', 'day_-4', 'day_-5', 'day_-6']
    df.index.name = 'id'

    print(df.reset_index())

    id day_0 day_-1 day_-2 day_-3 day_-4 day_-5 day_-6
    0 0 10 10 14 8 14 14 14
    1 1 10 13 11 11 8 10 10
    2 2 10 12 9 12 9 10 10
    3 3 12 12 9 11 9 12 13
    4 4 12 13 8 12 8 11 9
    5 5 13 9 8 13 9 12 10
    6 6 8 9 8 14 8 13 14
    7 7 13 10 14 12 8 9 11
    8 8 8 8 10 12 11 14 14
    9 9 14 13 13 9 11 14 13


    I would like to find daily weights (so in total 7 weights: w_0, w_-1, w_-2, w_-3, w_-4, w_-5, w_-6) which need to have the following properties:

    1. w_0 > w_-1 > w_-2 > ... > w_-6
    2. w_0 + w_-1 + w_-2 + ... + w_-6 = 7
    3. the weighted average for 7 out of 10 ids to be below a threshold (e.g. 11)

    I can achieve prerequisites 1 & 2 by using the exponential decay function and later normalizing:

    import numpy as np

    n = 7

    _lambda = 0.5

    # Calculate the weights using exponential decay
    weights = np.exp(-_lambda * np.arange(n))

    # Normalize the weights so that their sum is equal to the length of the time series
    weights *= n / np.sum(weights)


    But I don't know how I could apply also prerequisite 3.

    Is that possible? How can I do that in python?

    Continue reading...

Compartilhe esta Página