Friday 23 December 2011

Advanced Dynamic Updating of Plots

I will use a LocatorPane, embedded in a DynamicModule, in a way similar to what I've already discussed here. The difference is that some elements of the Plot will not be updated continuously as the user drags the locators, but will only refresh once the drag-and-drop process is complete, i.e. when the mouse button is back up. I found this useful in illustrating a model in which a number of firms compete in terms of both the quantity of output and the minimum price at which they are willing to sell it (see my paper Games as Social Conventions for details). 

The outcome is demonstrated below for n = 3 firms, marginal costs c = 40, 'sunk' costs g = 30 and a linear demand function P(Q) = 100 - Q. The firms' quantity-price choices are represented by the three rectangular locators, where the price is displayed inside each locator, and the quantities are shown as subsequent intervals on the horizontal axis.



Observe that only once the mouse button is released, the firms are re-ordered (if necessary) so that the reservation price increases from left to right. The resulting step-wise aggregate supply schedule is then shown, together with the market-clearing price (in boldface on the vertical axis) and the areas representing the profits of a particular firm (in red, where any sunk costs of producing the output that remains unsold are shown in black and deducted accordingly).

This requires a rather lengthy code, so I'll skip discussing some of the functions involved. We have:

The key variable here is
loc, which lists the coordinates of each of the three locators, with initial values specified by locIn. These can be whole numbers from 0 to 100, as detailed in the last line of code.

A crucial feature that I'd like to focus on is the use of Dynamic in updating loc within the LocatorPane (line 3). This entails specifying a list of two functions as input (one after loc in line 3, the other in lines 4-5), to be evaluated respectively during and after the interactive changing of loc (which is done by dragging the locators). Those functions will in turn continuously update the variables that determine the look of the dynamically updated background of the LocatorPane (last line), which is basically the Plot that we wish to generate.

The two functions reset the value of dAll to either True (after loc is updated) or False (while loc is being updated), to specify whether all elements of the  background are to be drawn. They also use sub-functions updLoc and updQps to update the value of loc (i.e. the locator positions), as well as the corresponding quantity-price values stored as qps. This is done on the basis of the horizontal ordering of the firms, determined by the variables qpsO and ord

However, the first of the two functions uses existing ordering (one that was valid before the user started dragging a locator). For instance (as seen midway through the above video), when a locator is dragged above the one to the right of it, the two do not instantaneously 'jump' and switch places (as the new price ordering would dictate), which would have been visually confusing and difficult to follow. Only once the mouse button is released, the second of the two functions updates 
qpsO and ord (based on the new prices), and the locator positions are re-calculated accordingly. Once this is achieved, the market clearing price pE and the quantity qE that the firm is able to sell are computed.

This two-stage process is reflected by the behaviour of the background, defined as follows:

The additional components of the plot, specified by the Epilog option, are conditional on the value of dAll. When it is False (i.e. while a locator is dragged), only the graphics output by drawAlways is shown. Otherwise, drawAfter is displayed as well, where the Ticks and the Label of the plot are similarly contingent on dAll

The resulting interactive plot is displayed below (needs the usual Mathematica Player to be installed):


Click here to download the full Mathematica source code.