# Mmm.allocate\_budget\_to\_maximize\_response generating weird decimal places results

**URL:** <https://discourse.pymc.io/t/mmm-allocate-budget-to-maximize-response-generating-weird-decimal-places-results/14828>\
**Category:** version agnostic\
**Tags:** pymc-marketing\
**Created:** [July 23, 2024, 5:19pm UTC](https://discourse.pymc.io/t/mmm-allocate-budget-to-maximize-response-generating-weird-decimal-places-results/14828 "2024-07-23T17:19:27Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![JenniferSiwu](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@JenniferSiwu](https://discourse.pymc.io/u/JenniferSiwu)\
**Post date:** [July 23, 2024, 5:19pm UTC](https://discourse.pymc.io/t/mmm-allocate-budget-to-maximize-response-generating-weird-decimal-places-results/14828/1 "2024-07-23T17:19:27Z")

</div>

Hi there, I am trying to utilize the budget allocation function using pymc-marketing version 0.7.0…  
I put the total budget as 5 (representing 5 Million) for 2 weeks… Below are my code… The result came out to weird decimal points and did not add up to 5… What could be happening here?  
Thanks in advanced!

```auto
total_budget = 5
# Define your channels
channels = spend_cols
# The initial split per channel
budget_per_channel = total_budget / len(channels)
# Initial budget per channel as dictionary.
initial_budget_dict = {channel: budget_per_channel for channel in channels}
# bounds for each channel
budget_bounds = {'spend_youtube': [0.5, 2],
                'spend_linkedin': [0.5, 1],
                'spend_ctv': [0.01, 1],
                'spend_facebook': [0.01, 1],
                'spend_other_display_prospecting': [0.01, 1],
                'spend_nonbrand_bing_low': [0.01, 1],
                'spend_nonbrand_google_low': [0.01, 1],
                'spend_pinterest': [0.01, 1],
                'spend_radio': [0.01, 2],
                'spend_dm': [1, 4]
                 }

response = mmm.allocate_budget_to_maximize_response(
    budget=total_budget,
    num_days=2,
    time_granularity="weekly",
    budget_bounds=budget_bounds,
)

budgetalloc=mmm.optimal_allocation_dict
budgetalloc

```

The result looked like this…  
 ![Screenshot 2024-07-23 at 10.19.12 AM](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/c/cf647d77cf45178482c5ac1d052fa51fcfc7dbd5.png)

---

<div class="post-metadata">

**Author:** ![YT19](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/yt19/32/8349_2.png) [@YT19](https://discourse.pymc.io/u/YT19)\
**Post date:** [July 24, 2024, 1:05am UTC](https://discourse.pymc.io/t/mmm-allocate-budget-to-maximize-response-generating-weird-decimal-places-results/14828/2 "2024-07-24T01:05:24Z")

</div>

Hi @JenniferSiwu ,  
I am facing a similar issue as well and then I started to retest my model and question the rsq of my model. I would love help here as well.

---

<div class="post-metadata">

**Author:** ![Alfredo\_Delgado](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/alfredo_delgado/32/8033_2.png) [@Alfredo\_Delgado](https://discourse.pymc.io/u/Alfredo_Delgado)\
**Post date:** [July 24, 2024, 6:40am UTC](https://discourse.pymc.io/t/mmm-allocate-budget-to-maximize-response-generating-weird-decimal-places-results/14828/3 "2024-07-24T06:40:37Z")

</div>

Hi @JenniferSiwu

You need to rescale the values in your `mmm.optimal_allocation_dict` multiplying by `mmm.channel_transformer["scaler"].scale_`. See the example below:

```auto
allocate_spend = (
                  np.array(list(mmm.optimal_allocation_dict.values()))
                  * mmm.channel_transformer["scaler"].scale_
              )

```

Hope that helps!

---

<div class="post-metadata">

**Author:** ![YT19](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/yt19/32/8349_2.png) [@YT19](https://discourse.pymc.io/u/YT19)\
**Post date:** [July 24, 2024, 5:56pm UTC](https://discourse.pymc.io/t/mmm-allocate-budget-to-maximize-response-generating-weird-decimal-places-results/14828/4 "2024-07-24T17:56:52Z")

</div>

Hi @Alfredo_Delgado  
Thanks for the update - I tried this as I was also facing a similar issue but even after rescaling the the budget splits don’t add up to the total (500K).

Here’s the code and output that I am getting. Are you facing something similar @JenniferSiwu  
total\_budget = 500000

channels = [‘Google\_Spend’,‘Bing\_Spend’,‘ASA\_Spend’,‘Facebook\_Spend’,‘TikTok\_Spend’,‘Taboola\_Spend’]

budget\_per\_channel = total\_budget / len(channels)

initial\_budget\_dict = {channel: budget\_per\_channel for channel in channels}  
response = mmm.allocate\_budget\_to\_maximize\_response(  
budget=total\_budget,  
num\_days=4,  
time\_granularity=“weekly”

)

allocate\_spend = (  
np.array(list(mmm.optimal\_allocation\_dict.values()))  
\* mmm.channel\_transformer[“scaler”].scale\_  
)

Output:  
array([148084.16420463, 12016.41390591, 2857.62798331, 58788.11413278,  
7317.07505723, 6778.25850656])  
The sum is 235841.6537904233

Thank you so much! Appreciate your help!

---

<div class="post-metadata">

**Author:** ![Alfredo\_Delgado](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/alfredo_delgado/32/8033_2.png) [@Alfredo\_Delgado](https://discourse.pymc.io/u/Alfredo_Delgado)\
**Post date:** [July 24, 2024, 10:09pm UTC](https://discourse.pymc.io/t/mmm-allocate-budget-to-maximize-response-generating-weird-decimal-places-results/14828/5 "2024-07-24T22:09:26Z")

</div>

Hi @YT19 That means you do not need to spend 500k in the following 4 weeks to maximize your response and only less than 250k is enough 🙂

---

<div class="post-metadata">

**Author:** ![JenniferSiwu](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@JenniferSiwu](https://discourse.pymc.io/u/JenniferSiwu)\
**Post date:** [July 25, 2024, 8:03pm UTC](https://discourse.pymc.io/t/mmm-allocate-budget-to-maximize-response-generating-weird-decimal-places-results/14828/6 "2024-07-25T20:03:51Z")

</div>

thanks so much @Alfredo_Delgado ! this works ☺

---

<div class="post-metadata">

**Author:** ![JenniferSiwu](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@JenniferSiwu](https://discourse.pymc.io/u/JenniferSiwu)\
**Post date:** [July 29, 2024, 5:22pm UTC](https://discourse.pymc.io/t/mmm-allocate-budget-to-maximize-response-generating-weird-decimal-places-results/14828/7 "2024-07-29T17:22:15Z")

</div>

hey both @Alfredo_Delgado @YT19 , I’m running into another issue with the budget\_bounds… it seems like the minimizer isn’t really taking into consideration the lower bound constraints… I put minimum constraint at 100K and the optimizer still outputted ~1K for some channels… Are you experiencing this as well?

![Screenshot 2024-07-29 at 10.19.50 AM](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/4/471918497801d9830ff2d961b1395d9a91333d6d.png)  
 ![Screenshot 2024-07-29 at 10.20.11 AM](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/5/552ea73cb74e0765f8ccd60efc514fb4ce31ff21.png)

As for the channel\_contribution plot, is it based on the optimal spend level outputted by then optimizer?

 ![Screenshot 2024-07-29 at 10.21.33 AM](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/7/7ac5a36aef902c2dbd31433faf3b7cb56e766e5b.png)

---

<div class="post-metadata">

**Author:** ![Alfredo\_Delgado](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/alfredo_delgado/32/8033_2.png) [@Alfredo\_Delgado](https://discourse.pymc.io/u/Alfredo_Delgado)\
**Post date:** [July 30, 2024, 2:22am UTC](https://discourse.pymc.io/t/mmm-allocate-budget-to-maximize-response-generating-weird-decimal-places-results/14828/8 "2024-07-30T02:22:18Z")

</div>

@JenniferSiwu have you looked at the saturation curves? Maybe your bounds are too high and are above the inflection and saturation points.

Also, the response vs spent chart looks odd. Small spend gets a high contribution, and high spend gets small contribution so I suspect something is wrong in your model

---

<div class="post-metadata">

**Author:** ![Natalia\_Ziemba-Janko](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/natalia_ziemba-janko/32/6754_2.png) [@Natalia\_Ziemba-Janko](https://discourse.pymc.io/u/Natalia_Ziemba-Janko)\
**Post date:** [July 31, 2024, 8:31am UTC](https://discourse.pymc.io/t/mmm-allocate-budget-to-maximize-response-generating-weird-decimal-places-results/14828/9 "2024-07-31T08:31:13Z")

</div>

Hi @Alfredo_Delgado  
I write here the same message as in [pymc-marketing github](https://github.com/pymc-labs/pymc-marketing/discussions/843)

My problem is the same but I am sure that it is not the answer 🙂 if there will be not point to spend more then if I increase the hole budget, the budget allocation should not increase. In fact - it does.  
Second thing - example notebook suggest it should allocate hole budget as the next step is limiting the budget based on saturation curves to show that it almost doesn’t affect outcome.  
Finally and the most important, in library code it is impossible to not allocate hole budget, from budget\_allocation.py

```auto
constraints = {"type": "eq", "fun": lambda x: np.sum(x) - total_budget}

```

that means that default constraints ensure us that budget will be sum up to 1. So somewhere in between something is going on badly.

---

<div class="post-metadata">

**Author:** ![VT02](https://avatars.discourse-cdn.com/v4/letter/v/50afbb/32.png) [@VT02](https://discourse.pymc.io/u/VT02)\
**Post date:** [August 8, 2024, 10:20pm UTC](https://discourse.pymc.io/t/mmm-allocate-budget-to-maximize-response-generating-weird-decimal-places-results/14828/10 "2024-08-08T22:20:30Z")

</div>

Hi All, I am facing the same issue. Even with lower bound constraints, I am getting less budget. Were you able to resolve this issue? @JenniferSiwu @Alfredo_Delgado

---

<div class="post-metadata">

**Author:** ![JenniferSiwu](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@JenniferSiwu](https://discourse.pymc.io/u/JenniferSiwu)\
**Post date:** [August 9, 2024, 7:18pm UTC](https://discourse.pymc.io/t/mmm-allocate-budget-to-maximize-response-generating-weird-decimal-places-results/14828/11 "2024-08-09T19:18:22Z")

</div>

Hi there! Yeah i still have the same issue and i ended up downgrading pymc\_marketing to version 0.6.0… ☹

---

<div class="post-metadata">

**Author:** ![juanitorduz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/juanitorduz/32/8155_2.png) [@juanitorduz](https://discourse.pymc.io/u/juanitorduz)\
**Post date:** [August 9, 2024, 9:14pm UTC](https://discourse.pymc.io/t/mmm-allocate-budget-to-maximize-response-generating-weird-decimal-places-results/14828/12 "2024-08-09T21:14:57Z")

</div>

We will be looking into this soon! Thanks for reporting this!

---

<div class="post-metadata">

**Author:** ![Alfredo\_Delgado](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/alfredo_delgado/32/8033_2.png) [@Alfredo\_Delgado](https://discourse.pymc.io/u/Alfredo_Delgado)\
**Post date:** [August 14, 2024, 6:18am UTC](https://discourse.pymc.io/t/mmm-allocate-budget-to-maximize-response-generating-weird-decimal-places-results/14828/13 "2024-08-14T06:18:02Z")

</div>

I just realised that the below will never be equal to the budget to allocate because the budget is scaled using the max channel transformer scaler `self.channel_transformer["scaler"].scale_.max()`

> [@Alfredo\_Delgado](#):
>
> ```auto
> allocate_spend = (
> np.array(list(mmm.optimal_allocation_dict.values()))
> * mmm.channel_transformer["scaler"].scale_
> )
> 
> ```

So if we want to get the re-scaled allocated budget from `self.optimal_allocation_dict` we need to use the same scaler. Note the extra `.max()` below

```auto
allocate_spend = (
                  np.array(list(mmm.optimal_allocation_dict.values()))
                  * mmm.channel_transformer["scaler"].scale_.max()
              )

```

---

<div class="post-metadata">

**Author:** ![cetagostini](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cetagostini/32/6967_2.png) [@cetagostini](https://discourse.pymc.io/u/cetagostini)\
**Post date:** [August 14, 2024, 3:43pm UTC](https://discourse.pymc.io/t/mmm-allocate-budget-to-maximize-response-generating-weird-decimal-places-results/14828/14 "2024-08-14T15:43:21Z")

</div>

Hey @Alfredo_Delgado is right.

The issue was address in the following PR → [here](https://github.com/pymc-labs/pymc-marketing/pull/933)

---

<div class="post-metadata">

**Author:** ![VT02](https://avatars.discourse-cdn.com/v4/letter/v/50afbb/32.png) [@VT02](https://discourse.pymc.io/u/VT02)\
**Post date:** [August 28, 2024, 1:55am UTC](https://discourse.pymc.io/t/mmm-allocate-budget-to-maximize-response-generating-weird-decimal-places-results/14828/15 "2024-08-28T01:55:22Z")

</div>

Hi Jennifer,

For 0.6.0, which function are you using for budget allocation. Can you provide snippet if possible to understand

---

<div class="post-metadata">

**Author:** ![JenniferSiwu](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@JenniferSiwu](https://discourse.pymc.io/u/JenniferSiwu)\
**Post date:** [September 3, 2024, 5:14pm UTC](https://discourse.pymc.io/t/mmm-allocate-budget-to-maximize-response-generating-weird-decimal-places-results/14828/16 "2024-09-03T17:14:40Z")

</div>

Yes, so i used sigmoid params to compute the budget allocation because on 0.6.0, we are unable to use the mmm fitted output yet…

```auto
opt_params = mmm.compute_channel_curve_optimization_parameters_original_scale(method='sigmoid')
 response = mmm.optimize_channel_budget_for_maximum_contribution(
            method='sigmoid',
            total_budget=total_budget,
            budget_bounds=budget_bounds,
            parameters=opt_params
        )

```

---

<div class="post-metadata">

**Author:** ![VT02](https://avatars.discourse-cdn.com/v4/letter/v/50afbb/32.png) [@VT02](https://discourse.pymc.io/u/VT02)\
**Post date:** [September 3, 2024, 6:01pm UTC](https://discourse.pymc.io/t/mmm-allocate-budget-to-maximize-response-generating-weird-decimal-places-results/14828/17 "2024-09-03T18:01:03Z")

</div>

Got it, also to confirm how would the model understand the granularity. If the data is on weekly level and we would want to predict for 12 weeks, how would that be taken into consideration? Do you happen to have any idea on it?

---

<div class="post-metadata">

**Author:** ![VT02](https://avatars.discourse-cdn.com/v4/letter/v/50afbb/32.png) [@VT02](https://discourse.pymc.io/u/VT02)\
**Post date:** [September 5, 2024, 1:29am UTC](https://discourse.pymc.io/t/mmm-allocate-budget-to-maximize-response-generating-weird-decimal-places-results/14828/18 "2024-09-05T01:29:34Z")

</div>

Hi @cetagostini,

I modified the code to include `max()` at the end for optimized budget allocation. In the `allocate_budget_to_maximize_response()` function, I set the budget per unit (e.g., for a total budget of 50 over 10 weeks, I used 5 as the budget). However, after running the allocation, one channel gets the maximum budget while the rest are assigned the minimum values.

Could you clarify how the min and max bounds work and what values should be set to achieve a more balanced budget allocation?

---

<div class="post-metadata">

**Author:** ![cetagostini](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cetagostini/32/6967_2.png) [@cetagostini](https://discourse.pymc.io/u/cetagostini)\
**Post date:** [November 7, 2024, 1:16pm UTC](https://discourse.pymc.io/t/mmm-allocate-budget-to-maximize-response-generating-weird-decimal-places-results/14828/19 "2024-11-07T13:16:38Z")

</div>

Hello,

Indeed, the budget that goes into the optimizer and the bounds is per unit of time. If you set a budget of 5 dollars and you have 5 weeks, the total is 25 dollars for the 5 weeks.

If you have a budget bound the 10 for each channel, basically that channel should not spend more than 10\*N weeks (days or months) based on your time unit.  
@VT02
