# How to take cumulative sum of a 2D distribution?

How can I take the cumulative sum of a 2D distribution, starting from the highest value in the distribution?

Background: my 2D distribution is the probability of different combinations of wave heights and wave periods based on measurements made at an ocean site. Assume the distribution shows that the most common wave (highest probability, 1%) has a height of 3 meters and a period of 8 seconds. This combination represents the "peak" of a "mountain" in the distribution, i.e. adjacent combinations of wave height/period occur often but less frequently (say 0.1%), and combinations further "downhill" occur even less frequently (say 0.01%). Non-zero probability is spread out over many bins of wave height and wave period so it still totals to 1.

I want to be able to identify the sea states that occur 10% of the time, i.e. make a contour that encloses the height/period combinations that together add up to a probability of 10%. In other words, I can look at the contour and say "10% of the time, wave heights are between 1 and 4 meters and periods are between 6 and 10 seconds". I believe I have to contour the cumulative sum of the 2D distribution starting at the "peak" and working "downhill". What is confusing is that downhill extends in 2 directions since I am summing over 2 variables, height and period.

Any advice is helpful since I am having trouble wrapping my mind around this.

• It sounds like you're after the coverage of a highest density region. – Glen_b Aug 13 '16 at 5:55

• This is the right approach, but the solution can be obtained in a much easier fashion: having rasterized a density estimate, all you need to do is invert the cumulative sum of the values on the raster. This is much faster and simpler than looping over values and plotting contours. (This question was asked several years ago on the GIS site; answers include working R code, which requires all of six lines to find and display the solution.) A closely related question appears at stats.stackexchange.com/questions/63447. – whuber Aug 18 '14 at 14:25