Microsynthesis using quasirandom sampling and/or IPF

# humanleague ## Introduction

humanleague is a python and an R package for microsynthesising populations from marginal and (optionally) seed data. The package is implemented in C++ for performance.

The package contains algorithms that use a number of different microsynthesis techniques:

The latter provides a bridge between deterministic reweighting and combinatorial optimisation, offering advantages of both techniques:

• generates high-entropy integral populations
• can be used to generate multiple populations for sensitivity analysis
• goes some way to address the 'empty cells' issues that can occur in straight IPF
• relatively fast computation time

The algorithms:

• support arbitrary dimensionality* for both the marginals and the seed.
• produce statistical data to ascertain the likelihood/degeneracy of the population (where appropriate).

The package also contains the following utility functions:

• a Sobol sequence generator
• construct a closest integer population from a discrete univariate probability distribution.
• an algorithm for sampling an integer population from a discrete multivariate probability distribution, constrained to the marginal sums in every dimension (see below).
• 'flatten' a multidimensional population into a table: this converts a multidimensional array containing the population count for each state into a table listing individuals and their characteristics.

Version 1.0.1 reflects the work described in the Quasirandom Integer Sampling (QIS) paper.

## Installation

### Python

#### PyPI

python3 -m pip install humanleague --user


#### Anaconda

conda install -c conda-forge humanleague


### Build, install and test (from cloned repo)

pip install -e .
pytest


### R

Official release:

> install.packages("humanleague")


For a development version

> devtools::install_github("virgesmith/humanleague")


Or, for the legacy version

> devtools::install_github("virgesmith/humanleague@1.0.1")


## Documentation and Examples

### R

Consult the package documentation, e.g.

> library(humanleague)
> ?humanleague


### Python

The package now contains type annotations and your IDE should automatically display this, e.g.: ### Multidimensional integerisation

Building on the prob2IntFreq function - which takes a discrete probability distribution and a count, and returns the closest integer population to the distribution that sums to the count - a multidimensional equivalent integerise is introduced. In one dimension, for example:

>>> import numpy as np
>>> import humanleague
>>> p=np.array([0.1, 0.2, 0.3, 0.4])
>>> humanleague.prob2IntFreq(p, 11)
{'freq': array([1, 2, 3, 5]), 'rmse': 0.3535533905932736}


produces the optimal (i.e. closest possible) integer population to the discrete distribution.

The integerise function generalises this problem and applies it to higher dimensions: given an n-dimensional array of real numbers where the 1-d marginal sums in every dimension are integral (and thus the total population is too), it attempts to find an integral array that also satisfies these constraints.

The QISI algorithm is repurposed to this end. As it is a sampling algorithm it cannot guarantee that a solution is found, and if so, whether the solution is optimal. If it fails this does not prove that a solution does not exist for the given input.

>>> a = np.array([[ 0.3,  1.2,  2. ,  1.5],
[ 0.6,  2.4,  4. ,  3. ],
[ 1.5,  6. , 10. ,  7.5],
[ 0.6,  2.4,  4. ,  3. ]])
# marginal sums
>> sum(a)
array([ 3., 12., 20., 15.])
>>> sum(a.T)
array([ 5., 10., 25., 10.])
# perform integerisation
>>> r = humanleague.integerise(a)
>>> r["conv"]
True
>>> r["result"]
array([[ 0,  2,  2,  1],
[ 0,  3,  4,  3],
[ 2,  6, 10,  7],
[ 1,  1,  4,  4]])
>>> r["rmse"]
0.5766281297335398
# check marginals are preserved
>>> sum(r["result"]) == sum(a)
array([ True,  True,  True,  True])
>>> sum(r["result"].T) == sum(a.T)
array([ True,  True,  True,  True])


## Project details

This version 2.1.10 2.1.9 2.1.8 2.1.7 2.1.5 2.1.1 2.1.0 2.0.5 2.0.3

Uploaded source
{"ft_lang_label":"__label__en","ft_lang_prob":0.73775,"math_prob":0.88855517,"size":4408,"snap":"2022-27-2022-33","text_gpt3_token_len":1095,"char_repetition_ratio":0.124659404,"word_repetition_ratio":0.015625,"special_character_ratio":0.25589836,"punctuation_ratio":0.18421052,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98119825,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22],"im_url_duplicate_count":[null,4,null,1,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-14T00:51:03Z\",\"WARC-Record-ID\":\"<urn:uuid:1daeb9f0-32c0-4ef9-a56c-6d0a829947d8>\",\"Content-Length\":\"69283\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3904c241-9ede-41e7-988e-87774bd870f5>\",\"WARC-Concurrent-To\":\"<urn:uuid:6cadf8bd-ce88-46c9-8f69-9a0d885bd0c9>\",\"WARC-IP-Address\":\"151.101.128.223\",\"WARC-Target-URI\":\"https://pypi.org/project/humanleague/\",\"WARC-Payload-Digest\":\"sha1:VE6ZWMBUSGWFZN53ZCQND6HOSEPUAQL5\",\"WARC-Block-Digest\":\"sha1:FJQWQJWZAOLDOZYFHY6L44ONRHN57LVX\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882571989.67_warc_CC-MAIN-20220813232744-20220814022744-00176.warc.gz\"}"}