-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathback-testing.ado
More file actions
85 lines (78 loc) · 2.45 KB
/
Copy pathback-testing.ado
File metadata and controls
85 lines (78 loc) · 2.45 KB
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
//********************************************
// Name : John Tsang
// Purpose: Implement back-testing for forecasting errors
// File : back_testing.ado
// Date : 2nd May 2017
**********************************************..
capture program drop back_testing
program define back_testing, rclass
syntax varlist(ts min=2 max=2) [if] [in] [, num_decimals(integer 4)]
// Check if option is valid
if (`num_decimals' < 0 | `num_decimals' > 8) {
display as error "Invalid Number of decimals (Valid number of decimals: 0 to 8)"
exit
}
tempname touse
mark `touse' `if' `in'
qui tsset
local time_var r(timevar)
tokenize `varlist'
local actual_var `1'
local forecast_var `2'
local RMSE = 0
local MAPE = 0
local MAE = 0
display as text ""
display as result "Back-Testing: `actual_var' (Actual) Vs. `forecast_var' (Forecast)"
display as text ""
preserve
qui keep if `touse'
local is_MAPE = 1
local sample_size = _N
forvalues i = 1/`sample_size' {
if (`actual_var'[`i'] == 0) {
local is_MAPE = 0
}
if (`actual_var'[`i'] == .) {
display as error "Observation `i' of `actual_var' is missing"
exit
}
if (`forecast_var'[`i'] == .) {
display as error "Observation `i' of `forecast_var' is missing"
exit
}
local deviation_ = (`actual_var'[`i'] - `forecast_var'[`i'])
local RMSE = `RMSE' + (`deviation_')^2
if (`is_MAPE' == 1) {
local MAPE = `MAPE' + abs(`deviation_')/`actual_var'[`i']
}
local MAE = `MAE' + abs(`deviation_')
}
local RMSE = sqrt(`RMSE'/`sample_size')
if (`is_MAPE' == 1) {
local MAPE = `MAPE'/`sample_size'
}
local MAE = `MAE'/`sample_size'
restore
local a = 4
display as text "{hline 10}{c TT}{hline 20}"
display as text " RMSE {c |}", _continue
display as result %9.`num_decimals'f `RMSE'
display as text "{hline 10}{c +}{hline 20}"
display as text " MAPE {c |}", _continue
if (`is_MAPE' == 1) {
display as result %9.`num_decimals'f `MAPE'
}
else {
display as result "MAPE is not available (An Actual observation is 0)"
}
display as text "{hline 10}{c +}{hline 20}"
display as text " MAE {c |}", _continue
display as result %9.`num_decimals'f `MAE'
display as text "{hline 10}{c BT}{hline 20}"
return scalar RMSE = `RMSE'
return scalar MAE = `MAE'
if (`is_MAPE' == 1) {
return scalar MAPE = `MAPE'
}
end