## Blog

#### Percentile Contour Plots

Lonnie Chrisman 14 Mar 2012 Analytica tips I saw a graph today that I thought was pretty cool and thought I'd share it here.  In his Analytica model, Andrew DeBenedictis of E3 created a depiction energy usage percentiles by time of day in each month.  I think a plausible name for this type of plot would be a Percentile Contour plot. The Percentile Contour Plot

Andrew tells me he got the idea from the Contour/Regions Charts page that I had written many years ago on the Analytica Wiki, which shows how to create plots like these: Decision Boundaries Contour plot of Cos((X-180)*(Y-180)/50)
Nevertheless, I particularly liked the use of gray-scale to show the percentile (amount of activity), the customized scaling of percentiles, and layout of Andrew's graph.  So, how do you put a percentile contour graph like this together?  I thought I'd try it here.
First, of course, I need some data, such as `Energy_usage`, indexed by `Hour` of day and `Month`, and for that I need the `Hour` and `Month` indexes:
```Index Hour := 1..24 Index Month := 1..12```
`Variable Energy_usage := Table(Hour,Month)`
I started with randomly generated data for `Energy_usage`.   In a more realistic model, this would be a computed output.  Next, I created an index, `Percentile`, as a list of labels, and a table named `Percentile_breakpoints` encoding numeric upper bound for each percentile range.
Next, we need to map each `Energy_usage` value to a numeric `Energy_use_fractile` level:
`Rank2D(Energy_Usage,Hour,Month) / (size(Hour)*size(Month))`
and then map this to the `Percentile` range label:
`StepInterp(Percentile_breakpoin,Percentile,Energy_Use_fractile,Percentile)`
I made use here of a user-defined function, `Rank2D`, which is defined as follows:
Function Rank2D(A : Array[I,J] ; I,J:Index ; type : optional = 0)
Definition:

index K := 1..size(I)*size(J);
var r:= Rank(ConcatRows(A,I,J,K),K,type);
r[K=(@I-1)*size(J) + @J]

The result graph for `Usage_percentile` is initially a line graph that does not look anything at all like a percentile contour plot.  From here I set various graph settings and pivots.  In Graph Setup / Chart Type, I selected a symbol line style, checked Use separate color/symbol keys, and set the Symbol size to 25.  I then pivoted to Color Key:Usage Percentile, Horizontal Axis:Hour, Vertical Axis:Month. Then in Graph Setup / Axis Ranges and checked Categorical for both axes.  The plot now looks like:
My steps above took advantage of a "fix" found in the 4.4.3 patch build, and as I am writing this, I am using a pre-release version of this patch.  I was able to set the color key to Usage Percentile, which is a categorical value array, but not an index.  Prior to the 4.4.3 patch, the color key role would accept only index fillers, but now it accepts categorical value fields as well, making this setup more convenient as we saw here.   Without this 4.4.3 fix, you have to use the Symbol Size graphing role, as described at  Contour/Regions Charts.
Finally, by right-clicking on each circle in the key in turn and selecting Change Series Color..., I set the color scheme to a gray-scale, obtaining my version of a percentile contour plot:
As a final experiment, I changed the symbol shape from a circle to a square.  Analytica's graphing interface doesn't expose direct control over the symbol sequence, so I did this from the typescript interface (press F12 to reach the typescript window).  These lines in the typescript window display the current graph setup settings:

> with Usage_Percentile
> GraphSetup

I selected the text that was printed with the mouse and pressed Ctrl+C (Copy).  Then clicking at the typescript prompt, I typed:

> GraphSetup :

pressed Ctrl+V to paste just after the colon, then added a tilde to the end of the last line (the tilde in typescript means "continue on next line") and added:

Graph_symbolSeq : [2,1,3,4,5,6,7]

This swaps the positions of the first two symbols from the default symbol sequence.   I also went to Graph Setup / Style and set Grid to None.  The graph changes to look like this:
With a little resizing of the window, the graph takes on a very continuous appearance.  However, I think I prefer the circles, since they have the effect of both visually communicating the energy usage level visually with gray-scale, while also communicating that these are individual data points.
]]>

» Back ## Lonnie Chrisman

Lonnie Chrisman, PhD, is Lumina's Chief Technical Officer, where he heads engineering and development of Analytica®. He has authored dozens refereed publications in the areas of machine learning, Artificial Intelligence planning, robotics, probabilistic inference, Bayesian networks, and computational biology. He was was in eighth grade when he had his first paid programming job. He was awarded the Alton B. Zerby award "Most outstanding Electrical Engineering Student in the USA", 1987. He has a PhD in Artificial Intelligence and Computer Science from Carnegie Mellon University; and a BS in Electrical Engineering from University of California at Berkeley. Lonnie used Analytica for seismic structural analysis of an extension that he built to his own home where he lives with his wife and raised four daughters: So, he really trusts Analytica calculations!