MODEL:
 ! Newsvendor model with Endogenous Randomness, i.e., our decision
 variables affect the distribution of the random variables.
   Essential idea: 
     Stage 0: Choose parameters of the distribution.
     Stage 1 beginning: Generate a standard random variable.
     Stage 1 end: Transform standard r.v. to desired r.v. with
                  chosen parameters.
   This specific case:
 We can invest in advertising to increase mean demand.
 We can invest in forecasting to decrease standard deviation in demand.
 So our decisions in stage 0 affect the distribution of demand
 seen in stage 1;

! Keywords: Advertising, Forecasting, Endogenous uncertainty, Uncertainty,
   Stochastic optimization;

! Core model ---------------------------------------------------+;
  DATA:
   COST = 30;  ! Purchase cost per unit;
   SELLAT = 60;   ! Selling price per unit;
   COSTDUMP = 10; ! Cost/unit of disposing excess units;
   COSTSHORT = 5; ! Shortage penalty per unit;

!  Possible mean demands....; 
   MU0 = 60;  MU1 = 61;  MU2 = 62;
 !as a function of amount spent on advertising;
   CMU0 = 0; CMU1 = 25; CMU2 = 70;

! Possible standard deviations in forecast error...;
  SD0 = 12;  SD1 = 11;  SD2 = 10;
 !as a function of amount spent on forecasting, surveys, etc.;
  CSD0 = 0; CSD1 = 14; CSD2 = 40;

   SAMPSIZE = 10; ! Sample size;
  ENDDATA

! Core model;
   [R_OBJ] MAX = PROFIT;

   PROFIT = SALES - COST * Q - DISPOSAL_COSTS - SHORTAGE_COSTS
            - INVADVRT - INVFCAST; ! Minus investment in advertising & forecasting;
 
   SURP - SHORT = Q - FDEMAND;  ! Compute units over and under demand;
   DISPOSAL_COSTS = COSTDUMP * SURP;

   SHORTAGE_COSTS = COSTSHORT * SHORT;

   SALES = SELLAT * ( FDEMAND - SHORT);

! Mean demand. The WMUi choose a point on the piecewise
   linear curve;
   DMUADJ   = WMU0*MU0  + WMU1*MU1  + WMU2*MU2;
! Amount invested in advertising;
   INVADVRT = WMU0*CMU0 + WMU1*CMU1 + WMU2*CMU2;
          1 = WMU0      + WMU1      + WMU2;

! Effect of forecasting investment;
   DSDADJ   = WSD0*SD0  + WSD1*SD1  + WSD2*SD2;
   INVFCAST = WSD0*CSD0 + WSD1*CSD1 + WSD2*CSD2;
          1 = WSD0      + WSD1      + WSD2;
   
! Final demand, taking into account advertising and forecasting,
  convert the standard random variable, DEMAND, to endogenous r.v.;
   FDEMAND = DMUADJ + DSDADJ*DEMAND;
   @FREE( DEMAND);
   @FREE( FDEMAND);
   
! SP related declarations --------------------------------------+;
! 1) Staging information for decision variables;
   ! Initial decision is the order quantity at stage 0;
   @SPSTGVAR( 0, Q);  ! Quantity stocked;
   @SPSTGVAR( 0, INVADVRT); ! Investment in advertising;
   @SPSTGVAR( 0, INVFCAST); ! Investment in forecasting;
   @SPSTGVAR( 0, DMUADJ); ! Adjusted mean demand;
   @SPSTGVAR( 0, DSDADJ); ! Adjusted standard deviation in demand;


   ! Demand is a random variable in stage 1;
   @SPSTGRNDV( 1, DEMAND);

! 2) Distributions;
   ! Generic demand is Normal 0, 1;
   @SPDISTNORM( 0, 1, DEMAND);

 ! 3) Number scenarios or sample size;
   @SPSAMPSIZE( 1, SAMPSIZE);
END