-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCNNForecasting.m
More file actions
63 lines (55 loc) · 1.75 KB
/
CNNForecasting.m
File metadata and controls
63 lines (55 loc) · 1.75 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
% CNN Forecasting Toy Example
% addpath("/Users/yahoo/Documents/WashU/CSE515T/Code/gpml-matlab-v4.2-2018-06-11");
addpath("gpml-matlab-v3.6-2015-07-07");
addpath("utilities");
startup;
% reading race data from all years and states
% CNNdata = readData("CNNData.csv");
% pollthres = 40;
% CNNdata = indexPollster(CNNdata, pollthres, "Gaussian Process/CNNDataidx.csv");
CNNdata = readData("CNNDataidx.csv");
parms.mode = true;
parms.nfirm = max(CNNdata.pollsteridx);
years = unique(CNNdata.cycle);
states = unique(CNNdata.state);
% iterate cycle/state race
N = 0; nsuc = 0;
allPollVote = cell(numel(years),numel(states));
for i = 1:numel(years)
for j = 1:numel(states)
% success = -1 if there is race data for year/state
% success = 0 if failling to predict, otherwise 1
[success, predPolls, trueVotes] = buildOneRaceModel(CNNdata, years(i), states(j), parms);
N = N + (success>=0); nsuc = nsuc + max([0, success]);
if success>=0, allPollVote{i, j} = {predPolls, trueVotes, success}; end
end
end
disp(N + " races run.");
disp(nsuc + " successful predictions.");
disp("Prediction rate: " + nsuc/N);
% post training
a = [];
b = [];
errors = [];
for i = 1:numel(years)
for j = 1:numel(states)
if isempty(allPollVote{i,j}), continue; end
predPolls = allPollVote{i, j}{1};
trueVotes = allPollVote{i, j}{2};
a = [a, predPolls'];
b = [b, trueVotes'/100];
errors = [errors, (predPolls - trueVotes/100).'];
end
end
histogram(abs(errors));
xlabel("absolute error");
ylabel("freq");
wp = [3,1,2,2,0,3,3,0,1,2,1,2,3];
cp = [34,33,33,34,33,33,34,33,33,34,33,33,34] - wp;
h = bar(years, [wp;cp]);
l{1} = 'wrong';
l{2} = 'correct';
legend(h,l);
xlabel("year");
ylabel("#predictions");
title("#predictions over year");