% NDPM Nondemocratic Politics Model
  fprintf('\nNDPM Nondemocratic Politics MODEL\n')
  
% Enter model parameters
  thita =0.65;
  ybar = 5000;
  delta = 0.2;
  beta  = 0.9;                        % discount factor  
  
% Construct state space
  S = ((1:100)*0.01)';                 % threat 
  n = length(S);                       % number of states

% Variables 
  tau = 5*(1-S)/6 ;                    % Tax
  C = 2/5*tau.^(5/2);                  % Govenment Cost
  yp = (1-thita)*ybar/(1-delta);       % Income  
  
% Construct reward function (revolution=1, endure=2)
  f = [(1-S)*ybar/(1-delta) (1-tau)*yp+(tau-C)*ybar]; 
%  f(end,1) = -inf;
  
% Construct state transition function
  g = zeros(n,2);
  for i=1:n
     g(i,1) = min(i,n);                           % revolution
     g(i,2) = 50;                                 % endure
  end
  
% Pack model structute
  clear model
  model.reward     = f;
  model.transfunc  = g;
  model.discount   = beta;

% Solve infinite-horizon model using policy iteration
  [v,x,pstar] = ddpsolve(model);

% Plot Optimal Value
  figure(1);subplot(2,2,1); plot(S,v); 
  title('Optimal Value Function');
  xlabel('Threat'); ylabel('Value');

% Plot Endure-Revolution Payoff
  subplot(2,2,2); plot(S,f); 
  title('Endure-Revolution Payoff');
  xlabel('Threat'); ylabel('Payoff');
  legend('Revolution','Endure');
  xlim([0 1])
  
% Plot Tax & Value
  subplot(2,2,3); plot(tau,v);
  title('Tax & Value Function');
  xlabel('Tax'); ylabel('Value');
  xlim([0 0.8])

% Plot Tax & Payoff
  subplot(2,2,4); plot(tau,f);
  title('Tax & Payoff');
  xlabel('Tax'); ylabel('Payoff');
  legend('Revolution','Endure');
  xlim([0 0.8])
  
% Plot Endure-Revolution Payoff
  figure(2);subplot(1,2,1); plot(S,f); 
  title('(a) Endure-Revolution Payoff');
  xlabel('Threat'); ylabel('Payoff');
  legend('Revolution','Endure');
  xlim([0 1])
  
% Plot Tax & Payoff
  subplot(1,2,2); plot(tau,f);
  title('(b) Tax & Payoff');
  xlabel('Tax'); ylabel('Payoff');
  legend('Revolution','Endure');
  xlim([0 0.8])