-
Notifications
You must be signed in to change notification settings - Fork 0
/
forecast.js
243 lines (221 loc) · 7.69 KB
/
forecast.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
Forecast.JS
Modifying the original rate comparison with some forecast data
and params that you can fit for the next few years.
*/
// width and height are set by the render
var margin = {top: 20, right: 40, bottom: 40, left: 50};
var parseTime = d3.timeParse("%m/%d/%Y");
var FORECAST_START = parseTime('01/01/2018');
function makeForecast(data, options, verbose=false) {
//* use projections to forecast rates
// helper functions
function calcGap(gdp, potential_gdp) {
return (gdp - potential_gdp) / potential_gdp * 100;
}
function calcTaylor(gdp, inf, gap) {
// inputs are floats (gap is negative)
return gdp + inf + 0.5*(gdp-inf) + 0.5*gap
}
var forecastData = data.filter(function(d) { return d.DATE >= FORECAST_START });
forecastData.forEach(function(d, i) {
// calc the forecast with some FOMC projection data
if (i < 3) {
var gdp = d.GDP_PROJ,
gdp_pot = d.GDPPOT_PROJ,
gdppc1_pc1 = d.GDPPC1_PROJ,
inf = d.INFLATION_PROJ,
gap = calcGap(gdp, gdp_pot);
}
// otherwise, project it out using the previous year with growth
else {
var gdp = forecastData[i-1].GDP_PROJ * (1 + (+options.gwt/100)),
gdp_pot = d.GDPPOT_PROJ, // we have projections for this
gdppc1_pc1 = forecastData[i-1].GDPPC1_PROJ* (1+options.gwt/100),
inf = +options.inf,
gap = calcGap(gdp, gdp_pot);
// save the previous year for the next calculation
forecastData[i].GDP_PROJ = Math.round(gdp, 4);
forecastData[i].GDPPC1_PROJ = Math.round(gdppc1_pc1, 4);
}
var newProjection = calcTaylor(gdppc1_pc1, inf, gap);
d.PROJECTION = newProjection;
if (verbose){
console.log("forecast data:", gdp, gdppc1_pc1, inf,gap)
console.log("projection "+i, newProjection);
}
});
return data
}
(function plotForecast() {
var svg2 = d3.select('#forecast').append('svg')
.attrs({
width: svgWidth,
height: svgHeight
})
width = +svg2.attr("width") - margin.left - margin.right
height = +svg2.attr("height") - margin.top - margin.bottom
var forecast = svg2.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
var x = d3.scaleTime()
.rangeRound([0, width]);
var y = d3.scaleLinear()
.rangeRound([height, 0]);
/*
Data is from 2007 and has new fields:
FEDFUNDS_PROJ: fed funds
GDPPC1_PROJ: real_gdp growth projections
INFLATION_PROJ
GDPPOT_PROJ: potential gdp projections
GDP_PROJ: projected gdp in dollars
*/
var datafile = './data/data-forecast.csv';
// load and plot the first chart
d3.csv(datafile, function(d) {
// wrangle
d.DATE = parseTime(d.DATE);
d.FEDFUNDS = +d.FEDFUNDS;
d.TAYLORRATE = +d.TAYLORRATE;
// projection data
d.FEDFUNDS_PROJ = +d.FEDFUNDS_PROJ;
d.GDPPC1_PROJ = +d.GDPPC1_PROJ;
d.INFLATION_PROJ = +d.INFLATION_PROJ;
d.GDPPOT_PROJ = +d.GDPPOT_PROJ;
d.GDP_PROJ = +d.GDP_PROJ;
d.PROJECTION = 0; // placeholder for forecast
return d;
}, function(error, data) {
if (error) throw error;
x.domain(d3.extent(data, function(d) { return d.DATE; }));
y.domain(d3.extent(data, function(d) { return d.TAYLORRATE; }));
// series data
// only defined at certain dates
var fed_funds = d3.line()
.x(function(d) { return x(d.DATE); })
.y(function(d) { return y(d.FEDFUNDS); })
.defined(function(d) { return d.DATE < FORECAST_START ; })
.curve(d3.curveCatmullRom.alpha(0.5));
var taylor_rate = d3.line()
.x(function(d) { return x(d.DATE); })
.y(function(d) { return y(d.TAYLORRATE); })
.defined(function(d) {
return d.DATE < FORECAST_START
& d.DATE < parseTime('07/01/2017'); })
.curve(d3.curveCatmullRom.alpha(0.5));
var ff_proj = d3.line()
.x(function(d) { return x(d.DATE); })
.y(function(d) { return y(d.FEDFUNDS_PROJ); })
.defined(function(d) {
return d.DATE >= FORECAST_START
& d.DATE <= parseTime('01/01/2020'); })
.curve(d3.curveCatmullRom.alpha(0.5));
var taylor_rate_proj = d3.line()
.x(function(d) { return x(d.DATE); })
.y(function(d) { return y(d.PROJECTION); })
.defined(function(d) { return d.DATE >= FORECAST_START ; })
.curve(d3.curveCatmullRom.alpha(0.5));
forecast.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
.attr('class', 'axis x-axis')
.select(".domain")
.remove();
forecast.append("g")
.call(d3.axisLeft(y))
.attr('class', 'axis y-axis')
.append("text")
.attr("fill", "#000")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("Interest Rate (%)");
var ff = forecast.append("path")
.attr("id", "fed-funds")
.datum(data)
.attr("fill", "none")
.attr("stroke", "#888888")
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("stroke-width", 1)
.attr("d", fed_funds);
var ff_proj = forecast.append("path")
.attr("id", "fed-funds-proj")
.datum(data)
.attr("fill", "none")
.attr("stroke", "red")
.attr("stroke-linejoin", "round")
.attr("stroke-dasharray", ("3, 3"))
.attr("stroke-linecap", "round")
.attr("stroke-width", 1)
.attr("d", ff_proj);
var tr = forecast.append("path")
.attr("id", "taylor-rate")
.datum(data)
.attr("fill", "none")
.attr("stroke", "#2F74FF")
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("stroke-width", 1)
.attr("d", taylor_rate);
var proj = forecast.append("path")
.attr("id", "projection")
.datum(data)
.attr("fill", "none")
.attr("stroke", "#AB3C93")
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("stroke-width", 3)
.attr("d", taylor_rate_proj);
// end series
// shade the forecast region
forecast.append('rect')
.attr('class', 'bar')
.transition()
.delay(100)
.attr('x', function(d) { return x(FORECAST_START); })
.attr('y', 1)
.attr('width', function() { return width - x(FORECAST_START); })
.attr('height', height)
.attr('rx', 2)
.attr('ry', 2);
// plot legend
var legendSize = 200;
var ordinal = d3.scaleOrdinal()
.domain(["Fed Funds", "FOMC Projection", "Taylor Rate", "Your Projection"])
.range(["black", "red", "#2F74FF", "#AB3C93"]);
var legend = forecast.append("g")
.attr("class", "legend")
.attr("transform", "translate("+(width - legendSize) +","+ 2*margin.top +")");
var legendOrdinal = d3.legendColor()
.shape("path", d3.symbol().type(d3.symbolCircle).size(legendSize)())
.shapePadding(5)
.cellFilter(function(d){ return d.label !== "e" })
.scale(ordinal);
legend.call(legendOrdinal);
// ** update forecast
function updateData(data) {
/*
Take some existing projections and allow the user to forecast data
options are the value of user input
options.inflation
options.growth
*/
// get the options and coerce to number
var options = {};
options["inf"] = +d3.select("#inflation").node().value;
options["gwt"] = $("input[name=growth]:checked").val()
// get the data
data = makeForecast(data, options);
// select and update the projection
var update = forecast.transition()
update.select('#projection')
.duration(400)
.attr('d', function(d){return taylor_rate_proj(d)})
}
// init forecast and on input, update the line
updateData(data)
var updater = $('input').on('change', function() {
updateData(data)});
});
})();