-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp5.jl
2800 lines (2353 loc) · 89.7 KB
/
p5.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
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
### A Pluto.jl notebook ###
# v0.19.18
using Markdown
using InteractiveUtils
# ╔═╡ 3b07c362-a1be-11ed-220f-bb7a984af75d
using DataFrames
# ╔═╡ 30a30649-1b0a-42ba-9e57-c58c9487c24e
using CSV
# ╔═╡ 826c34a2-d07e-4e00-9206-794e422f5d64
using ColorSchemes
# ╔═╡ b8dbef1c-1497-4f2b-b144-8a8b36406979
using Gadfly
# ╔═╡ a5367edd-7efa-43d3-8bcd-89ab5030801d
using YAML
# ╔═╡ 19b28374-44d1-4554-9bf4-bc452dac1261
using Dates
# ╔═╡ f34e8779-809d-4b00-b8fa-7acab4c08322
using StatsBase
# ╔═╡ 252aff18-14eb-41c8-a47f-900df098af2c
using MLJ
# ╔═╡ 8f159c5f-0994-4f25-bfde-e577bcaa8fe6
using Latexify
# ╔═╡ 68606de6-932b-48b8-8ea5-b33ee7bf347e
using Compose
# ╔═╡ dcda4940-6c1f-45f2-84c5-05ac616e1cc4
using Plots
# ╔═╡ df01f430-3d39-428e-a74b-24c67c8dbb07
using Random
# ╔═╡ 3c798c19-8fa9-49fd-bbb8-eb4b22681ce1
import Cairo, Fontconfig
# ╔═╡ db313831-96d4-4cbc-b6f5-1f49f0cff221
seasons = Dict(
1 => "Winter",
2 => "Winter",
3 => "Spring",
4 => "Spring",
5 => "Spring",
6 => "Summer",
7 => "Summer",
8 => "Summer",
9 => "Autumn",
10 => "Autumn",
11 => "Autumn",
12 => "Winter"
)
# ╔═╡ 7299bc57-f7c3-41d5-8a28-49e30799cf93
# ╔═╡ 0acd8872-98bc-4f2b-960d-d8d449d16407
# ╔═╡ 9462e8e8-5f45-48d2-978c-ff03695cc115
common_terms = [
"Property Id",
"date",
"month",
"prediction",
"recorded",
"model",
"zone",
"zipcode",
"weather_station_distance",
"area",
"heightroof"
]
# ╔═╡ d53007de-27f5-4cbb-9012-cacb281d612b
errfunc(deltay) = abs(deltay)
# ╔═╡ 90d09570-3eab-4999-bb7b-4b74ccae2f6f
# ╔═╡ e4b11e6f-bc70-4470-b407-92eb192489ca
# ╔═╡ 73d18d6d-b236-4fd4-9981-e70f8c1c37b7
nmbe(ŷ, y, p=1) = 100 * sum(y.-ŷ) / (( length(y)-p ) * mean(y) )
# ╔═╡ 31ea0f25-69d5-4da8-826b-a95e80772375
cvrmse(ŷ, y, p=1) = 100 * (sum((y.-ŷ).^2) / (length(y)-p))^0.5 / mean(y)
# ╔═╡ 51fee1ff-78fc-4d5c-950c-5f6158d068b9
cvstd(ŷ, y, p=1) = (100 * mean(y)) / ((sum( (y .- mean(y)).^2 )/(length(y)-1))^0.5)
# ╔═╡ 7d671ae3-4061-4f8a-a04b-6caed84a2bd9
md"""
#### Overall Benefits
Individual Building UBEM metrics
"""
# ╔═╡ 50c292c1-dcbc-47bb-a545-1ca12a0784fe
# modelname = "793244" # s1
# modelname = "689269" # s2
modelname = "746694" # s3
# ╔═╡ 258dc61d-e20e-4ec3-a8c7-feca49515225
modelname
# ╔═╡ 750c5adc-9f84-4a4a-9f8a-b9d347b00a2b
begin
sources_file = joinpath(pwd(), "sources.yml")
sources = YAML.load_file(sources_file)
data_destination = sources["output-destination"]
data_path = joinpath(data_destination, "data", "nyc")
input_dir = joinpath(joinpath(data_path, "p3_o"), modelname)
output_dir = joinpath(joinpath(data_path, "p5_o"), modelname)
mkpath(output_dir)
dataout_dir = joinpath(input_dir, "data_out")
end;
# ╔═╡ 6f9bb8eb-977c-499a-9ee7-d34b58ecc03e
epw = CSV.read(joinpath(data_path, "p1_o", "epw.csv"), DataFrame)
# ╔═╡ 5b27b6a5-140b-41b6-b0d5-208b83c5ff8c
sources
# ╔═╡ 4ec7e98e-c183-4c51-ac6b-4c79495eac67
begin
electricₜ = CSV.read(joinpath(input_dir, "training_electric.csv"), DataFrame);
gasₜ = CSV.read(joinpath(input_dir, "training_gas.csv"), DataFrame);
end;
# ╔═╡ 113966ec-2c5f-4778-866e-5d0e8f5185f5
describe(electricₜ, :detailed)[:,["variable","q25","median","q75"]]
# ╔═╡ 37a25f49-0283-4b63-82df-f627cd9b1f92
skewness(electricₜ.electricity_mwh)
# ╔═╡ 56d7a517-3e32-4ae9-ac9a-11ac6f62f6a8
describe(gasₜ, :detailed)[:,["variable","q25","median","q75"]]
# ╔═╡ a83ced13-9959-4c9c-8cba-2e136115f7a7
skewness(gasₜ.naturalgas_mwh)
# ╔═╡ b5852716-bec1-4b41-bb36-1588751ca824
begin
# electric
teₐ′₀ = CSV.read(joinpath(dataout_dir, "tea0.csv"), DataFrame)
teₐ′₂ = CSV.read(joinpath(dataout_dir, "tea2.csv"), DataFrame)
teₐ′₃ = CSV.read(joinpath(dataout_dir, "tea3.csv"), DataFrame)
teₐ′₄ = CSV.read(joinpath(dataout_dir, "tea4.csv"), DataFrame)
teₐ′₅ = CSV.read(joinpath(dataout_dir, "tea5.csv"), DataFrame)
teₐ′₆ = CSV.read(joinpath(dataout_dir, "tea6.csv"), DataFrame)
teₐ′₇ = CSV.read(joinpath(dataout_dir, "tea7.csv"), DataFrame)
teₐ′₈ = CSV.read(joinpath(dataout_dir, "tea8.csv"), DataFrame)
teₐ′₉ = CSV.read(joinpath(dataout_dir, "tea9.csv"), DataFrame)
teₐ′ₑ = CSV.read(joinpath(dataout_dir, "teae.csv"), DataFrame)
# # natural gas
teᵧ′₀ = CSV.read(joinpath(dataout_dir, "teg0.csv"), DataFrame)
teᵧ′₂ = CSV.read(joinpath(dataout_dir, "teg2.csv"), DataFrame)
teᵧ′₃ = CSV.read(joinpath(dataout_dir, "teg3.csv"), DataFrame)
teᵧ′₄ = CSV.read(joinpath(dataout_dir, "teg4.csv"), DataFrame)
teᵧ′₅ = CSV.read(joinpath(dataout_dir, "teg5.csv"), DataFrame)
teᵧ′₆ = CSV.read(joinpath(dataout_dir, "teg6.csv"), DataFrame)
teᵧ′₇ = CSV.read(joinpath(dataout_dir, "teg7.csv"), DataFrame)
teᵧ′₈ = CSV.read(joinpath(dataout_dir, "teg8.csv"), DataFrame)
teᵧ′₉ = CSV.read(joinpath(dataout_dir, "teg9.csv"), DataFrame)
# teᵧ′ₑ = CSV.read(joinpath(dataout_dir, "tege.csv"), DataFrame)
end;
# ╔═╡ a9a616b6-b121-4f84-9261-d3465466ba02
teₐ′₅
# ╔═╡ 9289932b-2af7-4991-971f-96f6ef395b14
begin
test_terms = [teₐ′₀,teₐ′₂,teₐ′₃,teₐ′₄,teₐ′₅,teₐ′₆,teₐ′₇,teₐ′₈,teₐ′₉];
resultsₑ = vcat([ DataFrames.select(x, common_terms) for x in test_terms ]...);
resultsₑ.diff = errfunc.(resultsₑ.prediction .- resultsₑ.recorded);
resultsₑ.fuel = repeat(["Electricity"], nrow(resultsₑ))
resultsₑ
end;
# ╔═╡ 4efca97a-34f4-41dd-bd76-3ffed4262d28
null_namesₑ = names(test_terms[1])
# ╔═╡ c1587eef-ec3e-42d1-ab0b-c5d8a395e5cd
begin
dataset_namesₑ = Dict()
null_terms = filter( x -> ~occursin(r"_f.", x), names(test_terms[1]))
push!(dataset_namesₑ, "Null" => length(null_terms))
for terms in test_terms
term_names = filter( x -> occursin(r"_f.", x), names(terms))
model_name = terms.model[1]
for name in term_names
push!(dataset_namesₑ, model_name => length(term_names))
end
end
dataset_namesₑ
end
# ╔═╡ 9679fec5-be9b-4fa8-a699-b18f0ada44b9
unique(resultsₑ.date)
# ╔═╡ 94a4b152-683f-450b-8016-1a3252b8ec56
begin
test_termsᵧ = [teᵧ′₀,teᵧ′₂,teᵧ′₃,teᵧ′₄,teᵧ′₅,teᵧ′₆,teᵧ′₇,teᵧ′₈,teᵧ′₉];
resultsᵧ = vcat([ DataFrames.select(x, common_terms) for x in test_termsᵧ ]...);
resultsᵧ.diff = errfunc.(resultsᵧ.prediction .- resultsᵧ.recorded);
resultsᵧ.fuel = repeat(["Natural Gas"], nrow(resultsᵧ))
resultsᵧ
end;
# ╔═╡ 75a9d962-54bd-498d-a513-035e9a225eff
results = vcat(resultsₑ, resultsᵧ);
# ╔═╡ 9a3fbbb9-0520-4a04-841e-e960928212b2
landsat = CSV.read(joinpath(data_path, "p2_o", "landsat8.csv"), DataFrame; dateformat="yyyy-mm-dd HH:MM:SS")
# ╔═╡ 5a07ce6a-4e09-4983-b0d6-c1a3e9d1d980
dropmissing!(landsat)
# ╔═╡ b1ffd85f-9e55-4b62-bfe5-6811744ca72b
landsat_dates = sort(unique(landsat.date))
# ╔═╡ d2381a47-019d-45df-ab7f-6b18f92ccd54
month_samples = values(countmap(Dates.Date.(Dates.Year.(landsat_dates), Dates.Month.(landsat_dates))))
# ╔═╡ 0c3cb738-f14a-4c3f-9a0a-af3bd1e6e585
mean(month_samples)
# ╔═╡ 19897c1b-8c2a-4ce8-a66e-8522591137b0
landsat_datediff = filter( x -> x >0, Dates.days.((landsat_dates[2:end] .- landsat_dates[1:end-1])))
# ╔═╡ 30c94fe5-fc8d-4ea1-8f0e-9f558905bb5c
Gadfly.plot(
x=landsat_datediff,
Geom.histogram,
Theme(default_color="black")
)
# ╔═╡ de81a406-6215-4396-9052-e4c9cb460351
median(landsat_datediff)
# ╔═╡ 2126d3c7-f73b-42b3-a8cd-9fad2f548786
landsat_dates[2]
# ╔═╡ 0f5c1310-84bb-491e-ac56-dde86b363a42
landsat_dates[1]
# ╔═╡ d3367231-3be3-4853-8639-04444ee32614
sort(unique(landsat.date))
# ╔═╡ ed4e75e7-14a0-41e4-81bf-56d5766135cc
function realnames(x)
rename(x, :fuel => "\\textbf{Energy Class}", :cvrmse => "\\textbf{CV(RMSE)}", :nmbe => "\\textbf{NMBE}", :cvstd => "\\textbf{CV(STD)}", :rmse => "\\textbf{RMSE}", :model => "\\textbf{Model}", :mae => "\\textbf{MAE}")
end
# ╔═╡ 14a12712-0dce-46ad-9e9b-57462d24b58d
md"""
Aggregated Building UBEM metrics
"""
# ╔═╡ 0df2d858-f0a7-4986-876f-f7c7b908051b
## graphical approach
# ╔═╡ 7288df95-22a7-423b-8821-f5d14742c1a3
results_agg = combine(groupby(results, ["model","month","zone","fuel"]), :diff => mean, renamecols=false);
# ╔═╡ 8266db80-4cd2-4f94-95f3-77792d149a9c
grouped_results = groupby(results, ["model","month","zone","fuel"]);
# ╔═╡ 80849026-8a25-48b0-8909-fea0aca1c712
results
# ╔═╡ b2215e09-a68e-453b-943b-76568faca5c1
sort(results_agg, [:fuel, :diff, :month])
# ╔═╡ eb9acb6d-2068-4681-8088-f7cb401f2682
sort(results_agg, [:fuel, :month, :zone, :diff])
# ╔═╡ 281893d0-4cc9-47c5-ae8b-468f2fcac2d2
r₁ = Gadfly.plot(
sort(results_agg, [:fuel, :month, :zone, :model]),
x=:month,
y=:model,
xgroup=:fuel,
ygroup=:zone,
color=:diff,
Geom.subplot_grid(
Geom.rectbin,
Guide.xticks(ticks=1:12)
),
Scale.ContinuousColorScale(
palette -> get(ColorSchemes.curl, palette),
minvalue=0,
maxvalue=100
),
Guide.title("Mean Absolute Error by Month, Model, and Building Class"),
Guide.colorkey(title="MAE")
)
# ╔═╡ 480c9afb-1103-427b-b534-7df2e60bb8f8
# ╔═╡ e88ed963-0ca0-4dc2-a475-3acb775f26ba
latexify(
rename(select(unstack(filter(x -> x.zone == "Commercial" && x.fuel == "Electricity", sort(results_agg, :month)), :month, :diff), Not([:zone, :fuel])), :model => Symbol("\\textbf{Model}")),
latex= false, fmt = FancyNumberFormatter(3), env=:table
)
# ╔═╡ fd8ad0e7-2610-4021-a0cc-b735c10a2e6a
latexify(
rename(select(unstack(filter(x -> x.zone == "Manufacturing" && x.fuel == "Electricity", sort(results_agg, :month)), :month, :diff), Not([:zone, :fuel])), :model => Symbol("\\textbf{Model}")),
latex= false, fmt = FancyNumberFormatter(3), env=:table
)
# ╔═╡ 679c5199-a2be-47cc-aefd-1fbcd3ec7126
latexify(
rename(select(unstack(filter(x -> x.zone == "Residential" && x.fuel == "Electricity", sort(results_agg, :month)), :month, :diff), Not([:zone, :fuel])), :model => Symbol("\\textbf{Model}")),
latex= false, fmt = FancyNumberFormatter(3), env=:table
)
# ╔═╡ c38e9b9a-7396-41f3-adf3-e3dd01ec6b19
latexify(
rename(select(unstack(filter(x -> x.zone == "Commercial" && x.fuel == "Natural Gas", sort(results_agg, :month)), :month, :diff), Not([:zone, :fuel])), :model => Symbol("\\textbf{Model}")),
latex= false, fmt = FancyNumberFormatter(3), env=:table
)
# ╔═╡ 94baa87b-0efa-46be-8ee7-1a9e4a568c37
latexify(
rename(select(unstack(filter(x -> x.zone == "Manufacturing" && x.fuel == "Natural Gas", sort(results_agg, :month)), :month, :diff), Not([:zone, :fuel])), :model => Symbol("\\textbf{Model}")),
latex= false, fmt = FancyNumberFormatter(3), env=:table
)
# ╔═╡ a86368bb-6c18-4c58-afbc-6e5e1737b887
latexify(
rename(select(unstack(filter(x -> x.zone == "Residential" && x.fuel == "Natural Gas", sort(results_agg, :month)), :month, :diff), Not([:zone, :fuel])), :model => Symbol("\\textbf{Model}")),
latex= false, fmt = FancyNumberFormatter(3), env=:table
)
# ╔═╡ 02a34094-e86c-482e-9fed-23001400a943
# ╔═╡ ac759b4b-aa8a-4645-ba37-d2a3e7c9b5be
draw(
PNG(
joinpath(output_dir, "aggregation_results.png"),
18cm,
16cm,
dpi=500
), r₁
)
# ╔═╡ aa362ebf-f169-4e1c-8434-719393731c2e
begin
results_agg_comparison = leftjoin(
results_agg,
filter(x->x.model == "EPW", results_agg),
on=["month","zone","fuel"],
makeunique=true
)
results_agg_comparison.improvement = 100 .* (results_agg_comparison.diff .- results_agg_comparison.diff_1 ) ./ results_agg_comparison.diff_1
results_agg_comparison.improvement = clamp.(results_agg_comparison.improvement, -Inf, 0)
end;
# ╔═╡ 09f74c7d-5ee3-4529-ae5f-ae2392de44de
sort(results_agg_comparison, [:fuel, :improvement])
# ╔═╡ de070df7-7dd2-4a97-a87b-33170144b896
sort(results_agg_comparison, [:fuel,:improvement])
# ╔═╡ 43a58ec0-95c3-4a9e-9106-c618284116b8
r₂ = Gadfly.plot(
# filter(x -> x.model ∈ ["EPW","NOAA","CMIP","Landsat8","Sentinel-2"],
sort(results_agg_comparison, [:fuel,:improvement], rev=true),
x=:month,
y=:model,
xgroup=:fuel,
ygroup=:zone,
color=:improvement,
Geom.subplot_grid(
Geom.rectbin,
Guide.xticks(ticks=1:12)
),
Guide.colorkey(title="Δ"),
Scale.ContinuousColorScale(
palette -> get(ColorSchemes.Greens, palette),
),
# Scale.ygroup(
# order=[
# "SAR",
# "VIIRS",
# "Dynamic World",
# "Sentinel-2",
# "Landsat8",
# "NOAA",
# "CMIP",
# "EPW",
# "Null"
# ]
# )
)
# ╔═╡ 4726c365-0295-4816-a86e-6dbafedcc003
draw(
PNG(
joinpath(output_dir, "aggregation_results_relative.png"),
20cm,
14cm,
dpi=500
), r₂
)
# ╔═╡ c9b841da-1974-467d-b360-40118082cb19
md"""
#### RQ 1
Weather Station Distance
"""
# ╔═╡ d7fabb28-8d25-46b9-8348-146c54c4ea8e
begin
meter_resolution = 750
results_metadata = filter(x-> x.model ∈ ["EPW","NOAA","Landsat8"], results);
results_metadata.distance_bucket = (results_metadata.weather_station_distance .- (results_metadata.weather_station_distance .% meter_resolution)) ./ 1000;
results_metadata.area_bucket = results_metadata.area .- (results_metadata.area .% 2500);
results_metadata.heightroof_bucket = results_metadata.heightroof .- (results_metadata.heightroof .% 100);
end;
# ╔═╡ 19d528ac-b099-4e9d-8037-b98cb39c731d
epw_results = filter(x -> x.model == "EPW", select(results, ["Property Id", "date", "model", "diff", "weather_station_distance","fuel"]))
# ╔═╡ 02dac397-1a6e-4523-9016-d7db811555cb
group_results = filter(x -> x.model ∈ ["Landsat8","NOAA"], select(results, ["Property Id", "date", "model", "diff","fuel"]))
# ╔═╡ 0531d20a-929d-4f0e-a999-5a5fba5a66d2
begin
resso = leftjoin(
group_results,
epw_results,
on=["Property Id","date","fuel"],
makeunique=true
)
resso.improvement = 100 .* (resso.diff .- resso.diff_1) ./ resso.diff_1
resso
end
# ╔═╡ 5b8c6173-11ee-4484-910f-1aec2e756ab6
# Gadfly.plot(
# resso,
# x=:weather_station_distance,
# y=:improvement,
# color=:model,
# ygroup=:fuel,
# Geom.subplot_grid(
# Geom.smooth(smoothing=0.1),
# Guide.xrug
# )
# )
# ╔═╡ 99b00d40-b112-40b5-af21-19f762c7bf99
rectbin_results = combine(groupby(results_metadata, [:area_bucket, :distance_bucket, :fuel, :heightroof_bucket]), :diff => mean, renamecols=false);
# ╔═╡ 86912ac4-6ad2-420d-a8a0-5680aa1d2c24
geomhair_results = combine(groupby(results_metadata, [:distance_bucket, :fuel,:model]), :diff => mean, renamecols=false);
# ╔═╡ 95645a95-d129-4ab2-965c-6fd28da6d211
begin
baseline_comparison = leftjoin(
geomhair_results,
filter(x -> x.model == "EPW", geomhair_results),
on=["distance_bucket","fuel"],
makeunique=true
)
baseline_comparison.random_dist = baseline_comparison.distance_bucket .+ (rand(nrow(baseline_comparison)) .- 0.5)
baseline_comparison.improvement = 100 .* (baseline_comparison.diff .- baseline_comparison.diff_1 ) ./ baseline_comparison.diff_1
end;
# ╔═╡ d04db8e6-784d-40c0-87dd-f42638c6fe89
baseline_comparison.distance_bucket
# ╔═╡ 0985c9d2-6705-4682-9d31-67f05dc8f81b
baseline_comparison.random_dist
# ╔═╡ 9ead69f4-4ce6-49e3-835f-47c88793a5c9
histogram(filter(x -> x.model == "Landsat8", baseline_comparison).improvement, bins=20)
# ╔═╡ 380a19ab-8219-45fe-aeff-6eba347c6806
results_epwdistance = Gadfly.plot(
filter(x -> x.model != "EPW", baseline_comparison),
x=:distance_bucket,
y=:improvement,
yintercept=[0],
y_group=:fuel,
# x_group=:model,
color=:model,
Geom.subplot_grid(
Geom.point,
# Geom.hair,
Geom.smooth(smoothing=0.5),
# Geom.line,
# Geom.step,
# Geom.smooth(smoothing=0.4),
# Geom.line,
Guide.xticks(ticks=collect(0:5:20)),
# Guide.xrug,
Guide.yticks(ticks=-20:15:30),
Geom.hline(color="pink"),
# Coord.cartesian(ymin=25, ymax=75),
free_y_axis=true
),
Theme(
point_size=1.75pt,
line_width=1.0pt,
key_position=:bottom,
default_color="gray",
major_label_font_size=12pt,
minor_label_font_size=10pt,
# highlight_width=0.5pt,
alphas=[0.7]
# color=[colorant"black", colorant"red"]
),
Guide.colorkey(title="Model"),
Guide.xlabel("Distance to Weather Station (km)"),
Guide.ylabel("Percent Difference MAE"),
Scale.color_discrete_manual(
colorant"#FF483B",
colorant"#83B592"
),
# Scale.y_log,
Guide.title("Prediction Error Relative to EPW (%) vs Distance")
)
# ╔═╡ 299d1227-6803-4f32-aa05-65b73817c3e3
draw(
PNG(
joinpath(output_dir, "distance_error_epw.png"),
13cm,
10cm,
dpi=500
), results_epwdistance
)
# ╔═╡ 3527d303-01ae-4b41-a4f5-f0cad5dba29c
# ╔═╡ a82232c7-4904-435c-ad1c-b797d79232ba
md"""
#### Hypothesis 2
Seasonal Benefits
"""
# ╔═╡ 20b0a476-3b73-4893-bc31-c9142b1a2531
Gadfly.plot(
results,
x=:date,
y=:diff,
color=:model,
ygroup=:fuel,
Geom.subplot_grid(
Geom.smooth(smoothing=0.2),
free_y_axis=true,
Guide.xticks(
ticks=DateTime("2018-01-1"):Month(3):DateTime("2021-01-01"),
orientation=:vertical
),
),
)
# ╔═╡ 470ea684-8977-43ef-aaa0-eab98907b93d
begin
Ξ = copy(results)
Ξ′ = combine(groupby(Ξ, [:model,:month,:fuel]), :diff => mean, renamecols=false)
Ξ′.color = map(x->seasons[x],Ξ′.month)
Ξ′.model = convert.(String, Ξ′.model)
end;
# ╔═╡ 2d6815ea-762f-4ff4-a632-715653c0c89a
# p₄ = Gadfly.plot(
# filter(x->x.model ∈ ["Null","EPW","Landsat8"], Ξ′),
# x=:month,
# y=:diff,
# color=:color,
# xgroup=:model,
# ygroup=:fuel,
# yintercept=[0],
# Guide.ylabel("Δ Prediction - Recorded"),
# Guide.xlabel("Month"),
# Guide.title("Mean Electricity Error by Season"),
# Guide.colorkey(title="Season"),
# Geom.subplot_grid(
# # Guide.yticks(ticks=0.0:0.001:0.01),
# Guide.xticks(ticks=1:2:12),
# # Geom.hair,
# Geom.point,
# # Geom.line,
# Geom.hline(color=["pink"], style=:solid),
# # Geom.point,
# free_y_axis=true,
# ), # Coord.cartesian(ymin=-0.1, ymax=0.1, aspect_ratio=1.5),
# Theme(key_position = :bottom),
# # Scale.color_discrete_manual(
# # colorant"skyblue",
# # colorant"lightgreen",
# # colorant"indianred",
# # colorant"coral"
# # ),
# )
# ╔═╡ 7980fbcd-9e53-481f-95ed-18818ecf0c49
begin
epw_comparison = leftjoin(
filter(x -> x.model == "EPW", Ξ′),
Ξ′,
on=[:month,:fuel],
makeunique=true
)
epw_comparison.improvement = 100 .* ( epw_comparison.diff_1 .- epw_comparison.diff ) ./ epw_comparison.diff
epw_comparison.improvement = clamp.(epw_comparison.improvement, -Inf, 0)
end;
# ╔═╡ aa8dcf2e-6776-403a-abcf-9dee17839c8f
p₄ = Gadfly.plot(
filter(x->x.model_1 ∈ ["Landsat8","NOAA"], epw_comparison),
x=:month,
y=:improvement,
color=:color_1,
xgroup=:model_1,
ygroup=:fuel,
yintercept=[0],
Guide.ylabel("Percent Difference to EPW - MAE"),
Guide.xlabel("Months by Model"),
Guide.title("Relative Benefits to EPW by Season - MAE"),
Guide.colorkey(title="Season"),
Geom.subplot_grid(
# Guide.yticks(ticks=0.0:0.001:0.01),
Guide.xticks(ticks=1:2:12),
Guide.yticks(ticks=-10:2.5:0),
Geom.hair,
Geom.point,
# Geom.line,
Geom.hline(color=["pink"], style=:solid),
# Geom.point,
# free_y_axis=true,
), # Coord.cartesian(ymin=-0.1, ymax=0.1, aspect_ratio=1.5),
Theme(
key_position = :bottom,
line_width=1.7pt,
point_size=3.0pt,
# minor_label_font_size,
major_label_font_size=12pt,
minor_label_font_size=10pt
),
Scale.color_discrete_manual(
colorant"#55134E",
colorant"#12817B",
colorant"#FF483B",
colorant"#83B592"
),
)
# ╔═╡ f193ade3-9ed2-493d-b97f-cdeb919acaeb
draw(
PNG(
joinpath(output_dir, "seasonality_benefits.png"),
12cm,
12cm,
dpi=500
), p₄
)
# ╔═╡ 575b3d7d-2484-4eb0-9f4d-720c3c550dae
mae
# ╔═╡ fdcef3ad-2692-4de2-b578-e30acf11842e
begin
function aggregation_results(individual_results, aggterms = ["date","zipcode","fuel","model"])
ṫ = combine(groupby(individual_results, aggterms), [:prediction, :recorded] .=> sum, nrow => :count, renamecols=false)
res = combine(groupby(ṫ, filter( x -> x ≠ "date", aggterms))) do vᵢ
(
cvrmse = cvrmse(vᵢ.prediction, vᵢ.recorded),
nmbe = nmbe(vᵢ.prediction, vᵢ.recorded),
cvstd = cvstd(vᵢ.prediction, vᵢ.recorded),
rmse = rmse(vᵢ.prediction, vᵢ.recorded),
mae = mae(vᵢ.prediction, vᵢ.recorded),
model = first(vᵢ.model),
count = first(vᵢ.count)
)
end
return res, combine(
groupby(res, [:fuel, :model]),
names(res, Float64) .=> median,
renamecols=false
)
end
end
# ╔═╡ 2808cc24-c3bb-4b69-bc4d-8503c1503a46
begin
res, aggresults = aggregation_results(results);
res.random_count = res.count .+ rand(nrow(res))
sort(aggresults, [:fuel, :rmse])
end
# ╔═╡ 2eb802aa-a9da-4f7e-8ca5-d3897c5e2874
resultsₐ = realnames(sort(select(aggresults, [:fuel, :model, :cvrmse, :nmbe, :mae], :), [:fuel, :cvrmse]))
# ╔═╡ 01ecc218-b2e6-4fec-8c7c-5898d20f769e
resultsₐ
# ╔═╡ 94769e0b-6649-4ba5-87e3-e4dd7603423e
resultsₐ
# ╔═╡ 33ce90bf-5c8e-496a-b7a4-514f1550989a
latexify(resultsₐ, latex=false, fmt = FancyNumberFormatter(4), env=:table)
# ╔═╡ 3322ff22-f2cd-467e-929a-238186ab7184
percentile(unique(select(res, [:zipcode, :count]), :zipcode).count, 75)
# ╔═╡ 9a0f2fb6-372a-4658-be76-d9562f478f31
begin
resᵪ, aggresultsᵪ = aggregation_results(results, ["date","zipcode","fuel","model","zone"]);
end
# ╔═╡ cbd5410b-5d3e-41aa-9c63-7d089617f54a
aggresultsᵪ
# ╔═╡ 4b6e88dc-82e1-43d7-a92a-6b3a5b01f01d
begin
function individual_results(df, aggterms = ["fuel","model"])
res = combine(groupby(df, aggterms)) do vᵢ
(
cvrmse = cvrmse(vᵢ.prediction, vᵢ.recorded),
nmbe = nmbe(vᵢ.prediction, vᵢ.recorded),
cvstd = cvstd(vᵢ.prediction, vᵢ.recorded),
rmse = rmse(vᵢ.prediction, vᵢ.recorded),
mae = mae(vᵢ.prediction, vᵢ.recorded),
model = first(vᵢ.model)
)
end
return res
end
end
# ╔═╡ 81af87a5-aba3-4c14-a2ec-57e49af69066
resultsᵢ = realnames(sort(select(individual_results(results), [:fuel, :model, :cvrmse, :nmbe, :mae], :), [:fuel, :rmse]))
# ╔═╡ 4936a25d-90a9-44f7-8213-0831b47b5953
latexify(resultsᵢ, latex=false, fmt = FancyNumberFormatter(4), env=:table)
# ╔═╡ 9172ed67-695c-4818-90d0-ee5e3104feae
resultsᵪᵢ = rename(realnames(sort(individual_results(results, ["fuel","zone","model"]), [:fuel,:zone,:cvrmse])), :zone => "\\textbf{Zone}")
# ╔═╡ 08d901c6-f7ce-4070-b41c-ec3661868861
latexify(resultsᵪᵢ, latex=false, fmt = FancyNumberFormatter(4), env=:table)
# ╔═╡ edc350d5-e505-4487-a4a6-cfb680711ac1
begin
joining_terms = ["Property Id", "date"]
extra_omission_features = [
"weather_station_distance",
"zone",
"zipcode",
"month",
"model",
"recorded",
"prediction"
]
exclusion_termsₑ = Not([
joining_terms...,
extra_omission_features...,
"electricity_mwh"
])
exclusion_termsᵧ = Not([
joining_terms...,
extra_omission_features...,
"naturalgas_mwh"
])
end;
# ╔═╡ 33f58d0f-bcb6-4528-a10d-548fdf429196
# Gadfly.plot(
# x=filter(x -> x>-1, res.count),
# Geom.histogram(bincount=40),
# Guide.xticks(ticks=0:5:50),
# Guide.title("Count of buildings in each zipcode"),
# Guide.xlabel("Count of Zipcodes"),
# Guide.ylabel("Building Count"),
# Theme(default_color="black")
# )
# ╔═╡ 506850d0-30dd-4eba-83fb-42c9a00b37d6
md"""
#### Hypothesis 3
Aggregated Benefits
"""
# ╔═╡ f5482c86-da26-4b37-bd22-78367726ccb6
combine(groupby(res, [:fuel, :model]), :cvrmse => median)
# ╔═╡ 5516e2ca-abc9-47fe-89b3-57ff36b003c6
median(filter(x -> x < 300, res.cvrmse))
# ╔═╡ a06bf7f0-b234-4a59-a92b-18be3cfb8f10
Gadfly.plot(
x=filter(x -> x < 300, res.cvrmse),
Guide.xticks(ticks=0:25:300),
Geom.histogram,
Guide.title("Histogram of CVRMSE scores"),
Guide.ylabel("Count of Buildings"),
Guide.xlabel("CV(RMSE)")
)
# ╔═╡ a9e93d79-b97b-4fc1-9304-2a5f3ec2f4bf
begin
modelchoice = ["EPW","Landsat8","NOAA"]
scalingplot = Gadfly.plot(
stack(
filter(x -> x.model ∈ modelchoice && x.count > 2,
res
), [:cvrmse, :nmbe, :rmse]), #:cvstd, :rmse
x=:random_count,
y=:value,
color=:model,
yintercept=[0],
ygroup=:variable,
# xgroup=:fuel,
Geom.subplot_grid(
# Geom.point,
Geom.smooth,
# Geom.smooth(smoothing=0.5, color="red"),
Geom.hline(color="pink"),
Guide.xrug,
Guide.xticks(ticks=collect(0:5:40), label=true),
free_y_axis=true
),
# Scale.x_log,
Guide.title("Metric Scaling Behavior"),
Guide.xlabel("Number of Buildings"),
Guide.ylabel("Error Metric"),
# Scale.x_log,
Theme(
line_width=1.2pt,
# rug_size=8pt,
point_size=0.5pt,
# alphas=[0.3],
default_color="gray",
highlight_width = 0pt,
key_position=:bottom,
),
Scale.color_discrete_manual(
# colorant"#55134E",
colorant"#12817B",
colorant"#FF483B",
colorant"#83B592"
),
)
end
# ╔═╡ 55d0c30f-8253-42c6-a531-66cb18cedd9a
draw(
PNG(
joinpath(output_dir, "scaling-behavior.png"),
12cm,
20cm,
dpi=500
), scalingplot
)
# ╔═╡ b65ab7f0-ab57-4e8a-813f-87d54c77f60d
md"""
###### temperature and error
"""
# ╔═╡ c0073981-495d-4352-b745-04511d02d5fd
resultsₜ = filter(x-> x.model ∈ ["EPW","Landsat8"], results);
# ╔═╡ 06228a5c-7bd5-41f4-910e-aca4ee21fbd1
begin
tₑ = select(teₐ′₅, Not(:electricity_mwh))
tₑ.fuel = repeat(["Electricity"], nrow(tₑ))
tᵧ = select(teᵧ′₆, Not(:naturalgas_mwh))
tᵧ.fuel = repeat(["Natural Gas"], nrow(tᵧ))
landsat_predictions = vcat(
tₑ,
tᵧ
)
landsat_predictions.diff = abs.(landsat_predictions.prediction .- landsat_predictions.recorded)
landsat_predictions.temperature_bucket = (landsat_predictions.ST_B10 .- (landsat_predictions.ST_B10 .% 2.5));
temperature_estimates = combine(groupby(landsat_predictions, [:fuel, :temperature_bucket]),
:diff => (x -> quantile(x, 0.25)) => :q25,
:diff => median => :q50,
:diff => (x -> quantile(x, 0.75)) => :q75,
:diff => (x -> length(x) / 50) => :nrow,
:diff => (x -> 0.0) => :zeroterms
);end
# ╔═╡ 1c68d5f7-fab6-4564-8184-106a2abf39e4
tₑ
# ╔═╡ f7de4fc1-5ba5-431e-808f-50b89b346bba
lsample = landsat_predictions[sample(1:nrow(landsat_predictions), 1000),:];
# ╔═╡ ce34731a-cf8f-4e17-b721-82cefd53e859
maximum(temperature_estimates.q75)
# ╔═╡ 771a46a8-bc0f-4582-afea-6e481704b95a
t₀ = Gadfly.plot(
temperature_estimates,
x=:temperature_bucket,
y=:nrow,
Geom.bar,
# Scale.y_log,
Guide.xticks(ticks=-15:5:55, label=false),
# Guide.yticks(label=false),
# Guide.yticks(ticks=0:1000:2000),
Guide.xlabel(""),
Guide.ylabel(""),
Theme(default_color="black")
)
# ╔═╡ f8d68092-ce96-454a-ae36-a6b94673b04f
t₁ = Gadfly.plot(
temperature_estimates,
x=:temperature_bucket,
y=:q50,
ymin=:q25,
ymax=:q75,
yintercept=[0],
xgroup=:fuel,
Geom.subplot_grid(
# Geom.point,
Geom.step,
# Geom.smooth(smoothing=0.5),
# Guide.xrug,
Geom.ribbon,
Geom.hline(color="gray"),
Guide.xticks(ticks=-15:20:55),
# free_y_axis=true,
# Guide.yticks(ticks=0:20:100),
#Coord.Cartesian(ymin=0, ymax=150),
),
# Geom.label,
# Geom.line,
# Guide.xticks(ticks=-15:5:55),
# Scale.y_log10,
Theme(
alphas=[0.5],
default_color="black",
point_size=2pt,
line_width=1pt,
major_label_font_size=12pt,
minor_label_font_size=10pt,
),
Guide.xlabel("Landsat8 LST - °C"),
Guide.ylabel("Monthly MAE - MWh"),
Guide.title("Extreme Temperature Predictions - Error Profile"),
Guide.ylabel(""),
# Geom.smooth(smoothing=0.5)
)
# ╔═╡ 298e9181-6f03-4b73-9ebb-2f54adfccf8d
draw(
PNG(
joinpath(output_dir, "temperature_error.png"),
12cm,
10cm,
dpi=500
), t₁
)
# ╔═╡ 98e438b6-d504-44a9-8fbe-45c9fc19e8c2
Plots.plot
# ╔═╡ 417470dc-aa97-4b49-b4bb-3e1c54b2f3b5
teₐ′₂