Skip to main content

Python library to analyze names and calculate statistics against a baseline

Project description

pynamebaseline

Python library to analyze names and calculate statistics against a baseline

Usage

Install namebaseline.

pip install namebaseline

Create your baseline and suspicious lists (one string per line)

If the baseline file is baseline.csv and the suspicious file is sus.csv, then you can run the following:

from namebaseline import str_cumdf, chi2_gof_test

# Each file is a list of names, 1 per line
# givenname,surname,...
baseline = pd.read_csv("baseline.csv") 
sus = pd.read_csv("sus.csv")

# Create Cumulative Distribution Functions
baseline_cumdf = str_cumdf(baseline['givenname'])
sus_cumdf = str_cumdf(sus['givenname'])

# Run Chi-Squared Goodness-of-Fit Test
result = namebaseline.chi2_gof_test(baseline_cumdf, sus_cumdf)

if result.follows_baseline:
    print("sus follows baseline")
else:
    print("sus does not follow baseline")

To save the baseline cumulative distribution function to use again later:

write_cdf("baseline.json", baseline_cumdf)

To load the baseline cumulative distribution function:

baseline_cumdf = read_cdf("baseline.json")

// TODO: Add more details about transformations

Chi-Square Goodness-of-Fit Test

https://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm

Does the sample come from a population with a specific distribution?

Chi-square goodness-of-fit test is applied to binned (classified) data. We create convert all names to floating point values from 0...1, and assign an equal score to each name (1). Conceptually, we are making all names plottable on a x/y grid, with the x position equal to the name value, and the y position equal to the score for that name.

We then create a histogram of all names, summing up the values for each bin. I create 1000 equally-sized bins for this: (0...0.001], (0.001...0.002], ...

This creates our binned counts.

// TODO: Add example image

We can create a probability distribution function by dividing each bin by the total number of names.

// TODO: Add example image

If we plot the baseline and the sample using this distribution, differences are not easily visible. However, if we create a cumulative distribution function from the probability distribution function, the differences are much easier to see.

A cumulative distribution function is created by summing all previous scores as we scan the values from left to right.

By graphing the baseline and sample CDFs together, we can easily see the differences.

// TODO: Add example image

We use the Chi-Square Goodness-of-Fit test to statistically determine whether our sample follows or does not follow our baseline distribution.

The Chi-Square test uses the following null (H_0) and alternate (H_a) hypotheses:

H_0: The data follows the baseline distribution H_a: The data does not follow the baseline distribution

The test statistic (x^2) is calculated from the baseline CDF and sample bins.

N is the total samples size. O_i is the count of samples in bin i. E_i is the expected counts in bin i based on the baseline.

x^2 = sumi=1...k^2 / E_i

E_i = N*(CDF_baseline(i) - CDF_baseline(i-1))

alpha: significance level = 0.05 (95% confidence)

Critical Region: k: non-empty cells (1000) c: estimated distribution function parameters (0) + 1

The test statistic follows an (approximately) X^2 distribution with (k-c) degrees of freedom)

Reject if X^2 > X^2_(1-a,k-c) The critical value is calculated using scipy.stats.chi2.ppf(q, df) where q = 1-a, df = the degrees of freedom

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

namebaseline-0.1.1.tar.gz (53.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

namebaseline-0.1.1-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file namebaseline-0.1.1.tar.gz.

File metadata

  • Download URL: namebaseline-0.1.1.tar.gz
  • Upload date:
  • Size: 53.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for namebaseline-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f57d9ad5f1f116e326d24bf1bc72d4d88ca2ebe1e6e335f082e0774f3ce78349
MD5 38242624a968ddb14b1fd540426cd047
BLAKE2b-256 4284f1b638f8703520ea2397b84a6ad5f5156ba94c7d06145c0bea2fdc2f692d

See more details on using hashes here.

File details

Details for the file namebaseline-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: namebaseline-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for namebaseline-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1bc6ab41475b1737971419000b443905fb8bff4da59573debdad68aea34db2b3
MD5 03ac2d0134276c9fa29acc130bd9d14b
BLAKE2b-256 b2aed46299fe90d1939db7349335a4beee6fd07d242adbf62805a956094ed5e4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page