/
vocfit.py
1346 lines (1217 loc) · 52.5 KB
/
vocfit.py
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
from stuff import *
import sys,re,argparse,pickle,pytz,pdb
from scipy.optimize import minimize
from scipy.stats import norm,binom,bernoulli
from scipy.special import gammaln
from math import log,exp,sqrt,sin,pi
import numpy as np
from subprocess import Popen,PIPE
from datetime import datetime
# (Make it auto download files?)
# Get ltla.csv from https://coronavirus.data.gov.uk/api/v2/data?areaType=ltla&metric=newCasesBySpecimenDate&format=csv
# Sanger data from https://covid-surveillance-data.cog.sanger.ac.uk/download/lineages_by_ltla_and_week.tsv
# COG-UK data from https://cog-uk.s3.climb.ac.uk/phylogenetics/latest/cog_metadata.csv
# SGTF data from Fig.16 Tech Briefing 12: https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/988608/Variants_of_Concern_Technical_Briefing_12_Data_England.xlsx
def sanitise(fn): return fn.replace(' ','_').replace("'","")
ltlaengdata=loadcsv("Local_Authority_District_to_Region__December_2019__Lookup_in_England.csv")
ltlaukdata=loadcsv("Local_Authority_District_to_Country_(April_2019)_Lookup_in_the_United_Kingdom.csv")
ltla2ltla=dict(zip(ltlaukdata['LAD19CD'],ltlaukdata['LAD19CD']))
ltla2uk=dict((ltla,"UK") for ltla in ltlaukdata['LAD19CD'])
ltla2country=dict(zip(ltlaukdata['LAD19CD'],ltlaukdata['CTRY19NM']))
ltla2region=dict(ltla2country,**dict(zip(ltlaengdata['LAD19CD'],ltlaengdata['RGN19NM'])))
ltla2name=dict(zip(ltlaukdata['LAD19CD'],map(sanitise,ltlaukdata['LAD19NM'])))
#variantset=["B.1.617.2", "AY."];nonvariantset=["B.1.1.7"];variant="Delta";nonvariant="Alpha"
#variantset=["AY.4.2"];nonvariantset=[""];variant="AY.4.2";nonvariant="non-AY.4.2"
variantset=["BA.1", "BA.2"];nonvariantset=[""];variant="Omicron";nonvariant="non-Omicron"
# Set bounds for relative daily growth rate
(hmin,hmax)=(0.0,0.2)
if variant=="B.1.617.2": (hmin,hmax)=(0.03,0.15)
if variant=="AY.4.2": (hmin,hmax)=(0.01,0.04)
if variant=="Omicron": (hmin,hmax)=(0.3,0.5)
def varmatch(var,pattern):
if pattern=="": return True
if pattern[-1:]!='.': return var==pattern
return var[:len(pattern)]==pattern
def coglab2uk(x): return "UK"
def coglab2country(x): return x.split('/')[0].replace('_',' ')
def coglab2coglab(x): return x
def sgtf2region(x):
if x=='Yorkshire and Humber': return 'Yorkshire and The Humber'
return x
def sgtf2country(x): return 'England'
def includeltla(ltla,ltlaset):
if ltlaset=="London":
return ltla2region[ltla]=='London'
elif ltlaset=="test":
return ltla2region[ltla]=='London' and ltla<'E09000010'
elif ltlaset=="Bolton":
return ltla=='E08000001'
elif ltlaset=="Hartlepool":
return ltla=='E06000001'
elif ltlaset=="NE":
return ltla2region[ltla]=='North East'
elif ltlaset=="All":
return True
else:
raise RuntimeError("Unrecognised ltla set "+ltlaset)
parser=argparse.ArgumentParser()
parser.add_argument('-l', '--load-options', help='Load options from a file')
parser.add_argument('-s', '--save-options', help='Save options to a file')
parser.add_argument('-g', '--graph-filename', help='Stem of graph filenames')
args=parser.parse_args()
### Model ###
#
# Known:
# n_i = number of confirmed cases on day i by specimen date (slightly adjusted for weekday)
# p = case ascertainment rate (chance of seeing a case)
# g_{-1}(r),v_{-1}(r) = emperical growth rate (and its variance) in the two weeks up to 2021-04-10 as a function of region, r
# r_j, s_j = Variant counts of non-Delta, Delta in j^th week
# I_j = set of days (week) corresponding to VOC counts r_j, s_j
# Assume chance of sequencing a case is a totally free parameter, and optimise over it
#
# Unknown:
# h = daily growth advantage of Delta over other variants
# X_n = Fourier coefficients controlling the growth of non-Delta
# A_0 = initial count of non-Delta
# B_0 = initial count of Delta
#
# Likelihood:
# A_{i+1}=e^{g_i}A_i
# B_{i+1}=e^{g_i+h}B_i
# n_i ~ NB(mean=p(A_i+B_i),var=mean/nif1)
# r_j ~ BetaBinomial(r_j+s_j, A_{I_j}nif2/(1-nif2), B_{I_j}nif2/(1-nif2)) (A_{I_j} means sum_{i in I_j}A_i)
# g_0 ~ N(g_{-1},v_{-1})
# X_n ~ N(0,1)
# L=ndays+2*bmsig
# g_i = g_0 + bmscale*sqrt(L)*(i/L*X_0 + sqrt(2)/pi*sum_n e^{-(n*bmsig/L)^2/2}sin(n*pi*i/L)*X_n/n)
# h ~ N(0,tau^2)
#
### End Model ###
nfp=5# Number of fixed (non BM) parameters
### Options ###
#source="Sanger"
#source="COG-UK"
source="SGTF"
# Can choose location size from "LTLA", "region", "country", "UK"
# Sanger works with LTLA, region, country
# COG-UK works with country, UK, and partially with coglab (coglab doesn't match up with case-count areas, so can only do VOC-only growth rates)
# SGTF works with region, country
#locationsize="coglab"
#locationsize="LTLA"
locationsize="region"
#locationsize="country"
#locationsize="UK"
ltlaexclude=set()
#ltlaexclude=set(['E08000001','E12000002'])# Bolton, Manchester
#ltlaexclude=set(['E08000001','E12000002']+[x for x in ltla2region if ltla2region[x]=='London'])# Bolton, Manchester, London
ltlaset="All"
#ltlaset="London"
#ltlaset="Bolton"
#ltlaset="Hartlepool"
# Will plot graph of these locations even if only encountered during subdivision of global growth mode
specialinterest=set()#['E08000001'])
mgt=5# Mean generation time in days
# Earliest day to use case data
minday=datetoday('2021-11-10')# Inclusive
# Earliest day to use VOC count data, given as end-of-week. Will be rounded up to match same day of week as lastweek.
#firstweek=minday+6
firstweek=datetoday('2021-11-10')
nif1=0.048 # Non-independence factor (1/overdispersion) for cases (less than 1 means information is downweighted)
nif2=0.255 # Non-independence factor (1/overdispersion) for VOC counts (ditto)
isd0=1.0 # Inverse sd for prior on starting number of cases of non-Delta: assume starts off similar to total number of cases
isd1=0.3 # Inverse sd for prior on starting number of cases of Delta (0.3 is very weak)
isd2=1 # Inverse sd for prior on competitive advantage (as growth rate per day). 0 means uniform prior. 1 is very weak.
# Prior linking initial daily growth rate to estimate from pre-Delta era
sig0=0.004
# Timescale in days over which growth rate can change significantly
# (lower = more wiggles)
bmsig=25
# Lengthscale for filtered Brownian motion
# (higher = greater amplitude for the wiggles)
# bmscale will be set below - no longer user specifiable
bmscale=0.01
# Case ascertainment rate
asc=0.408
# Discard this many cases at the end of the list of cases by specimen day (may be increased later if Wales is in the mix)
discardcasedays=1
# Discard this many days of the latest COG data
discardcogdays=2
# Collect together all locations without positive entries into one combined "Other" location
# (Makes little difference in practice)
bundleremainder=True
optmethod="SLSQP";minopts={"maxiter":100000,"eps":1e-4,'ftol':1e-11}
#optmethod="L-BFGS-B";minopts={"maxiter":50000,"maxfun":1000000}
mode="local growth rates"
#mode="global growth rate"
#mode="fixed growth rate",0.1
voclen=(1 if source=="COG-UK" or source=="SGTF" else 7)
conf=0.95
nsamp=2000
model="scaledpoisson"
#model="NBBB"
#model="NBBB+magicprior"
plainvarcountest=False
### End options ###
opts={
"Source": source,
"Location size": locationsize,
"LTLA set": ltlaset,
"LTLA exclude": list(ltlaexclude),
"Generation time (days)": mgt,
"Earliest day for case data": daytodate(minday),
"Earliest week (using end of week date) to use VOC count data": daytodate(firstweek),
"Optimisation mode": mode,
"nif1": nif1,
"nif2": nif2,
"Inverse sd for prior on initial non-Delta": isd0,
"Inverse sd for prior on initial Delta": isd1,
"Inverse sd for prior on growth": isd2,
"Sigma (prior on daily growth rate change)": sig0,
"Timescale of growth rate change (days)": bmsig,
"Lengthscale for filtered Brownian motion": bmscale,
"Case ascertainment rate": asc,
"Number of days of case data to discard": discardcasedays,
"Number of days of COG-UK data to discard": discardcogdays,
"Bundle remainder": bundleremainder,
"Minimiser options": minopts,
"Length of time period over which VOC counts are given (days)": voclen,
"Confidence level": conf,
"Number of samples for confidence calcultions in hierachical mode": nsamp,
"Model": model
}
if args.save_options!=None:
with open(args.save_options,'w') as fp: json.dump(opts,fp,indent=2)
if args.load_options!=None:
with open(args.load_options,'r') as fp: lopts=json.load(fp)
for x in lopts: opts[x]=lopts[x]
source=opts["Source"]
locationsize=opts["Location size"]
ltlaset=opts["LTLA set"]
ltlaexclude=set(opts["LTLA exclude"])
mgt=opts["Generation time (days)"]
minday=datetoday(opts["Earliest day for case data"])
firstweek=datetoday(opts["Earliest week (using end of week date) to use VOC count data"])
mode=opts["Optimisation mode"]
nif1=opts["nif1"]
nif2=opts["nif2"]
isd0=opts["Inverse sd for prior on initial non-Delta"]
isd1=opts["Inverse sd for prior on initial Delta"]
isd2=opts["Inverse sd for prior on growth"]
sig0=opts["Sigma (prior on daily growth rate change)"]
bmsig=opts["Timescale of growth rate change (days)"]
bmscale=opts["Lengthscale for filtered Brownian motion"]
asc=opts["Case ascertainment rate"]
discardcasedays=opts["Number of days of case data to discard"]
discardcogdays=opts["Number of days of COG-UK data to discard"]
bundleremainder=opts["Bundle remainder"]
minopts=opts["Minimiser options"]
voclen=opts["Length of time period over which VOC counts are given (days)"]
conf=opts["Confidence level"]
nsamp=opts["Number of samples for confidence calcultions in hierachical mode"]
model=opts["Model"]
# Make sure this is at least 2 if Wales is in the mix, because it has later reporting
discardcasedays=max(int(locationsize=="UK" or (source=="COG-UK" and locationsize=="country"))*2,discardcasedays)
opts["Number of days of case data to discard"]=discardcasedays
# if locationsize=="LTLA": bmscale=0.1
# elif locationsize=="region": bmscale=0.03
# else: bmscale=0.01
# opts["Lengthscale for filtered Brownian motion"]=bmscale
zconf=norm.ppf((1+conf)/2)
print("Options:")
print()
for x in sorted(list(opts)): print("%s:"%x,opts[x])
print()
sys.stdout.flush()
np.set_printoptions(precision=3,linewidth=250)
if ltlaset=="All":
if source=="COG-UK": areacovered="UK"
else: areacovered="England"
else:
areacovered=ltlaset
apicases=loadcsv("ltla.csv")
d=datetime.now(pytz.timezone("Europe/London"))
publishedday=Date(d.strftime('%Y-%m-%d'))
if d.hour+d.minute/60<16+5/60: publishedday-=1# Dashboard/api updates at 4pm UK time
if max(apicases['date'])<publishedday-1:
import requests
url='https://coronavirus.data.gov.uk/api/v2/data?areaType=ltla&metric=newCasesBySpecimenDate&format=csv'
response=requests.get(url, timeout=10)
if not response.ok: raise RuntimeError(response.text)
with open('ltla.csv','w') as fp: fp.write(response.text)
apicases=loadcsv('ltla.csv')
# Reset the effective published date to cater for delays in publishing, also for Christmas etc
publishedday=Date(max(apicases['date']))+1
maxday=datetoday(max(apicases['date']))-discardcasedays# Inclusive
ndays=maxday-minday+1
firstweek=max(firstweek,minday+voclen-1)
if source=="Sanger":
fullsource="Wellcome Sanger Institute"
assert voclen==7
sanger=loadcsv("lineages_by_ltla_and_week.tsv",sep='\t')
lastweek=datetoday(max(sanger['WeekEndDate']));assert maxday>=lastweek
nweeks=(lastweek-firstweek)//voclen+1
# Sanger week number is nweeks-1-(lastweek-day)//voclen
if locationsize=="LTLA":
reduceltla=ltla2ltla
elif locationsize=="region":
reduceltla=ltla2region
elif locationsize=="country":
reduceltla=ltla2country
else:
raise RuntimeError("Incompatible source, locationsize combination: "+source+", "+locationsize)
# Get Sanger (variant) data into a suitable form
vocnum={}
for (date,ltla,var,n) in zip(sanger['WeekEndDate'],sanger['LTLA'],sanger['Lineage'],sanger['Count']):
if ltla in ltlaexclude or not includeltla(ltla,ltlaset): continue
day=datetoday(date)
week=nweeks-1-(lastweek-day)//voclen
if week>=0 and week<nweeks:
place=reduceltla[ltla]
if place not in vocnum: vocnum[place]=np.zeros([nweeks,2],dtype=int)
if any(varmatch(var,pat) for pat in variantset): vocnum[place][week][1]+=n
elif any(varmatch(var,pat) for pat in nonvariantset): vocnum[place][week][0]+=n
elif source=="COG-UK":
fullsource="COG-UK"
cog=loadcsv("cog_metadata.csv")
lastweek=datetoday(max(cog['sample_date']))-discardcogdays
assert maxday>=lastweek
nweeks=(lastweek-firstweek)//voclen+1
# Week number is nweeks-1-(lastweek-day)//voclen
if locationsize=="coglab":
reduceltla=None;bundleremainder=False
reducecog=coglab2coglab
elif locationsize=="country":
reduceltla=ltla2country
reducecog=coglab2country
elif locationsize=="UK":
reduceltla=ltla2uk
reducecog=coglab2uk
else:
raise RuntimeError("Incompatible source, locationsize combination: "+source+", "+locationsize)
# Get COG-UK (variant) data into a suitable form
vocnum={}
for (date,seqname,var) in zip(cog['sample_date'],cog['sequence_name'],cog['lineage']):
day=datetoday(date)
week=nweeks-1-(lastweek-day)//voclen
if week>=0 and week<nweeks:
r=re.match("[^0-9-]*[0-9-]",seqname)
coglab=seqname[:r.end()-1]
place=reducecog(coglab)
if place not in vocnum: vocnum[place]=np.zeros([nweeks,2],dtype=int)
if any(varmatch(var,pat) for pat in variantset): vocnum[place][week][1]+=1
elif any(varmatch(var,pat) for pat in nonvariantset): vocnum[place][week][0]+=1
elif source=="SGTF":
assert voclen==1
l=[x for x in os.listdir('.') if x[:19]=='sgtf_regionepicurve']
if l==[]: raise RuntimeError("No sgtf_regionepicurve csv file found in current directory")
sgtf=loadcsv(max(l))
lastweek=max(datetoday(x) for x in sgtf['specimen_date'])
fullsource="SGTF data from Omicron daily overview, last specimen date "+daytodate(lastweek)
assert maxday>=lastweek
nweeks=(lastweek-firstweek)//voclen+1
# Week number is nweeks-1-(lastweek-day)//voclen
if locationsize=="region":
reduceltla=ltla2region
reducesgtf=sgtf2region
elif locationsize=="country":
reduceltla=ltla2country
reducesgtf=sgtf2country
else:
raise RuntimeError("Incompatible source, locationsize combination: "+source+", "+locationsize)
# Get SGTF data into a suitable form
vocnum={}
background=[0,0]
for (date,region,var,n) in zip(sgtf['specimen_date'],sgtf['UKHSA_region'],sgtf['sgtf'],sgtf['n']):
day=datetoday(date)
week=nweeks-1-(lastweek-day)//voclen
if week>=0 and week<nweeks:
place=reducesgtf(region)
if place not in vocnum: vocnum[place]=np.zeros([nweeks,2],dtype=int)
vocnum[place][week][int("SGTF" in var)]+=n
if date>=Date('2021-10-01') and date<Date('2021-11-10'):
background[int("SGTF" in var)]+=n
# Adjust for non-Omicron SGTFs, based on the assumption that these are in a non-location-dependent proportion to the number of non-Omicron cases
f=background[1]/background[0]
for place in vocnum:
for week in range(nweeks):
vocnum[place][week][1]=max(vocnum[place][week][1]-int(f*vocnum[place][week][0]+.5),0)
else:
raise RuntimeError("Unrecognised source: "+source)
fullsource+='; https://coronavirus.data.gov.uk/, last specimen date '+daytodate(maxday)
# specadj[d] = chance that a specimen from minday+d has been reported by now
ex=getextrap(publishedday)
n=len(ex)-discardcasedays
specadj=ex[n-ndays:n]
specadj_engregion={'England': specadj}
print("Incomplete specimen cases correction factors:")
for reg in sorted(list({ltla2region[x] for x in ltla2region if x[0]=='E'})):
ex=getextrap(publishedday,location=reg)
n=len(ex)-discardcasedays
specadj_engregion[reg]=ex[n-ndays:n]
print("%-27s"%reg,specadj_engregion[reg][-10:])
# Pro tem using English spec adj for these nif corrections (can't believe it's going to matter much)
nif1a=nif1*specadj
lognif1a=np.log(nif1a)
log1mnif1a=np.log(1-nif1a)
# Simple weekday adjustment by dividing by the average count for that day of the week.
# Use a relatively stable period (inclusive) over which to take the weekday averages.
weekadjdates=[datetoday('2021-09-20'),datetoday('2021-11-21')]
weekadj=np.zeros(7)
for (date,n) in zip(apicases['date'],apicases['newCasesBySpecimenDate']):
day=datetoday(date)
if day>=weekadjdates[0] and day<=weekadjdates[1]: weekadj[day%7]+=n
weekadjp=weekadj*7/sum(weekadj)
if reduceltla!=None:
# Get case data into a suitable form
preweek=minday+9# Gather pre-variant counts in two one-week periods up to this date
precases0={}
cases={}
for (ltla,date,n) in zip(apicases['areaCode'],apicases['date'],apicases['newCasesBySpecimenDate']):
if ltla not in reduceltla or ltla in ltlaexclude or not includeltla(ltla,ltlaset): continue
day=datetoday(date)
d=day-minday
place=reduceltla[ltla]
if place not in vocnum: continue
if ltla[0]=='E': reg=ltla2region[ltla]
else: reg='England'# Pro tem use English specimen adjustment for non-English ltlas
if place not in precases0: precases0[place]=np.zeros(2,dtype=int)
if place not in cases: cases[place]=np.zeros(ndays)
for i in range(2):
if day>preweek-7*(2-i) and day<=preweek-7*(1-i):
precases0[place][i]+=n
if d>=0 and d<ndays:
cases[place][d]+=n/specadj_engregion[reg][d]/weekadjp[day%7]
places=sorted(list(cases))
else:
places=sorted(list(vocnum))
# Restrict to places for which there is at least some of each variant, and bundle the remaining locations together as "Other"
okplaces=set([place for place in places if vocnum[place][:,0].sum()>0 and vocnum[place][:,1].sum()>0])
#okplaces=set(places)
if bundleremainder:
otherplaces=set(places).difference(okplaces)
othervocnum=sum((vocnum[place] for place in otherplaces),np.zeros([nweeks,2],dtype=int))
othercases=sum((cases[place] for place in otherplaces),np.zeros(ndays,dtype=int))
if othervocnum[:,0].sum()>0 and othervocnum[:,1].sum()>0:
okplaces.add("Other")
vocnum["Other"]=othervocnum
cases["Other"]=othercases
places=list(okplaces)
places.sort()# Alphabetical order
if 0:
vdir='tempd'
for place in places:
with open(os.path.join(vdir,place.replace(' ','_')),'w') as fp:
for w in range(nweeks):
print(daytodate(firstweek+voclen*w),"%6d %6d"%tuple(vocnum[place][w]),file=fp)
# Work out pre-variant case counts, amalgamated to at least region level
if reduceltla!=None:
precases={}
for place in precases0:
if bundleremainder and place in otherplaces:
dest="Other"
elif locationsize=="LTLA": dest=ltla2region[place]
else: dest=place
if dest not in precases: precases[dest]=np.zeros(2,dtype=int)
precases[dest]+=precases0[place]
def prereduce(place):
if place!="Other" and locationsize=="LTLA": return ltla2region[place]
else: return place
# Convert daily growth rate & uncertainty into R-number-based description
# dh = 1 standard deviation
def Rdesc(h0,dh):
(Tmin,T,Tmax)=[(exp(h*mgt)-1)*100 for h in [h0-zconf*dh,h0,h0+zconf*dh]]
return "%.0f%% (%.0f%% - %.0f%%)"%(T,Tmin,Tmax)
def Ddesc(h0,dh):
(Dmin,D,Dmax)=[log(2)/h for h in [h0+zconf*dh,h0,h0-zconf*dh]]
return "%.2f (%.2f - %.2f)"%(D,Dmin,Dmax)
def Gdesc(h0,dh):
return "%.2f%% (%.2f%% - %.2f%%)"%(h0*100,(h0-zconf*dh)*100,(h0+zconf*dh)*100)
def GDdesc(h0,hlow,hhigh):
hh=(h0,hlow,hhigh)
s="%.2f (%.2f - %.2f) per day"%hh
if abs(h0)>=0.01:
if h0>0: s+=" [doubling time: %.2f (%.2f - %.2f) days]"%tuple(log(2)/h for h in [h0,hhigh,hlow])
elif h0<0: s+=" [halving time: %.2f (%.2f - %.2f) days]"%tuple(-log(2)/h for h in hh)
return s
if plainvarcountest:
print("Estimating competitive advantage using variant counts only (not case counts)")
print("============================================================================")
print()
mincount=1
l=[]
eps=1e-20
for w in range(nweeks-1):
if len(places)<30: print(daytodate(firstweek+w*voclen),end=' ')
for place in places:
vn=vocnum[place]
if (vn[w:w+2,:]>=mincount).all():
g=((vn[w+1][1]+eps)/(vn[w+1][0]+eps))/((vn[w][1]+eps)/(vn[w][0]+eps))
l.append(g)
if len(places)<30:
print(" %6.1f%%"%(log(g)/voclen*100),end='')
else:
if len(places)<30:
print(" ------",end='')
if len(places)<30: print()
l.sort()
n=len(l)
k=int(binom.ppf((1-conf)/2,n,0.5))
med=log((l[n//2]+l[(n-1)//2])/2)/voclen
low=log(l[k-1])/voclen
high=log(l[n-1-k])/voclen
print("Separate location & weeks, unweighted high-low non-parametric test: %.2f%% (%.2f%% - %.2f%%)"%(med*100,low*100,high*100))
print()
l=[]
for w in range(nweeks-1):
for place in places:
vn=vocnum[place]
if (vn[w:w+2,:]>0).all():
wt=sqrt(1/(1/vn[w:w+2,:]).sum())
g=(vn[w+1][1]/vn[w+1][0])/(vn[w][1]/vn[w][0])
l.append((g,wt))
l.sort()
wts=np.array([wt for (g,wt) in l])
n=len(l)
nsamp=int(1e6/len(places))
rand=bernoulli.rvs(0.5,size=[nsamp,n])
samp=rand@wts
samp.sort()
wtlow=samp[int(nsamp*(1-conf)/2)]
wtmed=samp[int(nsamp/2)]
wthigh=samp[int(nsamp*(1+conf)/2)]
wt=0
low=med=high=None
for i in range(n):
wt+=l[i][1]
if low==None and wt>wtlow-1e-6: low=log(l[i][0])/voclen
if med==None and wt>wtmed-1e-6: med=log(l[i][0])/voclen
if high==None and wt>wthigh-1e-6: high=log(l[i][0])/voclen
print("Separate location & weeks, weighted high-low non-parametric test: %.2f%% (%.2f%% - %.2f%%)"%(med*100,low*100,high*100))
print()
for w in range(nweeks-1):
l=[]
for place in places:
vn=vocnum[place]
if (vn[w:w+2,:]>0).all():
wt=sqrt(1/(1/vn[w:w+2,:]).sum())
g=(vn[w+1][1]/vn[w+1][0])/(vn[w][1]/vn[w][0])
l.append((g,wt))
if l==[]: continue
l.sort()
wts=np.array([wt for (g,wt) in l])
n=len(l)
nsamp=int(1e6/len(places))
rand=bernoulli.rvs(0.5,size=[nsamp,n])
samp=rand@wts
samp.sort()
wtlow=samp[int(nsamp*(1-conf)/2)]
wtmed=samp[int(nsamp/2)]
wthigh=samp[int(nsamp*(1+conf)/2)]
wt=0
low=med=high=None
for i in range(n):
wt+=l[i][1]
if low==None and wt>wtlow-1e-6: low=log(l[i][0])/voclen
if med==None and wt>wtmed-1e-6: med=log(l[i][0])/voclen
if high==None and wt>wthigh-1e-6: high=log(l[i][0])/voclen
print(daytodate(firstweek+voclen*w),"Separate locations, weighted high-low non-parametric test: %6.2f%% (%6.2f%% - %6.2f%%)"%(med*100,low*100,high*100))
print()
from scipy.special import betaln
from scipy.integrate import quad
from scipy import inf
def crossratiosubdivide(matgen,duration=voclen):
tot=np.zeros([2,2],dtype=int)
ndiv=20
logp=np.zeros(ndiv)
L0=L1=0
for M in matgen:
tot+=M
if (M>0).all():
c=1/((1/M.flatten()).sum())
T=M[0,0]*M[1,1]/(M[0,1]*M[1,0])
L0+=c*log(T);L1+=c
for i in range(ndiv):
x=(hmin+(i+.5)/ndiv*(hmax-hmin))*duration# Convert to weekly growth rate
a,b,c,d=M[0,0],M[0,1],M[1,0],M[1,1]
l0=d*x-(betaln(a,b)+betaln(c,d))
# Faff around finding maximum to avoid underflow in integral
e=exp(x)
X=b+d-1;Y=a+b;Z=c+d
A=Y+Z-X
B=Y/e+Z-X*(1+1/e)
C=-X/e
z0=(-B+sqrt(B**2-4*A*C))/(2*A)
l1=(b+d-1)*log(z0) - (a+b)*log(1+z0) - (c+d)*log(1+e*z0)
sc=sqrt((b+d-1)/z0**2-(a+b)/(1+z0)**2-(c+d)*e**2/(1+e*z0)**2)# Set inverse length scale
#res=quad(lambda z: exp( (b+d-1)*log(z) - (a+b)*log(1+z) - (c+d)*log(1+e*z) - l1 ), 0, inf)
res=quad(lambda x: exp( (b+d-1)*log(x/sc) - (a+b)*log(1+x/sc) - (c+d)*log(1+e*x/sc) - l1 ), 0, inf)
logp[i]+=log(res[0]/sc)+l0+l1
if (tot==0).any():
print("Can't estimate best transmission factor because VOC count matrix has 1 or more zero entries");return
g=log(tot[0,0]*tot[1,1]/(tot[0,1]*tot[1,0]))/duration
dg=sqrt((1/tot.flatten()).sum())/duration
print("Overall cross ratio:",Gdesc(g,dg),Rdesc(g,dg),tot.flatten())
g=L0/L1/duration
dg=1/sqrt(L1)/duration
print("Inverse variance weighting method using log(CR):",Gdesc(g,dg),Rdesc(g,dg))
i=np.argmax(logp)
if i==0 or i==ndiv-1:
print("Can't properly estimate best transmission factor or confidence interval because the maximum is at the end")
imax=i
c=0.1
else:
b=(logp[i+1]-logp[i-1])/2
c=2*logp[i]-(logp[i+1]+logp[i-1])
imax=i+b/c
irange=1/sqrt(c)
g0=(hmin+(hmax-hmin)*(imax+.5)/ndiv)
dg=(hmax-hmin)*irange/ndiv
print("Likelihood method using log(CR):",Gdesc(g0,dg),Rdesc(g0,dg))
print()
# Simple regression with 1/(1/v0+1/v1) weighting
def simpleregress(NV):
DT=np.array(range(firstweek,firstweek+voclen*nweeks,voclen))
W=NV[:,0]*NV[:,1]/(NV.sum(axis=1)+1e-20)
day0=DT.sum()/nweeks
if (W>0).sum()<=2: return (day0,0,1)
X=DT-day0
Y=np.log((NV[:,1]+1e-20)/(NV[:,0]+1e-20))
m=np.array([[sum(W), sum(W*X)], [sum(W*X), sum(W*X*X)]])
r=np.array([sum(W*Y),sum(W*X*Y)])
c=np.linalg.solve(m,r)
mi=np.linalg.pinv(m)
R=c[0]+c[1]*X-Y
overdis=(R*R*W).sum()/len(R)
dg=sqrt(mi[1,1]*overdis)
return (day0-c[0]/c[1],c[1],dg)
n=1+len(places)
def NLL_vonly(xx):
g=xx[0]
LL=0
for (i,place) in enumerate(places):
nn=vocnum[place]
t0=xx[1+i]
for w in range(nweeks):
G=(w*voclen-t0)*g
LL+=-(nn[w][0]+nn[w][1])*log(1+exp(G))+nn[w][1]*G
return -LL/1000
if voclen>=7:
for w in range(nweeks-1):
day0=lastweek-(nweeks-w)*voclen+1
print(daytodate(day0),"-",daytodate(day0+2*voclen-1))
crossratiosubdivide(vocnum[place][w:w+2] for place in places)
print("All week pairs:")
crossratiosubdivide(vocnum[place][w:w+2] for place in places for w in range(nweeks-1))
print("First week to last week:")
crossratiosubdivide((vocnum[place][0:nweeks:nweeks-1] for place in places), duration=voclen*(nweeks-1))
if len(places)<50:
print("--- Inverse variance weighted regression using combined counts for %s ---"%areacovered)
sr=simpleregress(sum(vocnum.values()))
xx=[sr[1]]# Overall growth
print("%s:"%areacovered,Gdesc(sr[1],sr[2]),Rdesc(sr[1],sr[2])," crossover on",daytodate(sr[0]))
print()
print("--- Inverse variance weighted regression using counts for each %s ---"%locationsize)
s0=s1=0
for place in places:
sr=simpleregress(vocnum[place])
xx.append(sr[0]-firstweek)# Intercept of [place]
print("%25s:"%place,Gdesc(sr[1],sr[2]),Rdesc(sr[1],sr[2])," crossover on",daytodate(sr[0]))
iv=1/sr[2]**2
s0+=iv
s1+=iv*sr[1]
print()
g=s1/s0;dg=sqrt(1/s0)
print("Inverse variance weighted count for %s controlling for %s:"%(areacovered,locationsize),Gdesc(g,dg),Rdesc(g,dg))
print()
bounds=[(hmin,hmax)]+[(x-100,x+100) for x in xx[1:]]
res=minimize(NLL_vonly,xx,bounds=bounds,method=optmethod,options=minopts)
print("--- Quasi-Poisson regression controlling for %s ---"%locationsize)
print("Growth: %.2f%% crossover on"%(res.x[0]*100),daytodate(firstweek+res.x[1]))
print()
print()
if reduceltla==None: sys.exit(0)
print("Estimating competitive advantage using variant counts together with case counts")
print("===============================================================================")
print()
# L=ndays+2*bmsig
# i is time from start, in days
# t=i/L
# growth[i] = bmscale*sqrt(L)*(t*X_0 + sum_{n=1}^{N-1} sqrt(2)/pi*exp(-(n*bmsig/L)^2/2)*sin(n*pi*t)/n*X_n)
# where X_n ~ N(0,1), n=0,...,N; N=ceil(4*L/bmsig), say
bmL=ndays+int(bmsig*2+0.999)# Add on bmsig*2 to eliminate periodicity effects
bmN=int(2.5*bmL/bmsig+1)
bmsin=[sin(r*pi/bmL) for r in range(2*bmL)]
bmweight=[0]+[sqrt(2)/pi*exp(-(n*bmsig/bmL)**2/2)/n for n in range(1,bmN)]
bmsin2=[np.array([bmweight[n]*bmsin[(i*n)%(2*bmL)] for n in range(bmN)]) for i in range(ndays)]
# Need to scale the variables being optimised over to keep SLSQP happy
#condition=np.array([50,50,1000,1000]+[1.]*bmN)
t=40000*np.array(bmweight[1:])*bmscale/np.arange(1,bmN)+1
condition=np.array([70,80,5000,5000]+[1,]*(nfp-4)+[t[0]]+list(t))
# bmN+nfp parameters to be optimised:
# 0: a0
# 1: b0
# 2: h
# 3: g0
# [4...nfp-1: things]
# nfp ... nfp+bmN-1 : X_0, ..., X_{bmN-1}
def expand(xx):
(a0,b0)=xx[:2]
AA=[exp(a0)];BB=[exp(b0)]
h=xx[2]
g0=xx[3]
w=bmscale*sqrt(bmL)
GG=[]
H=exp(h)
for i in range(ndays-1):
t=i/bmL
gu=t*xx[nfp]+np.dot(bmsin2[i],xx[nfp:])
#for n in range(1,bmN):
# gu+=bmweight[n]*bmsin[(i*n)%(2*bmL)]*xx[nfp+n]
g=g0+w*gu
GG.append(g)
G=exp(g)
G2=exp(g/exp(xx[4]))
AA.append(AA[-1]*G)
BB.append(BB[-1]*G2*H)
return AA,BB,GG
# Return negative log likelihood (negative because scipy can only minimise, not maximise)
# If const is true then add in all the constant terms (that don't affect the optimisation)
def NLL(xx_conditioned,lcases,lvocnum,sig0,asc,lprecases,const=False):
xx=xx_conditioned/condition
tot=0
# Prior on starting number of cases of non-variant: assume starts off similar to total number of cases
a0=log(lcases[0]+.5)
tot+=-((xx[0]-a0)*isd0)**2/2
if const: tot-=log(2*pi/isd0**2)/2
# Very weak prior on starting number of cases of variant
tot+=-((xx[1]-(a0-4))*isd1)**2/2
if const: tot-=log(2*pi/isd1**2)/2
# Prior on h
tot+=-(xx[2]*isd2)**2/2
if const: tot-=log(2*pi/isd2**2)/2
a,b=lprecases[0]+.5,lprecases[1]+.5
g0=log(b/a)/7
v0=(1/a+1/b)/49+sig0**2
tot+=-(xx[3]-g0)**2/(2*v0)
if const: tot-=log(2*pi*v0)/2
# Prior on GTR
sd4=.2
tot+=-(xx[4]/sd4)**2/2
if const: tot-=log(2*pi*sd4**2)/2
AA,BB,GG=expand(xx)
# Component of likelihood due to number of confirmed cases seen
for i in range(ndays):
mu=asc*(AA[i]+BB[i])
r=mu*nif1a[i]/(1-nif1a[i])
n=lcases[i]
# n ~ Negative binomial(mean=mu, variance=mu/nif1)
# max with -10000 because the expression is unbounded below which can cause a problem for SLSQP
if model=="scaledpoisson":
tot+=max((-mu+n*log(nif1a[i]*mu))*nif1a[i],-10000)
if const: tot+=log(nif1a[i])-gammaln(nif1a[i]*n+1)# Approx normalisation
elif model=="NBBB":
tot+=max(gammaln(n+r)+r*lognif1a[i]+n*log1mnif1a[i]-gammaln(r),-10000)
if const: tot+=-gammaln(n+1)
elif model=="NBBB+magicprior":
tot+=max(gammaln(n+r)-nif1a[i]*gammaln(mu+r)+n*log1mnif1a[i],-10000)
if const: tot+=-gammaln(n+1)
else: raise RuntimeError("Unrecognised model "+model)
# Term to regulate change in growth rate
for i in range(bmN):
tot+=-xx[nfp+i]**2/2
if const: tot-=bmN*log(2*pi)/2
# Term to align the variant numbers with VOC count data
for w in range(nweeks):
endweek=lastweek-(nweeks-1-w)*voclen-minday
A=sum(AA[endweek-(voclen-1):endweek+1])
B=sum(BB[endweek-(voclen-1):endweek+1])
f=nif2/(1-nif2);a=f*A;b=f*B
r,s=lvocnum[w][0],lvocnum[w][1]
if model=="scaledpoisson":
r1,s1=nif2*r,nif2*s
tot+=r1*log(A/(A+B))+s1*log(B/(A+B))
if const: tot+=gammaln(r1+s1+1)-gammaln(r1+1)-gammaln(s1+1)+log(nif2)
elif model=="NBBB" or model=="NBBB+magicprior":
if abs(a+b)<10000*(r+s):
tot+=gammaln(a+r)+gammaln(b+s)-gammaln(a+b+r+s)+gammaln(a+b)-gammaln(a)-gammaln(b)
else:
tot+=r*log(A/(A+B))+s*log(B/(A+B))
if const: tot+=gammaln(r+s+1)-gammaln(r+1)-gammaln(s+1)
else: raise RuntimeError("Unrecognised model "+model)
return -tot
def Hessian(xx,lcases,lvocnum,sig0,asc,lprecases):
N=bmN+nfp
eps=1e-3
H=np.zeros([N,N])
for i in range(N-1):
for j in range(i+1,N):
v=0
eps1=eps/condition[i]
eps2=eps/condition[j]
for (s1,s2) in [(-1,-1),(-1,1),(1,-1),(1,1)]:
x=np.copy(xx)
x[i]+=s1*eps1
x[j]+=s2*eps2
v+=s1*s2*NLL(x*condition,lcases,lvocnum,sig0,asc,lprecases)
e=v/(4*eps1*eps2)
H[i,j]=e
H[j,i]=e
for i in range(N):
x=np.copy(xx)
v=0
eps1=eps/condition[i]
for s in [-1,0,1]:
x=np.copy(xx)
x[i]+=s*eps1
v+=(s*s*3-2)*NLL(x*condition,cases[place],vocnum[place],sig0,asc,precases[prereduce(place)])
H[i,i]=v/eps1**2
return H
# Returns log likelihood
def optimiseplace(place,hint=np.zeros(bmN+nfp),fixedh=None,fixedgtr=None,statphase=False):
xx=np.copy(hint)
# bounds[2][0]=0 prejudges new variant as being at least as transmissible as old variant. This helps SLSQP not get stuck in some cases
# though would need to relax this constraint if dealing with other variants where it might not be true.
bounds=[(-10,20),(-10,20),(0,1),(-1,1)]+[(-1.5,1.5)]*(nfp-4)+[(-10,10)]*bmN
if fixedh!=None: xx[2]=fixedh;bounds[2]=(fixedh,fixedh)
if fixedgtr!=None: xx[4]=fixedgtr;bounds[4]=(fixedgtr,fixedgtr)
res=minimize(NLL,xx*condition,args=(cases[place],vocnum[place],sig0,asc,precases[prereduce(place)]),bounds=bounds*np.repeat(condition,2).reshape([len(bounds),2]),method=optmethod,options=minopts)
if not res.success:
print(res)
print(fixedh)
print(place)
print("xx =",xx)
print("bounds =",bounds)
print("lcases =",list(cases[place]))
print("lprecases =",precases[prereduce(place)])
print("lvocnum =",vocnum[place])
for x in sorted(list(opts)): print("%s:"%x,opts[x])
print("nweeks, ndays, minday, lastweek =",nweeks,",",ndays,",",minday,",",lastweek)
raise RuntimeError(res.message)
xx=res.x/condition
# Work out log likelihood including constant terms
LL=-NLL(res.x,cases[place],vocnum[place],sig0,asc,precases[prereduce(place)],const=True)
# If 'statphase', make the log likelihood a better approximation to log(integral over all parameters) using stationary phase approximation
if statphase:
H=Hessian(xx,cases[place],vocnum[place],sig0,asc,precases[prereduce(place)])
det=np.linalg.det(H)
N=H.shape[0]
if det<=0: print("Warning: Hessian not positive for %s. Can't make corrected log likelihood."%place);det=1
LL+=N*log(2*pi)/2-log(det)/2
#pdb.set_trace()
# Return optimum xx log likelihood
return res.x/condition,LL
def getsamples(place,xx0):
H=Hessian(xx0,cases[place],vocnum[place],sig0,asc,precases[prereduce(place)])
Hcond=H/condition/condition[:,None]
eig=np.linalg.eigh(Hcond)
# np.diag(np.matmul(np.matmul(np.transpose(eig[1]),Hcond),eig[1])) ~= eig[0]
if not (eig[0]>0).all(): print("Hessian not +ve definite so can't do full confidence calculation");return None
nsamp=10000
N=bmN+nfp
t=norm.rvs(size=[nsamp,N])# nsamp x N
sd=eig[0]**(-.5)# N
u=t*sd# nsamp x N
samp_cond=np.matmul(u,np.transpose(eig[1]))# nsamp x N
samp=samp_cond/condition
SSS=np.zeros([nsamp,2,ndays])
for i in range(nsamp):
xx=xx0+samp[i]
AA,BB,GG=expand(xx)
SSS[i,0,:]=AA
SSS[i,1,:]=BB
return SSS
# Generate sample paths conditional on h = xx0[2] + dhsamp
# xx0 = N-vector that is optimal conditioned on xx0[2]
# dhsamp = nsamp-vector of deltas in xx0[2] to sample over (an externally imposed normal - not from the log likelihood)
# Use normal approximation with Hessian from log likelihood for other co-ordinates
#
# Types in terms of dimensions and conditionedness:
# L^this condition^this
# xx, xx0, dhsamp, samp 1 0
# condition 0 1
# H -2 0
# Hcond, Hcond_, Hcond__ -2 -2
# eig[0] -2 -2
# eig[1], t, s1 0 0
# sd, u, s0 1 1
def getcondsamples(place,xx0,dhsamp):
N=bmN+nfp
H=Hessian(xx0,cases[place],vocnum[place],sig0,asc,precases[prereduce(place)])
Hcond=H/condition/condition[:,None]
Hcond__=np.delete(np.delete(Hcond,2,0),2,1)# N-1 x N-1
Hcond_=np.delete(Hcond[2],2,0)# N-1
eig=np.linalg.eigh(Hcond__)
# np.matmul(np.matmul(np.transpose(eig[1]),Hcond__),eig[1]) ~= np.diag(eig[0])
m=eig[0].min()
if m<=0: print("Hessian not +ve definite in getcondsamples so can't do full confidence calculation");return None
if m<1e-6: print("Warning: Hessian has a very low eigenvalue in getcondsamples:",m)
nsamp=len(dhsamp)
t=norm.rvs(size=[nsamp,N-1])# nsamp x N-1
sd=eig[0]**(-.5)# N-1
u=t*sd# nsamp x N-1
s0=np.insert(np.matmul(u,np.transpose(eig[1])),2,0,1)# nsamp x N
s1=np.insert(-np.linalg.solve(Hcond__,Hcond_),2,1,0)# N
samp=s0/condition+dhsamp[:,None]*s1# nsamp x N
SSS=np.zeros([nsamp,2,ndays])
# This is the slow bit.
for i in range(nsamp):
xx=xx0+samp[i]
AA,BB,GG=expand(xx)
SSS[i,0,:]=AA
SSS[i,1,:]=BB
return SSS
def printplaceinfo(place,using=''):
name=ltla2name.get(place,place)+using
print()
print(name)
print("="*len(name))
print()
print(" Nonvar Var Seen")
for w in range(nweeks):
day0,day1=lastweek-(nweeks-w)*voclen+1,lastweek-(nweeks-1-w)*voclen
print(daytodate(day0),"-",daytodate(day1),"%6d %6d %6.0f"%(vocnum[place][w][0],vocnum[place][w][1],sum(cases[place][day0-minday:day1-minday+1])))
print()
def fullprint(AA,BB,lvocnum,lcases,T=None,Tmin=None,Tmax=None,area=None,using='',samples=None, gtr=None):
print("ModV1 = modelled number of new cases of "+nonvariant+" on this day multiplied by the ascertainment rate")
print("ModV2 = modelled number of new cases of "+variant+" on this day multiplied by the ascertainment rate")
print("Pred = predicted number of cases seen this day = ModV1+ModV2")
print("Seen = number of cases observed this day, after weekday adjustment, from api/dashboard")
print("PredV1 = p*Pred, where p = proportion of "+nonvariant+" amongst observed variant counts from",source)
print("PredV2 = (1-p)*Pred")
print("SeenV1 = p*Seen")
print("SeenV2 = (1-p)*Seen")
print("Q = estimated reproduction rate of "+nonvariant+" on this day")
print("R = estimated reproduction rate of "+variant+" on this day")
print("ModV1min = ModV1 min confidence interval")
print("ModV1med = ModV1 mode confidence interval")
print("ModV1max = ModV1 max confidence interval")
print("ModV2min = ModV1 min confidence interval")
print("ModV2med = ModV1 mode confidence interval")
print("ModV2max = ModV1 max confidence interval")
print("Qmin = Q min confidence interval")
print("Qmed = Q mode confidence interval")
print("Qmax = Q max confidence interval")
print("Rmin = R min confidence interval")
print("Rmed = R mode confidence interval")
print("Rmax = R max confidence interval")