The remainder of the model after the core model is the stochastic declarations section:

! SP Related Declarations -----------------------------+;

 

! Returns are the random variables;

@FOR( PERIODS( P) | P #GT# @INDEX( P0):

@SPSTGRNDV( P - 1, RETURN( P));

 );

 

! Sell(P0) is the init decision,

 Sell(P1..P5) are the recourse variables;

@FOR( PERIODS( P):

@SPSTGVAR( P - 1, SELL( P));

 );

 

SETS:

 

! Declare the outcome table distribution for returns;

  OUTCOMES: O_RETURN; !An outcome set with a single;

                      ! attribute of stock's return;

ENDSETS

 

DATA:

! Probability distribution and sampling data;

  SAMP_SZ = 0 4 4 4 4 4;        !sample size/period;

  O_RETURN = .09 .03 -.01 -.08; !4 outcomes for return;

ENDDATA

 

! Declare outcome table distribution for return;

@FOR( PERIODS( P) | P #GT# 1:

@SPDISTTABLE( O_RETURN, RETURN( P));

 );

 

! Set the sample sizes for the stages;

@FOR( PERIODS( P) | P #GT# 1:

@SPSAMPSIZE( P - 1, SAMP_SZ( P));

 );

 

END

Stochastic Declarations for Stock Option Example

Step 2 - Identifying the Random Variables:

As mentioned, the random variables in this example are the stock price returns, RETURN, for periods 1 through 5.  We indicate this with the expression:

! Returns are the random variables;

@FOR( PERIODS( P) | P #GT# @INDEX( P0):

@SPSTGRNDV( P - 1, RETURN( P));

 );

Note that return in the initial period, P0, is not valid in this model and we zeroed it out above.

Step 3 - Identifying the Initial Decision and Recourse Variables:

The initial decision is deciding whether or not to sell some, or all, of the option position in the initial period.  The recourse decisions are whether or not to sell some of our position in periods 1 through 5.  We set this up with the following:

! Sell(P0) is the initial decision,

 Sell(P1..P5) are the recourse variables;

@FOR( PERIODS( P):

@SPSTGVAR( P - 1, SELL( P));

 );

 

Step 4 - Declare Distributions

We have a single discrete distribution for the returns on the underlying stock.  As with the previous gas-buying example, this distribution is represented by an outcome table.  However, unlike the previous example, we will make use of the matrix-based @SPDISTTABLE function for declaring our outcome table and its random variables.  The code in the model devoted to declaring this distribution is:

SETS:

 

! Declare the outcome table distribution for returns;

  OUTCOMES: O_RETURN; !An outcome set with a single;

                      ! attribute of stock's return;

ENDSETS

 

DATA:

! Probability distribution and sampling data;

  SAMP_SZ = 0 4 4 4 4 4;        !sample size/period;

  O_RETURN = .09 .03 -.01 -.08; !4 outcomes for return;

ENDDATA

 

! Declare outcome table distribution for return;

@FOR( PERIODS( P) | P #GT# 1:

@SPDISTTABLE( O_RETURN, RETURN( P));

 );

 

! Set the sample sizes for the stages;

@FOR( PERIODS( P) | P #GT# 1:

@SPSAMPSIZE( P - 1, SAMP_SZ( P));

 );

 

END

First, we define a set OUTCOMES with an attribute O_RETURN. O_RETURN will be used to hold the four possible outcomes from the stock's return in a period.  Next, we call @SPDISTTABLE to declare the outcome table for each RETURN random variable in periods 1 through 5.

We then set the samples size for each of the periods to 4 in the data section:

SAMP_SZ = 0 4 4 4 4 4;        !sample size/period;

and then pass them to LINGO's SP solver in the @FOR loop:

! Set the sample sizes for the stages;

@FOR( PERIODS( P) | P #GT# @INDEX( P0):

  @SPSAMPSIZE( P - 1, SAMP_SZ( P));

);

Note that a sample size is not relevant for the initial period, P0, given that no random variables are permitted in the initial period.  So, we have explicitly avoided passing a sample size for period P0.

In the previous gas-buying example, we had only one stage and three outcomes, resulting in only three possible scenarios.   This current model has a much larger set of scenarios.  In this example, there are 5 stages with 4 possible outcomes for return in each stage.  This results in 45=1024 scenarios.   In order to keep the number of scenarios down to a more manageable size in larger models, you may need to reduce the sample sizes in each stage.  As an example you might want to try running this same model with the following sample sizes:

SAMP_SZ = 0 4 4 2 2 2;        !sample size/period;

This cuts the number of scenarios in the underlying SP model to 4*4*2*2*2=128 scenarios.  This should help to dramatically cut the runtime, but at the expense of potentially less accurate results due to sampling error.