You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Two different versions for weighted lorenz curve calculation in the examples #28534
There are 2 definitions of (weighted) lorenz_curve() functions here and here
The difference is in the X coordinates that these functions returns. Both return X coordinates between 0 and 1, but the first example returns equally spaced X coordinates:
Indeed this looks like a bug. Someone would need to do a quick literature review to find the correct way to compute this curve when there are per-individual weights / exposure.
EDIT: I have the feeling that the cumsum variant is the correct one but I am not 100% sure.
Indeed this looks like a bug. Someone would need to do a quick literature review to find the correct way to compute this curve when there are per-individual weights / exposure.
EDIT: I have the feeling that the cumsum variant is the correct one but I am not 100% sure.
I'm not expert but something I find strange in the unweighted version is that it does use the weights (exposure) but differently: cumulated_claim_amount = np.cumsum(ranked_pure_premium * ranked_exposure)
In a more intuitive terms - it use the weights to make the bar higher but not wider (each bar has the same width but the height of each bar is affected by the weight where in the weighted version the weight affects both the height and the width of each bar)
Indeed I agree it's weird to only use the weights on y axis and not on the x axis.
I would intuitively expect the following weighing semantics to hold:
setting weights to 0 to a fraction of the observations should be equivalent to trimming those data points;
setting weights to 2 to a fraction of the observations (while keeping the other to unit weights) should be equivalent to duplicating those data points.
We can empirically check when those properties hold: the curves for the weighted vs trimmed/duplicated but unweighted data should match exactly.
Indeed I agree it's weird to only use the weights on y axis and not on the x axis.
I would intuitively expect the following weighing semantics to hold:
setting weights to 0 to a fraction of the observations should be equivalent to trimming those data points;
setting weights to 2 to a fraction of the observations (while keeping the other to unit weights) should be equivalent to duplicating those data points.
We can empirically check when those properties hold: the curves for the weighted vs trimmed/duplicated but unweighted data should match exactly.
I find this topic interesting and I would like to contribute.
I started from the definition of Lorenz curve from this paper:
and checked that the approach with the cumulative sums in the scikit-learn examples matches the theory:
To check the behaviour when weighting I simulated unweighted data from a Poisson distribution with parameter 0.1 and then aggregated it. I aggregated the exposure and the claim count and computed again the frequency for the weighted data (claim count divided by exposure).
You can find this example here
My reasoning: When simulating iid samples from a poisson with parameter 0.1, we have many 0s and few values >= 1. If we aggregate the data the claim frequencies will converge toward 0.1 by increasing group sizes, hence there will be no inequality at all and the Lorenz curve will be the diagonal line.
Update:
I did another example where I followed the procedure for creating weighted data in #29248 where one first simulates the weighted data and then repeats the observations to get the unweighted data.
When building the Lorenz curve using the weighted data I then used this approach
Activity
ogrisel commentedon Feb 27, 2024
Indeed this looks like a bug. Someone would need to do a quick literature review to find the correct way to compute this curve when there are per-individual weights / exposure.
EDIT: I have the feeling that the cumsum variant is the correct one but I am not 100% sure.
lamdang2k commentedon Feb 29, 2024
I think you're right, the horizontal axis is the cumulative sum of probability Pr(Y=y). Here it corresponds to the individual weight/exposure.
We can use linspace when all claims are equally probable with probabilities 1/n.
Source: https://en.wikipedia.org/wiki/Lorenz_curve#:~:text=For%20a%20discrete,to%20n%3A
lorentzenchr commentedon Mar 8, 2024
One version is unweighted, the other takes into account sample weights. So neither is wrong.
See also #21320 (comment) and note that wikipedia is not a reliable source on those quantities.
yonil7 commentedon Mar 8, 2024
I'm not expert but something I find strange in the unweighted version is that it does use the weights (exposure) but differently:
cumulated_claim_amount = np.cumsum(ranked_pure_premium * ranked_exposure)In a more intuitive terms - it use the weights to make the bar higher but not wider (each bar has the same width but the height of each bar is affected by the weight where in the weighted version the weight affects both the height and the width of each bar)
ogrisel commentedon Mar 28, 2024
Indeed I agree it's weird to only use the weights on y axis and not on the x axis.
I would intuitively expect the following weighing semantics to hold:
We can empirically check when those properties hold: the curves for the weighted vs trimmed/duplicated but unweighted data should match exactly.
m-maggi commentedon Jun 15, 2024
I find this topic interesting and I would like to contribute.

I started from the definition of Lorenz curve from this paper:
and checked that the approach with the cumulative sums in the scikit-learn examples matches the theory:
To check the behaviour when weighting I simulated unweighted data from a Poisson distribution with parameter 0.1 and then aggregated it. I aggregated the exposure and the claim count and computed again the frequency for the weighted data (claim count divided by exposure).
You can find this example here
My reasoning: When simulating iid samples from a poisson with parameter 0.1, we have many 0s and few values >= 1. If we aggregate the data the claim frequencies will converge toward 0.1 by increasing group sizes, hence there will be no inequality at all and the Lorenz curve will be the diagonal line.
Update:
I did another example where I followed the procedure for creating weighted data in #29248 where one first simulates the weighted data and then repeats the observations to get the unweighted data.
When building the Lorenz curve using the weighted data I then used this approach
and obtained points lying on the unweighted curve.
ogrisel commentedon Oct 28, 2024
@m-maggi sorry I had not seen your reply.
Could you try to do the simple experiment:
And then compute the curves for:
vs
ogrisel commentedon Oct 28, 2024
But feel free to open a PR to fix the wrong example in any case.