%% Stochastic Dynamic Models
%
% We will use the CompEcon Toolbox solvers:
% ddpsolve()    -   Solves discrete-state/action dynamic program
%
%% Example 1: Water Management Model
%
%% Problem Statement:
%
% Water from a reservoir can be used for either irrigation or recreation. Irrigation during the spring benefits farmers but reduces the reservoir level during the summer, damaging the interests of recreational users. Specifically, if the reservoir contains sunits of water at the beginning of the year and x units are released for irrigation, farmer and recreational user benefits during the year will be F(x) and U(s-x), respectively. Reservoir levels are replenished by random rainfall during the winter. Specifically, it rains k units with probability p(r), for r=0,1,2,...,R. The reservoir can hold M units of water, and excess rainfall flows out without benefit to either farmer or recreational user.
% What irrigation policy maximizes the sum of farmer and recreational user benefits over an infinite time horizon?
%

% DEMDDP05 Water Management Model
% This program solves the water management model under the high rainfall
% scenario.
  fprintf('\nDEMDDP05 WATER MANAGEMENT MODEL\n')
  clc;clear;
  close all
  figure('units','normalized','outerposition',[0 0 0.75 1]) % make plot window open to X-fraction of screen
  figure(1);

%

% Enter model parameters
  alpha1 = 14;                      % producer benefit function parameter
  beta1  = 0.8;                     % producer benefit function parameter
  alpha2 = 10;                      % recreational user benefit function parameter
  beta2  = 0.4;                     % recreational user benefit function parameter  
  maxcap = 30;                      % maximum dam capacity
  r      = [0 1 2 3 4];             % rain levels
  p      = [0.1 0.2 0.4 0.2 0.1];   % rain probabilities
  delta  = 0.9;                     % discount factor

%

% Construct state space
  S = (0:maxcap)';              % vector of states
  n = length(S);                % number of states

% Construct action space
  X = (0:maxcap)';              % vector of actions
  m = length(X);                % number of actions

%

% Construct reward function

  f = zeros(n,m);
  
  i=1;
  k=1;
  
  for i=1:n
    for k=1:m
        
        if k>i
            
            f(i,k) = -inf;
        
        else
            
            f(i,k) = alpha1 * X(k).^beta1 + alpha2 * (S(i) - X(k)).^beta2; 
        
        end
        k=k+1;
    end
    i=i+1;
  end

%

% Construct state transition matrix

  P = zeros(m,n,n);  
  k=1;
  i=1;
  j=1;
  
  for k=1:m
    for i=1:n
        for j=1:length(r)
      
            snext = min( S(i) - X(k) + r(j), maxcap );
        
            inext = getindex(snext, S);
        
            P(k,i,inext) = P(k,i,inext) + p(j);
            
            j=j+1;
    
        end
        i=i+1;
    end
    k=k+1;
  end

%

% Pack model structure

  clear model
  model.reward     = f;
  model.transprob  = P;
  model.discount   = delta;

%

% Solve infinite-horizon model using policy iteration

  [v,x,pstar] = ddpsolve(model);

%

% Plot optimal policy
  subplot(2,2,1); 
  h=plot(S, X(x), '-*'); 
  title('Optimal Irrigation Policy');
  xlabel('Water Level'); ylabel('Irrigation');
  xlim([-1 31])
  ylim([0 6])

%

% Plot optimal value function
  subplot(2,2,2); 
  plot(S, v); 
  title('Optimal Value Function');
  xlabel('Water Level'); ylabel('Value');

%

% Generate random optimal paths, starting from zero water level
  sinit = ones(10000, 1); 
  nyrs  = 30;
  spath = ddpsimul(pstar, sinit, nyrs);

%

% Plot expected water level over time, starting from zero water level
  subplot(2,2,3); 
  plot(0:nyrs, mean(S(spath)));
  title('Optimal State Path'); 
  xlabel('Year'); ylabel('Water Level');
  ylim([0 14])

%

% Compute steady state distribution of water level
  subplot(2,2,4); 
  pi = markov(pstar);
  h=bar(S,pi,1); set(h,'FaceColor',[.75 .75 .75])
  title('Steady State Distribution');
  xlabel('Water Level'); ylabel('Probability'); 
  xlim([-1 31])
  ylim([0 0.16])

%

% Compute steady-state water level
  avgstock = pi'*S;
  fprintf('\nSteady-state Stock        %8.2f\n',avgstock)

%
%% In Lab Exercise:
%
% Modify the above script to conduct a sensitivity analysis:
% A climatologist predicts the rainfall pattern will change in the study region in the future and the predicted rainfall distribution in the next 50 years is given below.
% r={0,1,2,3,4}
% p(r)={0.2,0.5,0.1,0.1,0.1}
% What is the impact of the climate change on the optimal irrigation policy, and steady state distribution of the water level in the reservoir?


  
  
  
  
  
  
  
  
  
  

%%