Thursday, February 25, 2016

How to think about Dynamic Programming in Matlab code

(1) Dynamic programing is a multi-path-backward-recursive algorithm, i.e. known end result=> max target today.
(2) Oil reserve with normal/enhance 10k/20k exploration => recombining binary tree=> array
(3) value function on the binary tree has known result [0,0,0,0] at the end t=4.
(4) Binary Tree would have two value function Vn/Ve and two backward calculation, price progression 45-30-40
(5) [x]=dynProg() and x(1).Vn vs x(1).Ve will tell you which branch to go. Back-calc=>Forward visualization
(6) Code is not visual enough. Visualize an algorithm is a good WPF project.

function [x]=dynProg()
% states --oil reserve
x(1).rsrv=[600000];
x(2).rsrv=[500000 400000];
x(3).rsrv=[400000 300000 200000];
x(4).rsrv=[300000 200000 100000 0];

% Normal and Enhanced Pumping
% Value Onward at t=4 is 0 due to lease expiration
x(4).Vn=[0 0 0 0];
x(4).Ve=[0 0 0 0];
x(4).V=max(x(4).Vn,x(4).Ve);

% Value
% Onward=oilPrice*policy-multiplier*policy^1/reserve+discountedValueOnward
for i=1:3
   x(3).Vn(i)=40*100000- 1*100000^2/x(3).rsrv(i)+0.9*x(4).V(i);
   x(3).Ve(i)=40*200000- 20*200000^2/x(3).rsrv(i)+0.9*x(4).V(i+1);
end
% ValueOnward= Max (Norma, Enahnced)
x(3).V=max(x(3).Vn,x(3).Ve);

for i=1:2
   x(2).Vn(i)=30*100000- 1*100000^2/x(2).rsrv(i)+0.9*x(3).V(i);
   x(2).Ve(i)=30*200000- 20*200000^2/x(2).rsrv(i)+0.9*x(3).V(i+1);
end
x(2).V=max(x(2).Vn,x(2).Ve);

x(1).Vn(1)=45*100000- 1*100000^2/x(2).rsrv(1)+0.9*x(2).V(1);
x(1).Ve(1)=45*200000- 20*200000^2/x(2).rsrv(1)+0.9*x(2).V(2);
x(1).V=max(x(1).Vn,x(1).Ve)

No comments:

Post a Comment