% Avalanche risk - road cleaning or not
%

% Cost
C=5000;  % Cost of clearing avanalanche site
C0=100000;  % Cost of removing risk along entire, road up front

% Transition probability
p=0.95;

%Number of locations
n=50;

% Marginal probability of risk class 2
pmarg=zeros(n,1);
for i=1:n,
  pmarg(i,1)=1-0.99*p^(i-1);
end
figure(1);
clf;
plot(pmarg)

% Prior value
PV=max(-C0,-C*sum(pmarg));
PV

PoV=zeros(n,1);
VOI=zeros(n,1)
for j=2:n-1,
  % Conditioning on x_j=1
  psum1=0;
  % forward cond
  for i=j:n,
    psum1=psum1+(1-p^(i-j));
  end
  % backward cond
  for i=1:j-1,
    psum1=psum1+0;
  end
  PoVj=max(-C0,-C*psum1)*(1-pmarg(j));

  % Conditioning on x_j=2
  psum2=0
  % forward cond
  for i=j:n,
      psum2=psum2+1;
  end
  % backward cond
  for i=1:j-1,
    psum2=psum2+(pmarg(i)/pmarg(j));
  end
  PoVj=PoVj+max(-C0,-C*psum2)*pmarg(j)

  PoV(j,1)=PoVj
  VOI(j,1)=PoV(j,1)-PV
end

VOI

%plot(pmarg)
figure(2);
clf; 
plot(VOI); title('VOI');


