In [54]:
import statistics
import scipy.stats as stats
import pandas as pd
import numpy as np
import ineqpy

Random Variable


In [42]:
x = np.random.randn(10)

In [43]:
w = abs(np.random.randn(10))
w = w / w.sum()

In [44]:
np.mean(x)


Out[44]:
0.079282307081652598

In [45]:
ineqpy.mean(variable=x)


Out[45]:
0.079282307081652598

In [46]:
np.var(x)


Out[46]:
0.26771028632127503

In [47]:
ineqpy.var(variable=x)


Out[47]:
0.26771028632127503

In [48]:
stats.skew(x)


Out[48]:
-0.08683494039388037

In [49]:
ineqpy.skew(variable=x)


Out[49]:
-0.086834940393880372

In [52]:
stats.kurtosis(x) + 3


Out[52]:
2.1915828922522693

In [51]:
ineqpy.kurt(variable=x)


Out[51]:
2.1915828922522693

Repeated values


In [4]:
x = np.array([1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,6,6,6,6,7,7,7,8,8,9])

In [5]:
xi, fi = np.unique(x, return_counts=True)

In [6]:
xi # values


Out[6]:
array([1, 2, 3, 4, 5, 6, 7, 8, 9])

In [7]:
fi # absolute frequency


Out[7]:
array([1, 2, 3, 4, 5, 4, 3, 2, 1])

In [18]:
data = pd.DataFrame(np.c_[x, np.ones(len(x))], columns=list('xf'))

In [19]:
data


Out[19]:
x f
0 1.0 1.0
1 2.0 1.0
2 2.0 1.0
3 3.0 1.0
4 3.0 1.0
5 3.0 1.0
6 4.0 1.0
7 4.0 1.0
8 4.0 1.0
9 4.0 1.0
10 5.0 1.0
11 5.0 1.0
12 5.0 1.0
13 5.0 1.0
14 5.0 1.0
15 6.0 1.0
16 6.0 1.0
17 6.0 1.0
18 6.0 1.0
19 7.0 1.0
20 7.0 1.0
21 7.0 1.0
22 8.0 1.0
23 8.0 1.0
24 9.0 1.0

In [20]:
data_weighted = pd.DataFrame(np.c_[xi,fi], columns=list('xf'))

In [21]:
data_weighted


Out[21]:
x f
0 1 1
1 2 2
2 3 3
3 4 4
4 5 5
5 6 4
6 7 3
7 8 2
8 9 1

Mean


In [22]:
np.mean(x)


Out[22]:
5.0

In [23]:
data.mean()


Out[23]:
x    5.0
f    1.0
dtype: float64

In [24]:
ineqpy.mean(variable=x)


Out[24]:
5.0

In [25]:
ineqpy.mean(data, 'x')


Out[25]:
5.0

In [26]:
ineqpy.mean(variable=xi, weights=fi)


Out[26]:
5.0

In [27]:
ineqpy.mean(data, 'x', 'f')


Out[27]:
5.0

Variance


In [28]:
np.var(x, ddof=1)  # numpy (ddof=1)


Out[28]:
4.166666666666667

In [29]:
stats.variation(x)  # scipy (ddof=0)


Out[29]:
0.40000000000000002

In [30]:
data.var()  # pandas (ddof=1)


Out[30]:
x    4.166667
f    0.000000
dtype: float64

In [31]:
ineqpy.var(variable=x)


Out[31]:
4.0

In [32]:
ineqpy.var(variable=xi, weights=fi)


Out[32]:
4.0

Skewness


In [33]:
stats.skew(x)


Out[33]:
0.0

In [41]:
data.skew()


Out[41]:
x    0.0
f    0.0
dtype: float64

In [34]:
ineqpy.skew(variable=x)


Out[34]:
0.0

In [35]:
ineqpy.skew(variable=xi, weights=fi)


Out[35]:
0.0

Kurtosis


In [36]:
stats.kurtosis(x) + 3


Out[36]:
2.35

In [40]:
data.kurt()+3


Out[40]:
x    2.483004
f    3.000000
dtype: float64

In [37]:
ineqpy.kurt(variable=x)


Out[37]:
2.3500000000000001

In [38]:
ineqpy.kurt(variable=xi, weights=fi)


Out[38]:
2.3500000000000001

In [ ]: