%pylab inline
import numpy as np
points = np.array([[0,0], [1,1], [1,1], [2, 0]])
plt.scatter(points[:, 0])

"""
Explanation: 1

2

3

4

5

6

7

马尔可夫链的蒙特卡洛模拟？
8

9

10

11

可以将异或看成是不考虑进位的加法：0 + 0 = 0, 0 + 1 = 1, 1 + 1 = 0
12

13

14

15

16

17

18

19

20

21

22

http://stats.stackexchange.com/questions/41317/how-to-show-operations-on-two-random-variables-each-bernoulli-are-dependent-bu
这里举了一个例子，设X, Y是互相独立的服从0-1分布的两个随机变量，且P(X=1)=P(Y=1)=0.5，那么X+Y和|X-Y|就是两个不相关，却不独立的两个随机变量。
End of explanation
"""


import numpy as np

# rv is short for random variable
def cal_stats(rv):
    
#     if not rv:
#         raise ValueError("None")
        
    length = len(rv)
    
    if length == 0:
        raise ValueError("length of 0")
    mean = 0
    variance = 0
    # third order origin moment, 三阶原点矩
    third_order = 0
    two_order = 0
    fourth_order = 0
    for x in rv:
        mean += x
        two_order += x ** 2
        third_order += x ** 3
        fourth_order += x ** 4
    else:
        mean /= length
        two_order /= length
        third_order /= length
        fourth_order /= length
        
    mean = mean
    variance = two_order - mean ** 2
    skewness = (third_order - 3 * mean * variance - mean ** 3) / variance ** 1.5
    kurtosis = fourth_order / variance ** 2 - 3
    return (mean, variance, skewness, kurtosis)

# randn sample from normal distribution.
data = []
data.append(np.random.randn(10000))
data.append(2 *np.random.randn(10000))
data.append([x for x in data[0] if x > -0.5])
data.append(np.random.uniform(0, 4, 10000))

stats = []
for l in data:
    stats.append(cal_stats(l))

template = r'$\mu={0:.2f},\ \sigma={1:.2f},\ skewness={2:.2f},\ kurt={3:.2f}$'
infos = []
for stat in stats:
    infos.append(template.format(*stat))

plt.text(1, 0.38, infos[0], bbox=dict(facecolor='red', alpha=0.25))
plt.text(1, 0.35, infos[1], bbox=dict(facecolor='red', alpha=0.25))
plt.hist(data[0], 50, normed=True, facecolor='r', alpha=0.9)
plt.hist(data[1], 80, normed=True, facecolor='g', alpha=0.8)

"""
Explanation: 23

24

25

26

为什么要除n？
27

对称阵的不同特征值对应的特征向量相互正交，那么也就是说对称阵的单位特征向量组成的矩阵是正交阵。
正交阵:$Q^T * Q = I$，也就是说每一个列向量，自己乘自己，是1，和别人乘，是0。
另外还有对角化的关系

这个感觉和SVD有那么点关联，不太确定。
28

29

期望是一阶原点矩，方差是二阶中心矩。
30

31

32

33

34
End of explanation
"""


