Commit 74c37614 authored by Ondrej Zika's avatar Ondrej Zika
Browse files

updated

parent 6f758d0f
%% Cell type:markdown id: tags:
<h1>Table of Contents<span class="tocSkip"></span></h1>
<div class="toc"><ul class="toc-item"></ul></div>
%% Cell type:code id: tags:
``` python
import pandas as pd
import numpy as np
from datetime import datetime, date, timedelta
import plotly.graph_objects as go
from scipy.stats import norm
import plotly.graph_objects as go
import numpy.matlib
```
%% Cell type:code id: tags:
``` python
# Collection timing
sdate = date(2020, 1, 1) # start date
edate = date(2021, 3, 1) # end date
delta = edate - sdate # as timedelta
days = np.empty([0])
for i in range(delta.days + 1):
days = np.append(days, (sdate + timedelta(days=i)).strftime("%Y-%m-%d"))
fig = go.Figure()
# Calculate number of cases
ndays =len(days)
gwidth = 160
gap = 80
xvals = np.arange(gwidth)
xvals2 = np.arange(gwidth)+240
yvals = norm.pdf(xvals, gwidth/2, 23)
yvals = yvals/np.sum(yvals)
yvals2 = norm.pdf(xvals, gwidth/2, 23)
yvals2 = yvals2/np.sum(yvals2)
xarr = np.arange(ndays)
yarr = np.zeros(ndays)
yarr[xvals] = yvals
yarr[xvals2] = yvals2
fig.add_trace(go.Scatter(x=days, y=yarr,
mode='lines',
name='Profile of expected COVID-19 cases',
line=dict(color='rgb(170, 170, 170)', width=3) ))
# Sampling times - in days since Jan 1st 2020
gaps = [90, 21, 21, 21, 40, 40, 40, 14, 14, 14, 40, 40]
sample_times = np.cumsum(gaps)
yv = [0, 0.0015]
i = True
for s in sample_times:
fig.add_trace(go.Scatter(x=[days[s], days[s]], y=yv,
mode='lines',
name='Proposed survey dates',
line=dict(color='rgb(20, 20, 140)', width=4),
showlegend=i))
i = False
fig.update_layout(template="plotly_white", width=800, height=400)
fig.update_yaxes(tickvals=[])
fig.update_layout(legend=dict(
x=0.32,
y=1.1))
fig.show()
fig.write_image("sampling.png")
```
%% Output
%% Cell type:code id: tags:
``` python
# Collection timing
sdate = date(2020, 1, 1) # start date
edate = date(2021, 3, 1) # end date
delta = edate - sdate # as timedelta
days = np.empty([0])
for i in range(delta.days + 1):
days = np.append(days, (sdate + timedelta(days=i)).strftime("%Y-%m-%d"))
fig = go.Figure()
# Calculate number of cases
ndays =len(days)
gwidth = 160
gap = 80
xvals = np.arange(gwidth)
xvals2 = np.arange(gwidth)+240
yvals = norm.pdf(xvals, gwidth/2, 23)
yvals = yvals/np.sum(yvals)/3
yvals2 = norm.pdf(xvals, gwidth/2, 23)
yvals2 = yvals2/np.sum(yvals2)/3
xarr = np.arange(ndays)
yarr = np.zeros(ndays)
yarr[xvals] = yvals
yarr[xvals2] = yvals2
fig.add_trace(go.Scatter(x=days, y=yarr,
mode='lines',
name='Profile of expected COVID-19 cases (Anderson et al. 2020)',
line=dict(color='rgb(170, 170, 170)', width=3),
showlegend=True ))
# Sampling times - in days since Jan 1st 2020
gaps = [90, 21, 21, 21, 40, 40, 40, 14, 14, 14, 40, 40]
sample_times = np.cumsum(gaps)
yv = [0, 0.0015]
i = False
#for s in sample_times:
# fig.add_trace(go.Scatter(x=[days[s], days[s]], y=yv,
# mode='lines',
# name='Proposed survey dates',
# line=dict(color='rgb(20, 20, 140)', width=4),
# showlegend=i))
# i = False
# Expected fear trajectory
### show 'expected fear'
yf = np.zeros(ndays)
y1 = norm.pdf(xarr[0:66], gwidth/2, 17)
#y1 = y1/np.sum(y1)
yf[xarr[0:66]] = y1
n2 = 40
y2 = np.matlib.repmat(y1[-1] , 1, n2)
yf[xarr[66:66+n2]] =y2
cn = 66+n2
y3= np.array([y1[-1]])
n3 = 110
decay = 0.97
for i in np.arange(1,n3+1):
y3= np.append(y3, y3[i-1]*decay)
y3 = y3[:-1]
yf[xarr[cn:cn+n3]] =y3
cn = cn+n3
n4=80
y4 = norm.pdf(xarr[0:n4], gwidth/2, 12)/2
#y1 = y1/np.sum(y1)
yf[xarr[cn:cn+n4]] = y4
cn = cn+n4
y4b = norm.pdf(xarr[0:n4*2], gwidth/2, 12)/2
y4b = y4b[n4:]
yf[xarr[cn+30:cn+30+n4]] = y4b
yf[xarr[cn:cn+30]] = np.matlib.repmat(y4[-1],1,30)
#for j in range(20):
for i in range(len(yf)):
yf[i] = yf[i] + (np.random.normal()/800)
yf = np.convolve(yf, np.ones((20,))/20, mode='valid') + 0.001
yf = np.concatenate((np.matlib.repmat(yf[0], 1, 20)[0], yf))
fig.add_trace(go.Scatter(x=days, y=yf+0.005,
mode='lines',
name='Fear: Low Anxiety',
name='Low Anxiety',
line=dict(color='rgb(144, 1, 69)', width=3) ))
yf = np.zeros(ndays)
y1 = norm.pdf(xarr[0:66], gwidth/2, 17)
#y1 = y1/np.sum(y1)
yf[xarr[0:66]] = y1
n2 = 40
y2 = np.matlib.repmat(y1[-1] , 1, n2)
yf[xarr[66:66+n2]] =y2
cn = 66+n2
y3= np.array([y1[-1]])
n3 = 90
decay = 0.94
for i in np.arange(1,n3+1):
y3= np.append(y3, y3[i-1]*decay)
y3 = y3[:-1]
yf[xarr[cn:cn+n3]] =y3
cn = cn+n3
n4=80
y4 = norm.pdf(xarr[0:n4], gwidth/2, 1.6)/12
#y1 = y1/np.sum(y1)
yf[xarr[cn:cn+n4]] = y4
cn = cn+n4
y4b = norm.pdf(xarr[0:n4*2], gwidth/2,8)/3
y4b = y4b[n4:]
add= 50
yf[xarr[cn+add:cn+add+n4]] = y4b
yf[xarr[cn:cn+add]] = np.matlib.repmat(y4[-1],1,add)
#for j in range(20):
for i in range(len(yf)):
yf[i] = yf[i] + (np.random.normal()/800)
yf = np.convolve(yf, np.ones((20,))/20, mode='valid') + 0.001
yf = np.concatenate((np.matlib.repmat(yf[0], 1, 20)[0], yf))
fig.add_trace(go.Scatter(x=days, y=yf+0.005,
mode='lines',
name='Fear: High Anxiety',
name='High Anxiety',
line=dict(color='rgb(246, 103, 171)', width=3) ))
# add shapes
# Add shapes
w1_st = sdate = date(2020, 4, 15)
w1_end = sdate = date(2020, 7, 30)
y1_1 = 0.005
y1_2 = 0.016
fig.add_trace(go.Scatter(
mode = 'lines',
line=dict(color='rgb(100, 100, 230)', width=3),
x=[w1_st, w1_end, w1_end, w1_st, w1_st],
y=[y1_1, y1_1, y1_2, y1_2, y1_1],
showlegend=False # this sets its legend entry
))
w1_st = sdate = date(2020, 10, 1)
w1_end = sdate = date(2020, 11, 15)
y1_1 = 0.007
y1_2 = 0.025
fig.add_trace(go.Scatter(
mode = 'lines',
line=dict(color='rgb(100, 100, 230)', width=3),
x=[w1_st, w1_end, w1_end, w1_st, w1_st],
y=[y1_1, y1_1, y1_2, y1_2, y1_1],
showlegend=False # this sets its legend entry
))
fig.update_layout(template="plotly_white", width=800, height=400)
fig.update_yaxes(tickvals=[])
fig.update_layout(legend=dict(
x=0.25,
y=1.3))
fig.show()
fig.write_image("fear.png")
```
%% Output
%% Cell type:code id: tags:
``` python
n =12
bp = 0.1666*9
bonus = 5
final_bonus = 10
# attrition rate
N = np.array([1000])
print(N)
decay = 0.86
t_arr = np.array([str(N)])
for i in np.arange(1,n+1):
t_arr= np.append(t_arr, str(round(N[i-1]*decay)))
N= np.append(N, N[i-1]*decay)
xvals = np.arange(1,n+1)
bpvec = np.matlib.repmat(bp, 1, n)[0]
bonvec = np.matlib.repmat(0, 1, n)[0]
bonvec2 = np.matlib.repmat(0, 1, n)[0]
bonvec[np.array([2,5,8])] = bonus
bonvec2[np.array([n-1])] = final_bonus
fig = go.Figure(data=[
go.Bar(name='Base Payment', x=xvals, y=bpvec),
go.Bar(name='Bonus', x=xvals, y=bonvec),
go.Bar(name='Final Bonus', x=xvals, y=bonvec2)
])
fig.add_trace(go.Scatter(x=xvals, y=N/100,
mode='lines+text',
name='Expected participant N decay',
line=dict(color='rgb(150, 110, 150)', width=2),
text=t_arr,
textposition="top right" ))
# Change the bar mode
fig.update_layout(barmode='stack',
yaxis=dict(
title='Payment',
),
xaxis=dict(
title='Visit',
)
)
fig.show()
fig.write_image("payment.png")
N2 = N[:-1].copy()
npar = 1000;
#dropout
print('Payment pp: '+str(np.sum(np.concatenate((bpvec, bonvec, bonvec2)))))
print('Payment overall on-line only sample: '+str(np.sum(np.concatenate((bpvec*N2, bonvec*N2, bonvec2*N2)))))
print('Starting N participants: '+str(npar))
print('personally-recruited people:')
npers = 100
print('Payment pp: '+str(np.sum(np.concatenate((bpvec, bonvec, bonvec2)))))
print('Payment overall local sample: '+str(np.sum(np.concatenate((bpvec*npers, bonvec*npers, bonvec2*npers)))))
print('Overall participant cost: ' +str(np.sum(np.concatenate((bpvec*N2, bonvec*N2, bonvec2*N2)))+np.sum(np.concatenate((bpvec*npers, bonvec*npers, bonvec2*npers)))))
```
%% Output
[1000]
Payment pp: 42.9928
Payment overall on-line only sample: 18406.46282461475
Starting N participants: 1000
personally-recruited people:
Payment pp: 42.9928
Payment overall local sample: 4299.280000000001
Overall participant cost: 22705.742824614754
%% Cell type:code id: tags:
``` python
import sys
sys.path.append("/home/ondrej/tools/python/my_fncs/")
import my_plotting as mp
x = arange(5)
y1 = np.rand(5)
y2 = y1+1
color = 'rgb(20, 20, 230)'
#shade_area_bet_curves(x, y1, y2, color, op=0.2)
```
%% Output
hi
%% Cell type:code id: tags:
``` python
```
%% Output
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-334-4df4a069db43> in <module>
15
16 # Add shapes
---> 17 fig.add_shape(
18 # unfilled Rectangle
19 type="rect",
AttributeError: 'Figure' object has no attribute 'add_shape'
......
docs/fear.png

44.3 KB | W: | H:

docs/fear.png

42.6 KB | W: | H:

docs/fear.png
docs/fear.png
docs/fear.png
docs/fear.png
  • 2-up
  • Swipe
  • Onion skin
%% Cell type:markdown id: tags:
<h1>Table of Contents<span class="tocSkip"></span></h1>
<div class="toc"><ul class="toc-item"></ul></div>
%% Cell type:code id: tags:
``` python
import pandas as pd
import numpy as np
from datetime import datetime, date, timedelta
import plotly.graph_objects as go
from scipy.stats import norm
import plotly.graph_objects as go
import numpy.matlib
```
%% Cell type:code id: tags:
``` python
# Collection timing
sdate = date(2020, 1, 1) # start date
edate = date(2021, 3, 1) # end date
delta = edate - sdate # as timedelta
days = np.empty([0])
for i in range(delta.days + 1):
days = np.append(days, (sdate + timedelta(days=i)).strftime("%Y-%m-%d"))
fig = go.Figure()
# Calculate number of cases
ndays =len(days)
gwidth = 160
gap = 80
xvals = np.arange(gwidth)
xvals2 = np.arange(gwidth)+240
yvals = norm.pdf(xvals, gwidth/2, 23)
yvals = yvals/np.sum(yvals)
yvals2 = norm.pdf(xvals, gwidth/2, 23)
yvals2 = yvals2/np.sum(yvals2)
xarr = np.arange(ndays)
yarr = np.zeros(ndays)
yarr[xvals] = yvals
yarr[xvals2] = yvals2
fig.add_trace(go.Scatter(x=days, y=yarr,
mode='lines',
name='Profile of expected COVID-19 cases',
line=dict(color='rgb(170, 170, 170)', width=3) ))
# Sampling times - in days since Jan 1st 2020
gaps = [90, 21, 21, 21, 40, 40, 40, 14, 14, 14, 40, 40]
sample_times = np.cumsum(gaps)
yv = [0, 0.0015]
i = True
for s in sample_times:
fig.add_trace(go.Scatter(x=[days[s], days[s]], y=yv,
mode='lines',
name='Proposed survey dates',
line=dict(color='rgb(20, 20, 140)', width=4),
showlegend=i))
i = False
fig.update_layout(template="plotly_white", width=800, height=400)
fig.update_yaxes(tickvals=[])
fig.update_layout(legend=dict(
x=0.32,
y=1.1))
fig.show()
fig.write_image("sampling.png")
```
%% Output
%% Cell type:code id: tags:
``` python
# Collection timing
sdate = date(2020, 1, 1) # start date
edate = date(2021, 3, 1) # end date
delta = edate - sdate # as timedelta
days = np.empty([0])
for i in range(delta.days + 1):
days = np.append(days, (sdate + timedelta(days=i)).strftime("%Y-%m-%d"))
fig = go.Figure()
# Calculate number of cases
ndays =len(days)
gwidth = 160
gap = 80
xvals = np.arange(gwidth)
xvals2 = np.arange(gwidth)+240
yvals = norm.pdf(xvals, gwidth/2, 23)
yvals = yvals/np.sum(yvals)/3
yvals2 = norm.pdf(xvals, gwidth/2, 23)
yvals2 = yvals2/np.sum(yvals2)/3
xarr = np.arange(ndays)
yarr = np.zeros(ndays)
yarr[xvals] = yvals
yarr[xvals2] = yvals2
fig.add_trace(go.Scatter(x=days, y=yarr,
mode='lines',
name='Profile of expected COVID-19 cases (Anderson et al. 2020)',
line=dict(color='rgb(170, 170, 170)', width=3),
showlegend=True ))
# Sampling times - in days since Jan 1st 2020
gaps = [90, 21, 21, 21, 40, 40, 40, 14, 14, 14, 40, 40]
sample_times = np.cumsum(gaps)
yv = [0, 0.0015]
i = False
#for s in sample_times:
# fig.add_trace(go.Scatter(x=[days[s], days[s]], y=yv,
# mode='lines',
# name='Proposed survey dates',
# line=dict(color='rgb(20, 20, 140)', width=4),
# showlegend=i))
# i = False
# Expected fear trajectory
### show 'expected fear'
yf = np.zeros(ndays)
y1 = norm.pdf(xarr[0:66], gwidth/2, 17)
#y1 = y1/np.sum(y1)
yf[xarr[0:66]] = y1
n2 = 40
y2 = np.matlib.repmat(y1[-1] , 1, n2)
yf[xarr[66:66+n2]] =y2
cn = 66+n2
y3= np.array([y1[-1]])
n3 = 110
decay = 0.97
for i in np.arange(1,n3+1):
y3= np.append(y3, y3[i-1]*decay)
y3 = y3[:-1]
yf[xarr[cn:cn+n3]] =y3
cn = cn+n3
n4=80
y4 = norm.pdf(xarr[0:n4], gwidth/2, 12)/2
#y1 = y1/np.sum(y1)
yf[xarr[cn:cn+n4]] = y4
cn = cn+n4
y4b = norm.pdf(xarr[0:n4*2], gwidth/2, 12)/2
y4b = y4b[n4:]
yf[xarr[cn+30:cn+30+n4]] = y4b
yf[xarr[cn:cn+30]] = np.matlib.repmat(y4[-1],1,30)
#for j in range(20):
for i in range(len(yf)):
yf[i] = yf[i] + (np.random.normal()/800)
yf = np.convolve(yf, np.ones((20,))/20, mode='valid') + 0.001
yf = np.concatenate((np.matlib.repmat(yf[0], 1, 20)[0], yf))
fig.add_trace(go.Scatter(x=days, y=yf+0.005,
mode='lines',
name='Fear: Low Anxiety',
name='Low Anxiety',
line=dict(color='rgb(144, 1, 69)', width=3) ))
yf = np.zeros(ndays)
y1 = norm.pdf(xarr[0:66], gwidth/2, 17)
#y1 = y1/np.sum(y1)
yf[xarr[0:66]] = y1
n2 = 40
y2 = np.matlib.repmat(y1[-1] , 1, n2)
yf[xarr[66:66+n2]] =y2
cn = 66+n2
y3= np.array([y1[-1]])
n3 = 90
decay = 0.94
for i in np.arange(1,n3+1):
y3= np.append(y3, y3[i-1]*decay)
y3 = y3[:-1]
yf[xarr[cn:cn+n3]] =y3
cn = cn+n3
n4=80
y4 = norm.pdf(xarr[0:n4], gwidth/2, 1.6)/12
#y1 = y1/np.sum(y1)
yf[xarr[cn:cn+n4]] = y4
cn = cn+n4
y4b = norm.pdf(xarr[0:n4*2], gwidth/2,8)/3
y4b = y4b[n4:]
add= 50
yf[xarr[cn+add:cn+add+n4]] = y4b
yf[xarr[cn:cn+add]] = np.matlib.repmat(y4[-1],1,add)
#for j in range(20):
for i in range(len(yf)):
yf[i] = yf[i] + (np.random.normal()/800)
yf = np.convolve(yf, np.ones((20,))/20, mode='valid') + 0.001
yf = np.concatenate((np.matlib.repmat(yf[0], 1, 20)[0], yf))
fig.add_trace(go.Scatter(x=days, y=yf+0.005,
mode='lines',
name='Fear: High Anxiety',
name='High Anxiety',
line=dict(color='rgb(246, 103, 171)', width=3) ))
# add shapes
# Add shapes
w1_st = sdate = date(2020, 4, 15)
w1_end = sdate = date(2020, 7, 30)
y1_1 = 0.005
y1_2 = 0.016
fig.add_trace(go.Scatter(
mode = 'lines',
line=dict(color='rgb(100, 100, 230)', width=3),
x=[w1_st, w1_end, w1_end, w1_st, w1_st],
y=[y1_1, y1_1, y1_2, y1_2, y1_1],
showlegend=False # this sets its legend entry
))
w1_st = sdate = date(2020, 10, 1)
w1_end = sdate = date(2020, 11, 15)
y1_1 = 0.007
y1_2 = 0.025
fig.add_trace(go.Scatter(
mode = 'lines',
line=dict(color='rgb(100, 100, 230)', width=3),
x=[w1_st, w1_end, w1_end, w1_st, w1_st],
y=[y1_1, y1_1, y1_2, y1_2, y1_1],
showlegend=False # this sets its legend entry
))
fig.update_layout(template="plotly_white", width=800, height=400)
fig.update_yaxes(tickvals=[])
fig.update_layout(legend=dict(
x=0.25,
y=1.3))
fig.show()
fig.write_image("fear.png")
```
%% Output
%% Cell type:code id: tags:
``` python
n =12
bp = 0.1666*9
bonus = 5
final_bonus = 10
# attrition rate
N = np.array([1000])
print(N)
decay = 0.86
t_arr = np.array([str(N)])
for i in np.arange(1,n+1):
t_arr= np.append(t_arr, str(round(N[i-1]*decay)))
N= np.append(N, N[i-1]*decay)
xvals = np.arange(1,n+1)
bpvec = np.matlib.repmat(bp, 1, n)[0]
bonvec = np.matlib.repmat(0, 1, n)[0]
bonvec2 = np.matlib.repmat(0, 1, n)[0]
bonvec[np.array([2,5,8])] = bonus
bonvec2[np.array([n-1])] = final_bonus
fig = go.Figure(data=[
go.Bar(name='Base Payment', x=xvals, y=bpvec),
go.Bar(name='Bonus', x=xvals, y=bonvec),
go.Bar(name='Final Bonus', x=xvals, y=bonvec2)
])
fig.add_trace(go.Scatter(x=xvals, y=N/100,
mode='lines+text',
name='Expected participant N decay',
line=dict(color='rgb(150, 110, 150)', width=2),
text=t_arr,
textposition="top right" ))
# Change the bar mode
fig.update_layout(barmode='stack',
yaxis=dict(
title='Payment',
),
xaxis=dict(
title='Visit',
)
)
fig.show()
fig.write_image("payment.png")
N2 = N[:-1].copy()
npar = 1000;
#dropout
print('Payment pp: '+str(np.sum(np.concatenate((bpvec, bonvec, bonvec2)))))
print('Payment overall on-line only sample: '+str(np.sum(np.concatenate((bpvec*N2, bonvec*N2, bonvec2*N2)))))
print('Starting N participants: '+str(npar))
print('personally-recruited people:')
npers = 100
print('Payment pp: '+str(np.sum(np.concatenate((bpvec, bonvec, bonvec2)))))
print('Payment overall local sample: '+str(np.sum(np.concatenate((bpvec*npers, bonvec*npers, bonvec2*npers)))))
print('Overall participant cost: ' +str(np.sum(np.concatenate((bpvec*N2, bonvec*N2, bonvec2*N2)))+np.sum(np.concatenate((bpvec*npers, bonvec*npers, bonvec2*npers)))))
```
%% Output
[1000]
Payment pp: 42.9928
Payment overall on-line only sample: 18406.46282461475
Starting N participants: 1000
personally-recruited people:
Payment pp: 42.9928
Payment overall local sample: 4299.280000000001
Overall participant cost: 22705.742824614754
%% Cell type:code id: tags:
``` python
import sys
sys.path.append("/home/ondrej/tools/python/my_fncs/")
import my_plotting as mp
x = arange(5)
y1 = np.rand(5)
y2 = y1+1
color = 'rgb(20, 20, 230)'
#shade_area_bet_curves(x, y1, y2, color, op=0.2)
```
%% Output
hi
%% Cell type:code id: tags:
``` python
```
%% Output
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-334-4df4a069db43> in <module>
15
16 # Add shapes
---> 17 fig.add_shape(
18 # unfilled Rectangle
19 type="rect",
AttributeError: 'Figure' object has no attribute 'add_shape'
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment