Giter Site home page Giter Site logo

fossabot / ineqpy Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mmngreco/ineqpy

0.0 0.0 0.0 3.66 MB

IneqPy package provides statistics to do a properly quantitative analysis of inequality.

Home Page: https://mmngreco.github.io/IneqPy/

License: MIT License

Python 42.90% HTML 51.86% Jupyter Notebook 5.00% Shell 0.11% Dockerfile 0.14%

ineqpy's Introduction

Build Status FOSSA Status

IneqPy Package

This package provides statistics to do a properly quantitative analysis of inequality. Among the estimators provided by this package you can find:

Main Statistics Inequality Indicators
Weighted Mean Weighted Gini
Weighted Variance Weighted Atkinson
Weighted Coefficient of variation Weighted Theil
Weighted Kurtosis Weighted Kakwani
Weighted Skewness Weighted Lorenz curve

Installation

$ pip install git+https://github.com/mmngreco/IneqPy.git

Examples

Some examples of how use this package: Data of example:

>>> import pandas as pd
>>> import numpy as np
>>> import ineqpy
>>> d

             renta   factor
0        -13004.12   1.0031
89900    141656.97   1.4145
179800     1400.38   4.4122
269700   415080.96   1.3295
359600    69165.22   1.3282
449500     9673.83  19.4605
539400    55057.72   1.2923
629300     -466.73   1.0050
719200     3431.86   2.2861
809100      423.24   1.1552
899000        0.00   1.0048
988900     -344.41   1.0028
1078800   56254.09   1.2752
1168700   60543.33   2.0159
1258600    2041.70   2.7381
1348500     581.38   7.9426
1438400   55646.05   1.2818
1528300       0.00   1.0281
1618200   69650.24   1.2315
1708100   -2770.88   1.0035
1798000    4088.63   1.1256
1887900       0.00   1.0251
1977800   10662.63  28.0409
2067700    3281.95   1.1670

Descriptive statistics

>>> ineqpy.mean(variable=d.renta, weights=d.factor)
20444.700666031338
>>> ineqpy.var(variable=d.renta, weights=d.factor)
2982220948.7413292
>>> x, w = d.renta.values, d.factor.values

Note that the standardized moment for order n, retrieve the value in that column:

n value
1 0
2 1
3 Skew
4 Kurtosis

A helpful table of interpretation of the moments

>>> ineqpy.std_moment(variable=x, weights=w, order=1)  # = 0
2.4624948200717338e-17
>>> ineqpy.std_moment(variable=x, weights=w, order=2)  # = 1
1.0
>>> ineqpy.std_moment(variable=x, weights=w, order=3)  # = skew
5.9965055750379426
>>> ineqpy.skew(variable=x, weights=w)
5.9965055750379426
>>> ineqpy.std_moment(variable=x, weights=w, order=4)  # = kurtosis
42.319928851703004
>>> ineqpy.kurt(variable=x, weights=w)
42.319928851703004

Inequality estimators

# pass a pandas.DataFrame and inputs as strings
>>> ineqpy.gini(data=d, income='renta', weights='factor')
0.76739136365917116
# you can pass arrays too
>>> ineqpy.gini(income=d.renta.values, weights=d.factor.values)
0.76739136365917116

More examples and comparision with other packages:

We generate random weighted data to show how ineqpy works. The variables simulate being:

x : Income
w : Weights

To test with classical statistics we generate:

x_rep : Income values replicated w times each one.
w_rep : Ones column.

Additional information:

np : numpy package
sp : scipy package
pd : pandas package
gsl_stat : GNU Scientific Library written in C.
ineq : IneqPy

MEAN

>>> np.mean(x_rep)       = 488.535714286
>>> ineq.mean(x, w)      = 488.535714286
>>> gsl_stat.wmean(w, x) = 488.5357142857143

VARIANCE

>>> np.var(x_rep)                = 63086.1364796
>>> ineq.var(x, w)               = 63086.1364796
>>> ineq_stat.wvar(x, w, kind=1) = 63086.1364796
>>> ineq_stat.wvar(x, w, kind=2) = 63247.4820972
>>> gsl_stat.wvariance(w, x)     = 63993.161585889124
>>> ineq_stat.wvar(x, w, kind=3) = 63993.1615859

COVARIANCE

>>> np.cov(x_rep, x_rep)            =  [[ 63247.48209719  63247.48209719]
 [ 63247.48209719  63247.48209719]]
>>> ineq_stat.wcov(x, x, w, kind=1) =  63086.1364796
>>> ineq_stat.wcov(x, x, w, kind=2) =  4.94065645841e-324
>>> ineq_stat.wcov(x, x, w, kind=3) =  9.88131291682e-324

SKEWNESS

>>> gsl_stat.wskew(w, x) =  -0.05742668111416989
>>> sp_stat.skew(x_rep)  =  -0.058669605967865954
>>> ineq.skew(x, w)      =  -0.0586696059679

KURTOSIS

>>> sp_stat.kurtosis(x_rep)  =  -0.7919389201857214
>>> gsl_stat.wkurtosis(w, x) =  -0.8540884810553052
>>> ineq.kurt(x, w) - 3      =  -0.791938920186

PERCENTILES

>>> ineq_stat.percentile(x, w, 50) =  526
>>> np.percentile(x_rep, 50)       =  526.0
>>> ineq_stat.percentile(x, w, 25) =  293
>>> np.percentile(x_rep, 25)       =  293.0
>>> ineq_stat.percentile(x, w, 75) =  667
>>> np.percentile(x_rep, 75)       =  670.0
>>> ineq_stat.percentile(x, w, 10) =  135
>>> np.percentile(x_rep, 10)       =  135.0
>>> ineq_stat.percentile(x, w, 90) =  839
>>> np.percentile(x_rep, 90)       =  839.0

Another way to use this is through the API module as shown below:

API MODULE

Using api module:

>>> data = svy(data=data, columns=columns, weights='w')
>>> data.head()
     x  w
0  111  3
1  711  4
2  346  4
3  667  1
4  944  1

Statistics:

>>> data.weights = w
>>> df.mean(main_var)       = 488.535714286
>>> df.percentile(main_var) = 526
>>> df.var(main_var)        = 63086.1364796
>>> df.skew(main_var)       = -0.0586696059679
>>> df.kurt(main_var)       = 2.20806107981
>>> df.gini(main_var)       = 0.298494329293
>>> df.atkinson(main_var)   = 0.0925853855635
>>> df.theil(main_var)      = 0.156137490566

License

FOSSA Status

ineqpy's People

Contributors

dependabot[bot] avatar fossabot avatar mmngreco avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.