Skip to content

Instantly share code, notes, and snippets.

@jfinkels
Created January 11, 2016 04:58
Show Gist options
  • Save jfinkels/f304a285098e9a4606bd to your computer and use it in GitHub Desktop.
Save jfinkels/f304a285098e9a4606bd to your computer and use it in GitHub Desktop.
Estimating mean and relative size of a sample of a population
def estimate_mean(sample, values, weights=None):
"""Estimates the average value of a set of elements based on the given
sample.
Parameters
----------
sample : iterable
Iterable of objects.
values : function
This function takes elements from ``sample`` as inputs and must return
a :class:`float` representing the value of the element.
weights : function
This function takes elements from ``sample`` as inputs and must return
a :class:`float` representing the weight of the element. If this is
not provided, then it is assumed to be the constant function.
Returns
-------
float
An estimate of the mean of the values of all elements.
Examples
--------
Estimating the mean of twice the node indices in the wheel graph on ten
vertices::
>>> import networkx as nx
>>> G = nx.wheel_graph(10)
>>> sample = uniform_independent_node_sample(G, size=20)
>>> estimate_mean(sample, values=lambda x: 2 * x)
8.9000000000000004
Estimating the mean of the node labels on a graph given a large number of
samples::
>>> import networkx as nx
>>> G = nx.wheel_graph(10)
>>> N = 10000
>>> sample = list(nx.uniform_independent_node_sample(G, size=N))
>>> nx.estimate_mean(sample, lambda v: v)
???
Estimating the mean of the node labels on a graph given a large number of
samples, weighted according to degree::
>>> import networkx as nx
>>> G = nx.wheel_graph(10)
>>> N = 10000
>>> sample = list(nx.uniform_independent_node_sample(G, size=N))
>>> nx.estimate_mean(sample, values, weights=G.degree)
???
"""
if weights is None:
weights = lambda x: 1
up = sum(values(v) / weights(v) for v in sample)
down = sum(1 / weights(v) for v in sample)
return up / down
def estimate_relative_size(sample, labeled, weights=None):
"""Estimates the fraction of labeled objects given a sample of the entire
population.
Parameters
----------
sample : iterable
Iterable of objects.
labeled : Boolean function
This function takes elements from ``sample`` as inputs and must return
``True`` if and only if the element is labeled.
weights : function
This function takes elements from ``sample`` as inputs and must return
a :class:`float` representing the weight of the element.
Returns
-------
int
An estimate of the fraction of labeled elements.
Examples
--------
Estimate the fraction of labeled elements in the wheel graph on ten
vertices, where the first five nodes are labeled::
>>> import networkx as nx
>>> G = nx.wheel_graph(10)
>>> is_labeled = lambda v: v in range(5)
>>> estimate_relative_size(S, labeled=is_labeled, weights=G.degree)
0.50679999999999814
"""
values = lambda s: 1 if labeled(s) else 0
return estimate_mean(sample, values, weights)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment