/* [wxMaxima batch file version 1] [ DO NOT EDIT BY HAND! ]*/
/* [ Created with wxMaxima version 0.8.5 ] */

/* [wxMaxima: comment start ]
   Eric Doviak          
   22 April 2012        
                        
   Math Methods 7025X   
   Problem #3           
   [wxMaxima: comment end   ] */

/* [wxMaxima: comment start ]
   A mortgage lender seeks to maximize the expected value of its portfolio.
   No generality is lost by examining the case of one loan.
   
   E[port] = (1−p)*B + p*g*B
   
   Lenders may not recover more than the principal balance through the foreclosure process. 
   So when a borrower is "underwater," the lender recovers than the principal balance.

   g*B == V-L

   Probability of foreclosure is an increasing function of "balance-to-value ratio."

   p  == p(B/V)
   p' >  0

   In the notebook below, we'll use capital P for the probability (the cumulative distribution)
   and lowercase p for its derivative (the probability density function).
   [wxMaxima: comment end   ] */

/* [wxMaxima: input   start ] */
/* expected value of "portfolio" (here, one loan) */
EPort(B):=B*(1-P(B/V))+P(B/V)*(V-L)$

/* since we're defining in terms of _reducing_ balance */
/* MC and MB are reversed */

/* marginal benefit of reducing principal balance */
MB(B):=(1/V)*p(B/V)*(B-(V-L))$

/* marginal cost of reducing principal balance */
MC(B):=1-P(B/V)$

print("")$
print("expected value of portfolio")$
print("")$
print("EPort(B) = ",EPort(B))$
print("")$
print("")$
print("marginal benefit of reducing principal balance")$
print("")$
print("MB(B) = ",MB(B),"")$
print("")$
print("")$
print("marginal cost of reducing principal balance")$
print("")$
print("MC(B) = ",MC(B),"")$
print("")$
/* [wxMaxima: input   end   ] */

/* [wxMaxima: comment start ]
   MC and MB are reversed because the problem is cast in terms of
   the costs and benefits of _reducing_ the principal balance.
   [wxMaxima: comment end   ] */

/* [wxMaxima: comment start ]
   Since we're working with probability, we need to specify a distribution.
   Here, we'll use lognormal. 
   [wxMaxima: comment end   ] */

/* [wxMaxima: input   start ] */
/*  lognormal distribution  */
P(x):=(1/2)*(1+erf((beta*log(x)-mu)/sqrt(2*s^2)))$
p(x):=''(diff(P(x),x))$

/* lognormal */
s:0.39$ mu:0.5$ beta:1$

print("")$
print("cumulative lognormal density, with: s=0.39, mu=0.5, beta=1")$
wxplot2d(P,[x,0.01,5]);

print("")$
PAtOne:''(float(P(1)))$
PAtTwo:''(float(P(2)))$
print("when x=1, probability is:",PAtOne)$
print("when x=2, probability is:",PAtTwo)$
/* [wxMaxima: input   end   ] */

/* [wxMaxima: comment start ]
   Let's plot E[port] as a function of the principal balance. And let's plot the
   Marginal Cost and Marginal Benefit of principal balance reductions.
   [wxMaxima: comment end   ] */

/* [wxMaxima: input   start ] */
/* value normalized to one */
V:1$ 

/* legal fees are 5 percent of sale value */
L:0.05$

/* refresh the functions */
EPort(B):=B*(1-P(B/V))+P(B/V)*(V-L)$
MB(B):=(1/V)*p(B/V)*(B-(V-L))$
MC(B):=1-P(B/V)$
/* [wxMaxima: input   end   ] */

/* [wxMaxima: input   start ] */
print("")$
print("expected value of the portfolio")$
wxplot2d(EPort,[B,1,3]);

print("")$
print("marginal benefit and marginal cost of principal balance reductions")$
wxplot2d([MB,MC],[B,1,3]);
/* [wxMaxima: input   end   ] */

/* [wxMaxima: comment start ]
   MC and MB are reversed because the problem is cast in terms of
   the costs and benefits of _reducing_ the principal balance.
   [wxMaxima: comment end   ] */

/* [wxMaxima: comment start ]
   Now, let's calculate the optimal principal balance.
   [wxMaxima: comment end   ] */

/* [wxMaxima: input   start ] */
/* B at maximum and percentage recovered */
max:subst(lbfgs(-EPort(B),[B],[1.01],0.0001,[-1,0]),B)$

/*  percentage recovered depends on how far underwater borrower is  */
/*  balance is greater than sale value if:  V < B                   */
G(B):=(V-L)/B$
g(B):=''(diff(G(B),B))$

print("")$
print("optimal B: ",max)$
print("optimal principal balance exceeds value (recall that: V=1)")$
print("")$
print("percentage recovered: ",G(max))$
/* [wxMaxima: input   end   ] */

/* Maxima can't load/batch files which end with a comment! */
"Created with wxMaxima"$
