File size: 3,658 Bytes
2a8276b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

p.rm0        = p.rm;
p.rm1        = (1 + 0.015)^(1/4) - 1;

p.P0         = 1;
p.P1         = 1; 

p.mbar0      = p.rm0/(1 - (1 + p.rm0)^(-p.D));            % minimum payment required per 1 of initial debt
p.mbar1      = p.rm1/(1 - (1 + p.rm1)^(-p.D));            % minimum payment required per 1 of initial debt

p.rm         = [];
p.mbar       = [];

p.rmgrid     = [p.rm0; p.rm1]; 
p.Pgrid      = [p.P0;  p.P1];

p.rgrid      = [1; 2];                                   % index for whether mortgage is old o rnew

p.nr         = 2;                                        % number of possible mortgage contracts

fprintf('\n');
fprintf('PV of savings from rate refi (discounted at old rm)    = %9.2f\n',  (p.mbar0 - p.mbar1)*(1 - (1 + p.rm0)^(-p.D))/p.rm0*p.thetam*p.hbar);
fprintf('\n');

% Construct grids: 

sv           = gridmake(sv,    [1; 2]); 
sw           = gridmake(sw,    [1; 2]); 
svbar        = gridmake(svbar, [1; 2]); 

cmax         = bisect('savings', 1e-13, 1e5, p.lgrid, p, amin);     % c that implies a' = amin
cmin         = bisect('savings', 1e-13, 1e5, p.lgrid, p, amax);     % c that implies a' = amax

cmax         = repmat(cmax, p.nt*p.nr, 1); 
cmin         = repmat(cmin, p.nt*p.nr, 1); 

Vbar         = repmat(Vbar, p.nr, 1); 


for iter = 1 : 5

    Vbarold     = Vbar;
    
    EV          = griddedInterpolant({p.agrid, p.tgrid, p.rgrid},  reshape(Vbar, p.na, p.nt, p.nr), intmeth, 'linear');
 
    % solve consumption-savings choice
    
    c           = solve_golden('wfunc_new', cmin, cmax, sw, EV, p);
    
    [~, aprime] = savings(c, sw, p);

    W           = wfunc_new(c, sw, EV, p);

    Winterp     = griddedInterpolant({p.lgrid, p.tgrid, p.rgrid},  reshape(W, p.nl, p.nt, p.nr), intmeth, 'linear'); 


    % Solve discrete choice problem

    V          = solveh_new(sv, Winterp, p);
   
    % Interpolate V(w, theta)
    
    Vinterp    = griddedInterpolant({p.wgrid, p.tgrid, p.rgrid},  reshape(V, p.nw, p.nt, p.nr), intmeth, 'linear'); 

    
    % Compute expected value and update vbar
    
    Vbar        = zeros(p.na*p.nt*p.nr, 1); 

    for i = 1 : p.ny
        
    Vbar        = Vbar + wy(i)*Vinterp((1 + p.rl)*svbar(:,1) + y(i), svbar(:,2), svbar(:,3)); 
    
    end
    
    
    fprintf('%4i %6.2e \n', [iter, norm(Vbar - Vbarold)/norm(Vbar)]);    

end


for iter = 1 : 5000

    Vbarold     = Vbar;
    
    EV          = griddedInterpolant({p.agrid, p.tgrid, p.rgrid},  reshape(Vbar, p.na, p.nt, p.nr), intmeth, 'linear');
 
    % solve consumption-savings choice
    
    if mod(iter, 50) == 0
    
    c           = solve_golden('wfunc_new', cmin, cmax, sw, EV, p);
    
    end
    
    [~, aprime] = savings(c, sw, p);

    W           = wfunc_new(c, sw, EV, p);

    Winterp     = griddedInterpolant({p.lgrid, p.tgrid, p.rgrid},  reshape(W, p.nl, p.nt, p.nr), intmeth, 'linear'); 


    % Solve discrete choice problem

    V           = solveh_new(sv, Winterp, p);
   
    % Interpolate V(w, theta)
    
    Vinterp     = griddedInterpolant({p.wgrid, p.tgrid, p.rgrid},  reshape(V, p.nw, p.nt, p.nr), intmeth, 'linear'); 

    
    % Compute expected value and update vbar
    
    Vbar        = zeros(p.na*p.nt*p.nr, 1); 

    for i = 1 : p.ny
        
    Vbar        = Vbar + wy(i)*Vinterp((1 + p.rl)*svbar(:,1) + y(i), svbar(:,2), svbar(:,3)); 
    
    end
    
    if mod(iter, 50) == 0
        
    fprintf('%4i %6.2e \n', [iter/50, norm(Vbar - Vbarold)/norm(Vbar)]);    

    if norm(Vbar - Vbarold)/norm(Vbar) < 1e-7, break, end
    
    end
    
end