diversification_ratio#
- pymc_marketing.mmm.utility.diversification_ratio(samples, budgets)[source]#
Calculate the Diversification Ratio of a portfolio to evaluate risk distribution.
The Diversification Ratio measures the effectiveness of diversification by comparing the weighted average volatility of individual assets to the overall portfolio volatility. A higher ratio indicates better diversification, as it reflects lower correlations among assets, leading to reduced portfolio risk. It provides insight into how individual asset volatilities and their correlations contribute to the overall portfolio risk.
The Diversification Ratio is calculated as:
\[DR = \frac{\sum_{i=1}^{n} w_i \cdot \sigma_i}{\sigma_p}\]where:
\(w_i\) is the weight of asset \(i\)
\(\sigma_i\) is the volatility (standard deviation) of asset \(i\)
\(\sigma_p\) is the volatility of the portfolio
- Parameters:
- samples
XTensorVariable 2D PyTensor tensor variable where each column represents the returns of an asset.
- budgets
XTensorVariable 1D PyTensor tensor variable representing the investment amounts in each asset.
- samples
- Returns:
XTensorVariableDiversification Ratio.
- Raises:
ValueErrorIf
samplesis not 2D with asampledim, ifbudgetsis not 1D, or if the dimbudgetsis labelled with is not a dim ofsamples.
Notes
samplesmust have exactly two dims:sampleand the asset dim thatbudgetsis labelled with. The response distributionsBudgetOptimizerpasses to a utility function do not have that shape out of the box: the defaultresponse_variableis a 1D(sample,)total, and per-channel variables carry adatedim as well. Reduce overdatebefore calling this function. The optimizer invokes the utility with keyword arguments, so a wrapper’s parameters must be namedsamplesandbudgets.The ratio is undefined when the portfolio volatility is zero: a channel at exactly zero budget contributes zero in every sample, and a channel driven deep into saturation is numerically constant. Both give
0 / 0for the value and an infinite gradient, which the optimizer reports as an opaque solver failure. The default bounds allow zero budgets, so passbudget_boundswith strictly positive lower bounds when optimizing for diversification.References
Choueifaty, Y., & Coignard, Y. (2008). Toward Maximum Diversification. Journal of Portfolio Management.
Meucci, A. (2009). Managing Diversification. Risk, 22(5), 74-79.
Examples
Optimize a fitted MMM for diversification of the per-channel contribution:
from pymc_marketing.mmm.utility import diversification_ratio optimizer = mmm.budget_optimizer( start_date="2025-01-06", end_date="2025-03-31", response_variable="channel_contribution", utility_function=lambda samples, budgets: diversification_ratio( samples.sum(dim="date"), budgets ), ) result = optimizer.allocate_budget( total_budget=100.0, budget_bounds={channel: (1.0, 100.0) for channel in mmm.channel_columns}, )