-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget_errmetrics.m
88 lines (79 loc) · 2.31 KB
/
get_errmetrics.m
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
function errout = get_errmetrics(ypred,ytrue,type,NV)
arguments
ypred double {mustBeReal}
ytrue double {mustBeReal} %or measured
type char {mustBeMember(type,{'e','ae','mae','se','rmse','all'})} = 'all'
NV.dispQ(1,1) logical = false
end
% GET_ERRMETRICS get various error metrics for measured data relative to
% true data, and specify types based on the lowercase symbol. e.g. 'rmse',
% or 'all' to output a struct of error metrics. e = predicted - measured
%--------------------------------------------------------------------------
% Inputs:
% predicted - scalar, vector, or matrix of predicted values
%
% measured - scalar, vector, or matrix of measured (i.e. true or lookup)
% values, matching size of predicted
%
% type - type of error metric to output
%
% Outputs:
% errout - the error metric (or a structure of error metrics) specified by
% type, where e, ae, and se, are column vectors,
%
% Usage:
% errmetrics = get_errmetrics(datatrue,dataout); %outputs struct
%
% rmse = get_errmetrics(datatrue,dataout,'rmse'); %outputs rmse value
%
% Dependencies:
% var_names.m
%
% Notes:
% See https://en.wikipedia.org/wiki/Root-mean-square_deviation
%
% Author(s): Sterling Baird
%
% Date: 2020-09-17
%--------------------------------------------------------------------------
%additional argument validation
szpred = size(ypred);
szmeas = size(ytrue);
assert(all(szpred==szmeas),['predicted size: ' num2str(szpred), ', measured size: ' num2str(szmeas)])
%error metrics
e = ypred-ytrue; %error
ae = abs(e); %absolute error
mae = mean(ae,'all'); %mean absolute error
se = e.^2; %square error
mse = mean(se,'all'); %mean square error
rmse = sqrt(mse); %root mean square error
if NV.dispQ
disp(['rmse = ' num2str(rmse) ', mae = ' num2str(mae)])
end
%compile into struct
errmetrics = var_names(ypred,ytrue,e,ae,mae,se,mse,rmse);
%assign output error value(s)
switch type
case 'all'
errout = errmetrics;
otherwise
errout = errmetrics.(type); %dynamic field reference
end
end
%---------------------------CODE GRAVEYARD---------------------------------
%{
%assign output error value(s)
switch type
case 'e'
errout = e;
case 'ae'
errout = ae;
case 'se'
errout = se;
case 'mse'
errout = mse;
case 'rmse'
errout = rmse;
case 'all'
end
%}