# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Experiment Design [2021-09-07 version]

# We are investigating whether and to what extent the privacy concerns of individuals impact the efficacy of contact tracing systems.
#
# ## Prior results
#
# Prior work by Lloyd et al. (2006) shows the connection between network structure and the spread of infections using a Watts-Strogatz model. They note that the surprising result of the Watt-Strogatz model is that a relatively small number of rewired edges has a large (nonlinear) effect on the path lengths between nodes.
#
# Lloyd et al. demonstrate that infected ratio is also nonlinearly increasing in the rewiring rate, with a steep inflection point between .001 and .01. They address precursors to this result in the use of percollation theory [cite] to explain epidemic sizes, though their discussion is largely about the difference between network epidemics and those in well-mixed populations.
#
# This work, as well as intuition, suggests that an individual edge is structurally significant to the spread of an infection in part due to how much it impacts the shortest path lengths between other nodes. This can be measured precisely by comparing the _average distance_ between nodes in the graph with the edge, and with the edge removed.
#
# As rewiring rate $p$ increases, the "first" rewired edge reduces the average distance significantly; the "second" edge less so, and so on. With a high rate of $p$, an individually rewired edge has only a small effect on average distance.
#
# One measure of the role of an edge in average distances is _edge betweenness centrality_: the number of shortest paths between nodes that run through an edge. A singular rewired edge will have a very high betweenness centrality. In a random graph (which is approximated if $p = 1$) the distribution of edge betweeness will be far more flat.
#
# We posit (and can test) that edges with higher betweeness centrality matter more for infectiousness. [We suspect based on preliminary work that these edges are _not_ as significant for contact tracing, which is significant because...]
#
#
# ## Privacy as protection from information infection
#
# We have intuitively settled on the idea that a 'distant' edge is one that may be more sensitive than a 'close' edge. But why is that?
#
# As discussed above, 'distant' edges on a rewired lattice are those responsible for shortening average path distances between nodes. One thing that defines the local neighborhood of a lattice is that all the nodes in a neighborhood are a short distance from each other. Indeed, a neighborhood might be defined as a set of nodes such that each included node is at most a distance of $d$ from the others. When $d = 1$, a neighborhood is a clique. [Is there a more canonical term for this?]
#
# Suppose that each individual $u$ is aware of an edge, or lack of edge, between other individuals $v$ and $w$ with a decreasing probability based on their distance to $v$ and $w$. Then $u$ will be more aware of edges within their neighborhood than those outside of their neighborhood. If there is a "distant" edge between $w$ and non-neighborhood individual $x$, then that edge is less likely to be known to $u$.
#
# In other words, we are considering the "privacy" of edges to be based on an implicit information diffusion of the graph's structure to the nodes within the structure itself. This information diffusion has some elements in common with a process of infection or percolation.
#
# For this reason [which we should spell out more], there is a sense in which the edges that are structurally _most private_ are also structurally _most important for infection_. This sets up a tension that we explore directly with out models.

# ### Our model
#
# We model a population with a graph $G$ in which nodes represent individuals and weight edges represent the chance of a contact occurring between those individuals. A disease spreads along this network according to a standard SEIR model, with some augmentations. Infected individuals may become _Symptomatic_, at which point they undergo testing and _Quarantine_, effectively removing themselves from the pandemic spread. The contact tracing system works by recording past contacts and spreading testing and quarantining along these traced paths.
#
# Individuals may or may not participate in the contact tracing system. An edge is only traced if both end nodes are _Adopters_ of the system. Also, there may be edges that are not traced because the individual chooses not to trace that specific edge. 

#
#
# # Experiment Design [2021-08-24 version]
#
# We have an $n$-dimensional lattice with $p$ (proportion) of the edges $E$ rewired.
#
# The contact tracing system is deployed such that $q$ of the rewired edges (_distant_) are traced, and $r$ of the original lattice (_close_) edges are traced.
#
# Holding $p$ constant for now, the model definition implies a probability distribution $P(i | q, r)$.
#
# This distribution has moments _mean_ $\mu_{q,r}$ and _standard deviation_ $\sigma_{q,r}$.
#
# There are other values of interest defined as follows:
#
# $$T = qp + r (1 - p)$$

# ## A complication
#
# We believe that for low enough values of $q$ and $r$ the distribution $P(i | q, r)$ is bimodal.
#
# The lower mode consists of cases where the epidemic does not occur because the disease dies out quickly.
#
# The upper mode consists of epidemic cases. In the untraced graph, epidemics are of large size because the boundary of the infected region of the graph can expand geometrically as the disease traverses distant edges.
#
# However, as the contract tracing system is more widely deployed (higher $q$ and $r$), the upper peak diminishes and epidemic sizes become more of a tail.
#
# In all cases, with only a single index at the start of the simulation, early extinctions are common.
#
# 1. _We are commiting to starting with 1 index_
#
# 2. How will we measure the threshold phenomenon?
#  - EH: We should pick a non-size dependent value. That is the threshold for a 'major outbreak' vs. 'minor outbreak'.
#  - We can analyze: (a) the success of minor outbreak/extinction., and (b) the mitigation of a major outbreak
#  - Both are interesting from a public health perspective.
#
# 3. How do we measure the transition?
#  -  Look at the base (0 contact tracing, control), and we chose the threshold based on the control case, and its well-separatedness
#  - because the size is monotonic in the contact tracing
#  
#  
#  As we increase the size of the network, we might get a cleaner base distribution.

# # Operationalizing the research question
#
# We are interested, ultimately, in the design of contact tracing systems and especially the sensitivity of those designs to the privacy preferences of their users.
#
# We have asserted that the _distant_ edges are more likely to be sensitive than _close_ edges, and that a user might opt out of the entire system rather than have a distant edge traced.
#
# For this reason, we are interested in the relative significance of distant and close edges for tracing efficacy.
#
# We believe that distant edges are more important for the spread of the disease.
#
# **Q1: How do we operationalize the relative importance of the distance vs. close edges for the spread of the disease?**
#
# We believe (based on preliminery results) that nonetheless the distant edges are not more important than close edges for the _tracing_ of the disease.
#
# **Q2: How do we operationalize the relative importance of distant vs. close edges for the tracing of the disease?**

# # Q1: 
#
# In earlier tests, we found that the final infected ratio of networks _without_ the contact tracing system was increasing in $p$, with an inflection point.
#
# _[TODO]: What was the reference on this?_
#
# In subsequent runs, we have picked a $p$ value that is on the phase boundary so that the effects of the contact tracing system would be visible. (I.e., with $p$ too high or too low, the graph topology might overwhelm any contact tracing effect.)

# # Q2: 
#
# One way to do this is to look at the effect of each additional edge on the mean infection rate.
#
# One approach: we can compare $\partial_q \mu$ with $\partial_r \mu$, the effect of a change in $q$ or $r$ on the mean infected ratio. We can then normalize these values to determine whether an additional distant or closed edge is more effective at improving the contact tracing system.
#
# The distant edges is more effective if the following condition holds:
#
# $$\frac{(1 - p) \partial_q \mu}{p \partial_r \mu}(q,r) > 1$$
#
# However, we cannot compute this value directly. The functions involved are indeed not continuous when defined on finite graphs. Also, we need to work from simulated samples of $P$. Furthermore, a difference in means will not have any direct meaning in terms of statistical significance.
#
# So instead of trying to compute a partial derivative, we need to look at statistical effect sizes instead. We consider [Cohen's d](https://en.wikiversity.org/wiki/Cohen%27s_d), the difference of means divided by the pooled standard deviation, as an effect size measure:
#
# $$d(x_1, x_2) = \frac{\bar{\mu}_1 - \bar{\mu}_2}{\bar{\sigma}_{1,2}}$$
#
# Where $\bar{\mu}$ and $\bar{\sigma}$ are the empirical mean and standard deviations for two cases 1 and 2.
#
# For a sampled grid of different values of $q$ and $r$ that are spaced at $\Delta_q$ and $\Delta_r$, we can compute 
#
# $$d_q(q, r) = (x(q, r),x(q + \Delta_q, r)) = \frac{\mu_{q,r} - \mu_{q + \Delta_q, r}}{\sigma_{q,r}}$$
#
# and 
#
# $$d_r(q,r) = d(x(q, r),x(q + \Delta_r, r)) = \frac{\mu_{q,r} - \mu_{q + \Delta_r, r}}{\sigma_{q,r}}$$
#
# to compute the effect size of shifts along the grid, and normalize these values to compare the approximate estimated effect of an additional distant or close edge:
#
# $$\frac{(1 - p) d_q (q, r)}{p d_r (q, r)} > 1$$
#
#
# **PROBLEM**: Taking tha ratio between these two effect sizes has some misleading results when:
#  - the effect sizes are very small, but subject to noise
#  - especially because the sign (positive or negative) can vary in these cases.
#  
# Upon consideration, I think it's better to define things somewhat differently. Use the pooled standard deviation from the entire data set, such that:
#
#
# $$d(x_1, x_2) = \frac{\bar{\mu}_1 - \bar{\mu}_2}{\bar{\sigma}}$$
#
# Where $\bar{\sigma}$ is the empirical standard deviations for the entire data set.
#
# For a sampled grid of different values of $q$ and $r$ that are spaced at $\Delta_q$ and $\Delta_r$, we can compute 
#
# $$d_q(q, r) = (x(q, r),x(q + \Delta_q, r)) = \frac{\mu_{q,r} - \mu_{q + \Delta_q, r}}{\sigma}$$
#
# and 
#
# $$d_r(q,r) = d(x(q, r),x(q + \Delta_r, r)) = \frac{\mu_{q,r} - \mu_{q + \Delta_r, r}}{\sigma}$$
#
# to compute the effect size of shifts along the grid, and normalize these values to compare the approximate estimated effect of an additional distant or close edge additively:
#
# $$(1 - p) d_q (q, r) - p d_r (q, r) > 0$$
#
#  

# ## Further considerations, for design
#
# In considering whether the effect size is great enough to warrant a change in the design of contact tracing system, we need to explicitly model user preferences.
#
#
# ### A (too) simple model
#
# A simple model is that the marginal user will decide against using the contact tracing system if it means that a distant edge is traced.
#
# For a lattice connectivity $K$ (i.e., the number of nodes each node is connected to in the original lattice), the average number of distant edges per node is $Kp$; the average number of close edges is $K(1 - p)$.
#
# The designer is notionally deciding between $n$ nodes adopting, with all viable close and distant edges traced, and $n + 1$ nodes adopting, but with the $(n + 1)$th node blocking the tracing on their own distant edge.
#
# In this case, it will _always_ be better to have the distant edges be optional, because there will be more traced edges in the privacy-preserving case.
#
# However, this model is almost certainly too simplistic, as is hardly interacts with the effectiveness of the contact tracing at all.
#
# KS: Think about the comparison with traditional contact tracing.
#
# Cases:
#  - Only voluntary tracing statements
#  - Involuntary trace if adopting / leave phone at home.
#
# ### A different model
#
# Here, the user's utility is modeled more explicitly. The privacy cost of having a traced distant edge is weighed against other costs such as desires for personal and public health. Depending on this utility function, we see either fewer nodes, but fully traced (close and distant edges), or more nodes, but with few distant edges among them traced.
#
# Note that the distribution of edges in either case will be different from the case we've been studying thus far, which has 100% adoption but arbitrarily limits edge tracing in order to measure the effectiveness rates of different edges. If we had a different way of operationalizing Q2, we might approach this differently.

#  _"For some selection of parameter ranges, it's not worth insisting on data collection on sensitive edges if it reduces adoption to the point that the epidemic is not supressed."_
#  
# JE: What if the insistence level is a value, and varying it leads to more/less effectiveness
#
# SB: Heterogenous privacy preferences.



StarcoderdataJupyterScriptsDedupFiltered