Wildebeests | MMMM: Minerals, Metals, the 'Merican economy, and Mathematica

Archive for August 2009

I often see plots with vertical bands highlighting segments of the plot. Here’s how to make them in Mathematica.

Starting with a data set of prices we want to highlight segments of the price plot where the correlation with stockpiles is strongly positive. The plot of the price data is made using DateListPlot with some user defined plotting options:

DateListPlot[ prices, options]

Let’s assume that I already have a list of the subsets of this data that we want to highlight. I’ll call these subsets subset1,subset2,… . Next step is to add these subset lists to the list of data:

DateListPlot[ {prices,subset1,subset2,…}, options]

You won’t be able to see the subsets when you plot this unless you specifically set a different styling option for the second and subsequent lists of data that you’re plotting:

DateListPlot[ {prices,subset1,subset2,…}, PlotStyle→{colour1,colour2,colour3,…}]

But this is about creating bands and the easy way to do that is to use the Filling option. If we fill from a subset segment down to the axis plus fill to some point above the plot we can create a continuous highlighting band.

DateListPlot[ {prices,subset1,subset2,…}, Filling{{2Bottom}, {2→Top}, {3Bottom}, {3→Top},…}]

What this syntax is telling us is that we want to fill list number 2 to zero and list number 2 to 5000, the same for the 3rd, 4th and subsequent lists (the first list is our list of prices)

An example of a plot with higlight bands

An example of a plot with highlight bands

Aug/09

6

Making a Wildebeest Correlation plot

What I’ve set out to do with the Wildebeest Correlation Index is to create a rolling correlation of prices and stockpiles. The correlation data then gets plotted with the line changing colour depending on the correlation: green for negatively correlated data and red for positively correlated data. The input is a list with the form {date, price, stockpile} …

From the initial data we create a temporary list of prices and stockpiles:

temp2 = data[[All,{2, 3}]]

This list is then partitioned into subsets of length chosen by the number, num, of points required for the correlation.The partitioned list is mapped on to the Correlation function:

temp = Correlation[#[ [All,1]], #[[All,2]] ]& /@ Partition[temp2, num, 1]

Next step is to add back the dates and make the plot. To make the line change colour we want to define a ColorFunction which sets the colour to be between green and red depending on the y value of the data:

ColorFunction→Function[{x,y}, Blend[{Green,Red},y]]

The Mathematica shortform notation for this function is written as

ColorFunction→(Blend[{Green,Red},#2]&)

An example of a WCI plot

An example of a WCI plot

Related Posts with Thumbnails

· ·

Get Adobe Flash playerPlugin by wpburn.com wordpress themes

Theme Design by devolux.nh2.me