-
Notifications
You must be signed in to change notification settings - Fork 226
/
transformations.jl
959 lines (777 loc) · 28.8 KB
/
transformations.jl
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
"""
`annualtoquarter(v)`
Convert from annual to quarter frequency... by dividing by 4.
"""
function annualtoquarter(v)
v ./ 4
end
"""
`quartertoannual(v)`
Convert from quarter to annual frequency... by multiplying by 4.
"""
function quartertoannual(v)
4 .* v
end
"""
`quartertoannualpercent(v)`
Convert from quarter to annual frequency in percent... by multiplying by 400.
"""
function quartertoannualpercent(v)
400 .* v
end
"""
`nominal_to_real(col, df; deflator_mnemonic = :GDPDEF)`
Converts nominal to real values using the specified deflator.
## Arguments
- `col`: Symbol indicating which column of `df` to transform
- `df`: DataFrame containining series for proper population measure and `col`
## Keyword arguments
- `deflator_mnemonic`: indicates which deflator to use to calculate real values. Default
value is the FRED GDP Deflator mnemonic.
"""
function nominal_to_real(col::Symbol, df::DataFrame; deflator_mnemonic::Symbol = :GDPDEF)
return df[!, col] ./ df[!, deflator_mnemonic]
end
"""
```
percapita(m, col, df)
percapita(col, df, population_mnemonic)
```
Converts data column `col` of DataFrame `df` to a per-capita value.
The first method checks `hpfilter_population(m)`. If true, then it divides by
the filtered population series. Otherwise it divides by the result of
`parse_population_mnemonic(m)[1]`.
## Arguments
- `col`: `Symbol` indicating which column of data to transform
- `df`: `DataFrame` containining series for proper population measure and `col`
- `population_mnemonic`: a mnemonic found in `df` for some population measure
"""
function percapita(m::AbstractDSGEModel, col::Symbol, df::DataFrame)
if hpfilter_population(m)
population_mnemonic = Nullable(:filtered_population)
else
population_mnemonic = parse_population_mnemonic(m)[1]
if isnull(population_mnemonic)
error("No population mnemonic provided")
end
end
percapita(col, df, get(population_mnemonic))
end
function percapita(col::Symbol, df::DataFrame, population_mnemonic::Symbol)
df[!, col] ./ df[!, population_mnemonic]
end
"""
```
series_lag_n = lag(series, n)
```
Returns a particular data series lagged by n periods
"""
function lag(series::AbstractArray, n::Int)
return vcat(NaN*ones(n), series[1:end-n])
end
"""
```
yt, yf = hpfilter(y, λ)
```
Applies the Hodrick-Prescott filter (\"H-P filter\"). The smoothing parameter `λ` is applied
to the columns of `y`, returning the trend component `yt` and the cyclical component `yf`.
For quarterly data, one can use λ=1600.
Consecutive missing values at the beginning or end of the time series are excluded from the
filtering. If there are missing values within the series, the filtered values are all missing.
See also:
```
Hodrick, Robert; Prescott, Edward C. (1997). \"Postwar U.S. Business Cycles: An Empirical
Investigation\". Journal of Money, Credit, and Banking 29 (1): 1–16.
```
"""
function hpfilter(y::AbstractVector, λ::Real)
# Indices of consecutive missing elements at beginning
i = 1
j = length(y)
while ismissing(y[i]) || isnan(y[i])
i = i+1
end
while ismissing(y[j]) || isnan(y[j])
j = j-1
end
# Filter and adjust for missings
yt_, yf_ = hpfilter_(y[i:j], λ)
yt = [fill(missing, i-1); yt_; fill(missing, length(y)-j)]
yf = [fill(missing, i-1); yf_; fill(missing, length(y)-j)]
return yt, yf
end
function hpfilter_(y::AbstractVector, λ::Real)
n = length(y)
a = spzeros(n,n)
for i = 3:n-2
a[i,i] = 6λ+1
a[i,i+1] = -4λ
a[i,i+2] = λ
a[i,i-2] = λ
a[i,i-1] = -4λ
end
a[2,2] = 1+5λ
a[2,3] = -4λ
a[2,4] = λ
a[2,1] = -2λ
a[1,1] = 1+λ
a[1,2] = -2λ
a[1,3] = λ
a[n-1,n-1] = 1+5λ
a[n-1,n-2] = -4λ
a[n-1,n-3] = λ
a[n-1,n] = -2λ
a[n,n] = 1+λ
a[n,n-1] = -2λ
a[n,n-2] = λ
yt = a\y
yf = y-yt
return yt, yf
end
"""
```
difflog(x::AbstractVector)
```
"""
function difflog(x::AbstractVector)
[missing; log.(x[2:end]) - log.(x[1:end-1])]
end
"""
```
difflog(x::AbstractArray{AbstractFloat})
```
"""
function difflog(x::AbstractArray)
return difflog(convert(Vector, x))
end
"""
```
oneqtrpctchange(y)
```
Calculates the quarter-to-quarter percentage change of a series.
"""
function oneqtrpctchange(y::AbstractVector)
100 .* difflog(y)
end
## REVERSE TRANSFORMS
"""
```
loggrowthtopct(y)
```
Transform from annualized quarter-over-quarter log growth rates to annualized
quarter-over-quarter percent change.
### Note
This should only be used in Model 510, which has the core PCE inflation
observable in annualized log growth rates.
"""
function loggrowthtopct(y::AbstractArray)
100. .* (exp.(y ./ 100.) .- 1.)
end
"""
```
loggrowthtopct_percapita(y, pop_growth)
```
Transform from annualized quarter-over-quarter log per-capita growth rates to
annualized quarter-over-quarter aggregate percent change.
### Note
This should only be used in Model 510, which has the output growth observable in
annualized log per-capita growth rates.
### Inputs
- `y`: the data we wish to transform to annualized percent change from
annualized log growth rates. `y` is either a vector of length `nperiods` or an
`ndraws x `nperiods` matrix.
- `pop_growth::Vector`: the length `nperiods` vector of log population growth
rates.
"""
function loggrowthtopct_percapita(y::AbstractArray, pop_growth::AbstractVector)
# `y` is either a vector of length `nperiods` or an
# `ndraws` x `nperiods` matrix
if ndims(y) == 1
nperiods = length(y)
else
nperiods = size(y, 2)
# Transpose `pop_growth` to a 1 x `nperiods` row vector so it can be
# broadcasted to match the dimensions of `y`
pop_growth = pop_growth'
end
@assert length(pop_growth) == nperiods "Length of pop_growth ($(length(pop_growth))) must equal number of periods of y ($nperiods)"
100. .* ((exp.(y ./ 100.) .* exp.(pop_growth).^4) .- 1.)
end
"""
```
loggrowthtopct_annualized(y)
```
Transform from log growth rates to annualized quarter-over-quarter percent change.
"""
function loggrowthtopct_annualized(y::AbstractArray)
100. .* (exp.(y ./ 100.).^4 .- 1.)
end
"""
```
loggrowthtopct_annualized_percapita(y, pop_growth)
```
Transform from log per-capita growth rates to annualized aggregate (not
per-capita) quarter-over-quarter percent change.
### Note
This should only be used for output, consumption, investment
and GDP deflator (inflation).
### Inputs
- `y`: the data we wish to transform to annualized percent change from
quarter-over-quarter log growth rates. `y` is either a vector of length
`nperiods` or an `ndraws x `nperiods` matrix.
- `pop_growth::Vector`: the length `nperiods` vector of log population growth
rates.
"""
function loggrowthtopct_annualized_percapita(y::AbstractArray, pop_growth::AbstractVector)
# `y` is either a vector of length `nperiods` or an
# `ndraws` x `nperiods` matrix
if ndims(y) == 1
nperiods = length(y)
else
nperiods = size(y, 2)
# Transpose `pop_growth` to a 1 x `nperiods` row vector so it can be
# broadcasted to match the dimensions of `y`
pop_growth = pop_growth'
end
@assert length(pop_growth) == nperiods "Length of pop_growth ($(length(pop_growth))) must equal number of periods of y ($nperiods)"
100. * (exp.(y ./ 100. .+ pop_growth).^4 .- 1.)
end
"""
```
logleveltopct_annualized(y, y0 = NaN)
```
Transform from log levels to annualized quarter-over-quarter percent change.
### Inputs
- `y`: the data we wish to transform to annualized quarter-over-quarter percent
change from log levels. `y` is either a vector of length `nperiods` or an
`ndraws x `nperiods` matrix.
- `y0`: the last data point in the history (of state or observable)
corresponding to the `y` variable. This is required to compute a percent
change for the first period.
"""
function logleveltopct_annualized(y::AbstractArray, y0::Real = NaN)
# `y_t1` is an array of the same size as `y`, representing the previous
# period observations for each draw
if ndims(y) == 1
y_t1 = vcat([y0], y[1:end-1])
else
ndraws = size(y, 1)
y0s = fill(y0, ndraws, 1)
y_t1 = hcat(y0s, y[:, 1:end-1])
end
# Subtract log levels to get log growth rates, then take the exponential to
# get growth rates
100. * (exp.(y./100. - y_t1./100.).^4 .- 1.)
end
"""
```
logleveltopct_annualized_percapita(y, pop_growth, y0 = NaN)
```
Transform from per-capita log levels to annualized aggregate (not per-capita)
quarter-over-quarter percent change.
### Note
This is usually applied to labor supply (hours worked per hour), and
probably shouldn't be used for any other observables.
### Inputs
- `y`: the data we wish to transform to annualized aggregate
quarter-over-quarter percent change from per-capita log levels. `y` is either
a vector of length `nperiods` or an `ndraws x `nperiods` matrix.
- `pop_growth::Vector`: the length `nperiods` vector of log population growth
rates.
- `y0`: The last data point in the history (of state or observable)
corresponding to the `y` variable. This is required to compute a percent
change for the first period.
"""
function logleveltopct_annualized_percapita(y::AbstractArray, pop_growth::AbstractVector, y0::Real = NaN)
# `y_t1` is an array of the same size as `y`, representing the previous
# period observations for each draw
if ndims(y) == 1
nperiods = length(y)
y_t1 = vcat([y0], y[1:end-1])
else
(ndraws, nperiods) = size(y)
y0s = fill(y0, ndraws, 1)
y_t1 = hcat(y0s, y[:, 1:end-1])
# Transpose `pop_growth` to a 1 x `nperiods` row vector so it can be
# broadcasted to match the dimensions of `y`
pop_growth = pop_growth'
end
@assert length(pop_growth) == nperiods "Length of pop_growth ($(length(pop_growth))) must equal number of periods of y ($nperiods)"
# Subtract log levels to get log growth rates, then take the exponential to
# get growth rates
100. * (exp.(y./100. .- y_t1./100. .+ pop_growth).^4 .- 1.)
end
"""
```
get_nopop_transform(transform::Function)
```
Returns the corresponding transformation which doesn't add back population
growth. Used for shock decompositions, deterministic trends, and IRFs, which are
given in deviations.
"""
function get_nopop_transform(transform::Function)
transform4q = if transform == loggrowthtopct_annualized_percapita
loggrowthtopct_annualized
elseif transform == logleveltopct_annualized_percapita
logleveltopct_annualized
else
transform
end
end
"""
```
get_irf_transform(transform::Function)
```
Returns the IRF-specific transformation, which doesn't add back population
growth (since IRFs are given in deviations).
"""
function get_irf_transform(transform::Function)
transform4q = if transform == loggrowthtopct_annualized_percapita
loggrowthtopct_annualized
elseif transform == logleveltopct_annualized_percapita
logleveltopct_annualized
else
transform
end
end
"""
```
get_transform4q(transform::Function)
```
Returns the 4-quarter transformation associated with the annualizing transformation.
"""
function get_transform4q(transform::Function)
if transform == loggrowthtopct_annualized_percapita
loggrowthtopct_4q_percapita
elseif transform == loggrowthtopct_annualized
loggrowthtopct_4q
elseif transform == logleveltopct_annualized_percapita
logleveltopct_4q_percapita
elseif transform == logleveltopct_annualized
logleveltopct_4q
elseif transform == quartertoannual
quartertoannual
elseif transform == identity
identity
else
error("4q equivalent not implemented for $transform")
end
end
"""
```
loggrowthtopct_4q(y, data = fill(NaN, 3))
```
Transform from log growth rates to 4-quarter percent change.
### Inputs
- `y`: the data we wish to transform to aggregate 4-quarter percent change from
log per-capita growth rates. `y` is either a vector of length `nperiods` or an
`ndraws x `nperiods` matrix.
- `data`: if `y = [y_t, y_{t+1}, ..., y_{t+nperiods-1}]`, then
`data = [y_{t-3}, y_{t-2}, y_{t-1}]`. This is necessary to compute
4-quarter percent changes for the first three periods.
"""
function loggrowthtopct_4q(y::AbstractArray, data::AbstractVector = fill(NaN, 3))
@assert length(data) == 3 "Length of data ($(length(data))) must be 3"
# Prepend previous three periods to `y`
y = prepend_data(y, data)
# `y` is either a vector of length `nperiods+3` or an
# `ndraws` x `nperiods+3` matrix
if ndims(y) == 1
y_4q = y[1:end-3] + y[2:end-2] + y[3:end-1] + y[4:end]
else
y_4q = y[:, 1:end-3] + y[:, 2:end-2] + y[:, 3:end-1] + y[:, 4:end]
end
100. * (exp.(y_4q ./ 100.) .- 1.)
end
"""
```
loggrowthtopct_4q_percapita(y, pop_growth, data = fill(NaN, 3))
```
Transform from log per-capita growth rates to aggregate 4-quarter percent
change.
### Note
This should only be used for output, consumption, investment, and GDP deflator
(inflation).
### Inputs
- `y`: the data we wish to transform to aggregate 4-quarter percent change from
log per-capita growth rates. `y` is either a vector of length `nperiods` or an
`ndraws x `nperiods` matrix.
- `pop_growth::Vector`: the length `nperiods` vector of log population growth
rates.
- `data`: if `y = [y_t, y_{t+1}, ..., y_{t+nperiods-1}]`, then
`data = [y_{t-3}, y_{t-2}, y_{t-1}]`. This is necessary to compute
4-quarter percent changes for the first three periods.
"""
function loggrowthtopct_4q_percapita(y::AbstractArray, pop_growth::AbstractVector,
data::Vector = fill(NaN, 3))
@assert length(data) == 3 "Length of data ($(length(data))) must be 3"
# Four-quarter population growth
pop_growth_4q = pop_growth[1:end-3] + pop_growth[2:end-2] + pop_growth[3:end-1] + pop_growth[4:end]
# Prepend previous three periods to `y`
y = prepend_data(y, data)
# `y` is either a vector of length `nperiods+3` or an
# `ndraws` x `nperiods+3` matrix
if ndims(y) == 1
y_4q = y[1:end-3] + y[2:end-2] + y[3:end-1] + y[4:end]
nperiods = length(y_4q)
else
y_4q = y[:, 1:end-3] + y[:, 2:end-2] + y[:, 3:end-1] + y[:, 4:end]
nperiods = size(y_4q, 2)
# Transpose `pop_growth` to a 1 x `nperiods` row vector so it can be
# broadcasted to match the dimensions of `y_4q`
pop_growth_4q = pop_growth_4q'
end
@assert length(pop_growth_4q) == nperiods "Length of pop_growth ($(length(pop_growth_4q))) must equal number of periods of y ($nperiods)"
100. .* (exp.(y_4q ./ 100. .+ pop_growth_4q) .- 1.)
end
"""
```
logleveltopct_4q(y, data = fill(NaN, 4))
```
Transform from log levels to 4-quarter percent change.
### Inputs
- `y`: the data we wish to transform to 4-quarter percent change from log
levels. `y` is either a vector of length `nperiods` or an `ndraws x `nperiods`
matrix.
- `data`: if `y = [y_t, y_{t+1}, ..., y_{t+nperiods-1}]`, then
`data = [y_{t-4}, y_{t-3}, y_{t-2}, y_{t-1}]`. This is necessary to compute
4-quarter percent changes for the first three periods.
"""
function logleveltopct_4q(y::AbstractArray, data::AbstractVector = fill(NaN, 4))
@assert length(data) == 4 "Length of data ($(length(data))) must be 4"
# `y_t4` is an array of the same size as `y`, representing the t-4
# period observations for each t
y_t4 = if ndims(y) == 1
nperiods = length(y)
prepend_data(y[1:nperiods-4], data)
else
nperiods = size(y, 2)
prepend_data(y[:, 1:nperiods-4], data)
end
y_4q = y - y_t4
# Subtract log levels to get log growth rates, then exponentiate to get
# growth rates
100. * (exp.(y_4q./100.) .- 1.)
end
"""
```
logleveltopct_4q_percapita(y, pop_growth, data = fill(NaN, 4))
```
Transform from per-capita log levels to 4-quarter aggregate percent change.
### Note
This is usually applied to labor supply (hours worked), and probably shouldn't
be used for any other observables.
### Inputs
- `y`: the data we wish to transform to 4-quarter aggregate percent change from
per-capita log levels. `y` is either a vector of length `nperiods` or an
`ndraws x `nperiods` matrix.
- `pop_growth::Vector`: the length `nperiods` vector of log population growth
rates.
- `data`: if `y = [y_t, y_{t+1}, ..., y_{t+nperiods-1}]`, then
`data = [y_{t-4}, y_{t-3}, y_{t-2}, y_{t-1}]`. This is necessary to compute
4-quarter percent changes for the first three periods.
"""
function logleveltopct_4q_percapita(y::AbstractArray, pop_growth::AbstractVector,
data::AbstractVector = fill(NaN, 4))
@assert length(data) == 4 "Length of data ($(length(data))) must be 4"
# Four-quarter population growth
pop_growth_4q = pop_growth[1:end-3] + pop_growth[2:end-2] + pop_growth[3:end-1] + pop_growth[4:end]
# `y_t4` is an array of the same size as `y`, representing the t-4
# period observations for each t
if ndims(y) == 1
nperiods = length(y)
y_t4 = prepend_data(y[1:nperiods-4], data)
else
# Transpose `pop_growth` to a 1 x `nperiods` row vector so it can be
# broadcasted to match the dimensions of `y`
pop_growth_4q = pop_growth_4q'
nperiods = size(y, 2)
y_t4 = prepend_data(y[:, 1:nperiods-4], data)
end
@assert length(pop_growth_4q) == nperiods "Length of pop_growth ($(length(pop_growth_4q))) must equal number of periods of y ($nperiods)"
y_4q = y - y_t4
# Subtract log levels to get log growth rates, then exponentiate to get growth rates
100. * (exp.(y_4q ./ 100. .+ pop_growth_4q) .- 1.)
end
"""
```
prepend_data(y, data)
```
Prepends data necessary for running 4q transformations.
### Inputs:
- `y`: `ndraws x t` array representing a timeseries for variable `y`
- `data`: vector representing a timeseries to prepend to `y`
"""
function prepend_data(y::AbstractArray, data::AbstractVector)
if ndims(y) == 1
y_extended = vcat(data, y)
else
ndraws = size(y, 1)
datas = repeat(data', outer=(ndraws, 1))
y_extended = hcat(datas, y)
end
return y_extended
end
"""
```
get_scenario_transform(transform::Function)
```
Given a transformation used for usual forecasting, return the transformation
used for *scenarios*, which are forecasted in deviations from baseline.
The 1Q deviation from baseline should really be calculated by 1Q transforming
the forecasts (in levels) under the baseline (call this `y_b`) and alternative
scenario (`y_s`), then subtracting baseline from alternative scenario (since
most of our 1Q transformations are nonlinear). Let `y_d = y_s - y_b`. Then, for
example, the most correct `loggrowthtopct_annualized` transformation is:
```
y_b_1q = 100*(exp(y_b/100)^4 - 1)
y_s_1q = 100*(exp(y_s/100)^4 - 1)
y_d_1q = y_b_1q - y_s_1q
```
Instead, we approximate this by transforming the deviation directly:
```
y_d_1q ≈ 4*(y_b - y_s)
```
"""
function get_scenario_transform(transform::Function)
if transform in [loggrowthtopct_annualized_percapita, loggrowthtopct_annualized,
quartertoannual]
quartertoannual
elseif transform in [logleveltopct_annualized_percapita, logleveltopct_annualized]
logleveltopct_annualized_approx
elseif transform == identity
identity
else
error("Scenario equivalent not implemented for $transform")
end
end
function get_scenario_transform4q(transform::Function)
if transform in [loggrowthtopct_annualized_percapita, loggrowthtopct_annualized]
loggrowthtopct_4q_approx
elseif transform in [logleveltopct_annualized_percapita, logleveltopct_annualized]
logleveltopct_4q_approx
elseif transform == quartertoannual
quartertoannual
elseif transform == identity
identity
else
error("Scenario 4q equivalent not implemented for $transform")
end
end
"""
```
logleveltopct_annualized_approx(y, y0 = NaN)
```
Transform from log levels to *approximate* annualized quarter-over-quarter
percent change.
**This method should only be used to transform scenarios forecasts, which are in
deviations from baseline.**
### Inputs
- `y`: the data we wish to transform to annualized quarter-over-quarter percent
change from log levels. `y` is either a vector of length `nperiods` or an
`ndraws x `nperiods` matrix.
- `y0`: the last data point in the history (of state or observable)
corresponding to the `y` variable. This is required to compute a percent
change for the first period.
"""
function logleveltopct_annualized_approx(y::AbstractArray, y0::Real = NaN)
# `y_t1` is an array of the same size as `y`, representing the previous
# period observations for each draw
if ndims(y) == 1
y_t1 = vcat([y0], y[1:end-1])
else
ndraws = size(y, 1)
y0s = fill(y0, ndraws, 1)
y_t1 = hcat(y0s, y[:, 1:end-1])
end
# Subtract log levels to get log growth rates, then multiply by 4 to
# approximate annualizing
4. .* (y - y_t1)
end
"""
```
loggrowthtopct_4q_approx(y, data = fill(NaN, 3))
```
Transform from log growth rates to *approximate* 4-quarter percent change.
**This method should only be used to transform scenarios forecasts, which are in
deviations from baseline.**
### Inputs
- `y`: the data we wish to transform to aggregate 4-quarter percent change from
log per-capita growth rates. `y` is either a vector of length `nperiods` or an
`ndraws x `nperiods` matrix.
- `data`: if `y = [y_t, y_{t+1}, ..., y_{t+nperiods-1}]`, then
`data = [y_{t-3}, y_{t-2}, y_{t-1}]`. This is necessary to compute
4-quarter percent changes for the first three periods.
"""
function loggrowthtopct_4q_approx(y::AbstractArray, data::AbstractVector = fill(NaN, 3))
@assert length(data) == 3 "Length of data ($(length(data))) must be 3"
# Prepend previous three periods to `y`
y = prepend_data(y, data)
# `y` is either a vector of length `nperiods+3` or an
# `ndraws` x `nperiods+3` matrix
if ndims(y) == 1
y[1:end-3] + y[2:end-2] + y[3:end-1] + y[4:end]
else
y[:, 1:end-3] + y[:, 2:end-2] + y[:, 3:end-1] + y[:, 4:end]
end
end
"""
```
logleveltopct_4q_approx(y, data = fill(NaN, 4))
```
Transform from log levels to *approximate* 4-quarter percent change.
**This method should only be used to transform scenarios forecasts, which are in
deviations from baseline.**
### Inputs
- `y`: the data we wish to transform to 4-quarter percent change from log
levels. `y` is either a vector of length `nperiods` or an `ndraws x `nperiods`
matrix.
- `data`: if `y = [y_t, y_{t+1}, ..., y_{t+nperiods-1}]`, then
`data = [y_{t-4}, y_{t-3}, y_{t-2}, y_{t-1}]`. This is necessary to compute
4-quarter percent changes for the first three periods.
"""
function logleveltopct_4q_approx(y::AbstractArray, data::AbstractVector = fill(NaN, 4))
@assert length(data) == 4 "Length of data ($(length(data))) must be 4"
# `y_t4` is an array of the same size as `y`, representing the t-4
# period observations for each t
y_t4 = if ndims(y) == 1
nperiods = length(y)
prepend_data(y[1:nperiods-4], data)
else
nperiods = size(y, 2)
prepend_data(y[:, 1:nperiods-4], data)
end
y - y_t4
end
# # Accumulation transforms
# """
# ```
# get_transformlvl(transform::Function; nominal_transform::Bool = false)
# ```
# Returns the accumulation transformation associated with the annualizing transformation.
# """
# function get_transformlvl(transform::Function; nominal_transform::Bool = false)
# if nominal_transform
# if transform == loggrowthtopct_annualized_percapita
# loggrowthto_nominallvl_percapita
# elseif transform == loggrowthtopct_annualized
# loggrowthto_nominallvl
# elseif transform == logleveltopct_annualized_percapita
# loglevelto_nominallvl_percapita
# elseif transform == logleveltopct_annualized
# loglevelto_nominallvl
# elseif transform == quartertoannual
# quartertoannual
# elseif transform == identity
# identity
# else
# error("lvl equivalent not implemented for $transform")
# end
# else
# if transform == loggrowthtopct_annualized_percapita
# loggrowthtolvl_percapita
# elseif transform == loggrowthtopct_annualized
# loggrowthtolvl
# elseif transform == logleveltopct_annualized_percapita
# logleveltolvl_percapita
# elseif transform == logleveltopct_annualized
# logleveltolvl
# elseif transform == quartertoannual
# quartertoannual
# elseif transform == identity
# identity
# else
# error("lvl equivalent not implemented for $transform")
# end
# end
# end
# function loggrowthtolvl(y::AbstractArray, y0::S) where {S <: Real}
# y0 .* cumprod(exp.(y ./ 100.))
# end
# function loggrowthtolvl_percapita(y::AbstractArray, y0::S, pop_growth::AbstractVector) where {S <: Real}
# # `y` is either a vector of length `nperiods` or an
# # `ndraws` x `nperiods` matrix
# if ndims(y) == 1
# nperiods = length(y)
# else
# nperiods = size(y, 2)
# # Transpose `pop_growth` to a 1 x `nperiods` row vector so it can be
# # broadcasted to match the dimensions of `y`
# pop_growth = pop_growth'
# end
# @assert length(pop_growth) == nperiods "Length of pop_growth ($(length(pop_growth))) must equal number of periods of y ($nperiods)"
# y0 .* cumprod(exp.(y ./ 100. .+ pop_growth))
# end
# function logleveltolvl(y::AbstractArray, y0::S) where {S <: Real}
# y0 .+ exp.(y ./ 100.)
# end
# function logleveltolvl_percapita(y::AbstractArray, y0::S, pop_growth::AbstractVector) where {S <: Real}
# # `y_t1` is an array of the same size as `y`, representing the previous
# # period observations for each draw
# if ndims(y) == 1
# nperiods = length(y)
# y_t1 = vcat([y0], y[1:end-1])
# else
# (ndraws, nperiods) = size(y)
# y0s = fill(y0, ndraws, 1)
# y_t1 = hcat(y0s, y[:, 1:end-1])
# # Transpose `pop_growth` to a 1 x `nperiods` row vector so it can be
# # broadcasted to match the dimensions of `y`
# pop_growth = pop_growth'
# end
# @assert length(pop_growth) == nperiods "Length of pop_growth ($(length(pop_growth))) must equal number of periods of y ($nperiods)"
# # Subtract log levels to get log growth rates, add pop growth rates,
# # exponentiate to get growth rates, and cumprod to accumulate
# y0 .* cumprod(exp.(y ./ 100. .- y_t1 ./ 100. .+ pop_growth))
# end
# function loggrowthto_nominallvl(y::AbstractArray, y0::S, π::AbstractArray) where {S <: Real}
# y0 .* cumprod(exp.(y ./ 100.) + π)
# end
# function loggrowthto_nominallvl_percapita(y::AbstractArray, y0::S, π::AbstractArray, pop_growth::AbstractVector) where {S <: Real}
# # `y` is either a vector of length `nperiods` or an
# # `ndraws` x `nperiods` matrix
# if ndims(y) == 1
# nperiods = length(y)
# else
# nperiods = size(y, 2)
# # Transpose `pop_growth` to a 1 x `nperiods` row vector so it can be
# # broadcasted to match the dimensions of `y`
# pop_growth = pop_growth'
# end
# @assert length(pop_growth) == nperiods "Length of pop_growth ($(length(pop_growth))) must equal number of periods of y ($nperiods)"
# y0 .* cumprod(exp.(y ./ 100. .+ pop_growth) + π)
# end
# function loglevelto_nominallvl(y::AbstractArray, y0::S, π::AbstractArray) where {S <: Real}
# # `y_t1` is an array of the same size as `y`, representing the previous
# # period observations for each draw
# if ndims(y) == 1
# nperiods = length(y)
# y_t1 = vcat([y0], y[1:end-1])
# else
# (ndraws, nperiods) = size(y)
# y0s = fill(y0, ndraws, 1)
# y_t1 = hcat(y0s, y[:, 1:end-1])
# end
# # Subtract log levels to get log growth rates, add inflation,
# # exponentiate to get growth rates, and cumprod to accumulate
# y0 .* cumprod(exp.(y ./ 100. .- y_t1 ./ 100.) + π)
# end
# function loglevelto_nominallvl_percapita(y::AbstractArray, y0::S, π::AbstractArray, pop_growth::AbstractVector) where {S <: Real}
# # `y_t1` is an array of the same size as `y`, representing the previous
# # period observations for each draw
# if ndims(y) == 1
# nperiods = length(y)
# y_t1 = vcat([y0], y[1:end-1])
# else
# (ndraws, nperiods) = size(y)
# y0s = fill(y0, ndraws, 1)
# y_t1 = hcat(y0s, y[:, 1:end-1])
# # Transpose `pop_growth` to a 1 x `nperiods` row vector so it can be
# # broadcasted to match the dimensions of `y`
# pop_growth = pop_growth'
# end
# @assert length(pop_growth) == nperiods "Length of pop_growth ($(length(pop_growth))) must equal number of periods of y ($nperiods)"
# # Subtract log levels to get log growth rates, add pop growth rates and inflation,
# # exponentiate to get growth rates, and cumprod to accumulate
# y0 .* cumprod(exp.(y ./ 100. .- y_t1 ./ 100. .+ pop_growth) + π)
# end