<jupyter_start><jupyter_text>1 ![1](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0001.jpg)  2 ![2](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0002.jpg)  3 ![3](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0003.jpg)  4 ![4](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0004.jpg)  5 ![5](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0005.jpg)  6 ![6](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0006.jpg)  7 ![7](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0007.jpg) 马尔可夫链的蒙特卡洛模拟？  8 ![8](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0008.jpg)  9 ![9](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0009.jpg)  10 ![10](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0010.jpg)  11 ![11](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0011.jpg) 可以将异或看成是不考虑进位的加法：0 + 0 = 0, 0 + 1 = 1, 1 + 1 = 0  12 ![12](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0012.jpg)  13 ![13](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0013.jpg)  14 ![14](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0014.jpg)  15 ![15](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0015.jpg)  16 ![16](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0016.jpg)  17 ![17](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0017.jpg)  18 ![18](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0018.jpg)  19 ![19](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0019.jpg)  20 ![20](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0020.jpg)  21 ![21](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0021.jpg)  22 ![22](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0022.jpg) 这里举了一个例子，设X, Y是互相独立的服从0-1分布的两个随机变量，且P(X=1)=P(Y=1)=0.5，那么X+Y和|X-Y|就是两个不相关，却不独立的两个随机变量。<jupyter_code>%pylab inline
import numpy as np
points = np.array([[0,0], [1,1], [1,1], [2, 0]])
plt.scatter(points[:, 0])<jupyter_output>Populating the interactive namespace from numpy and matplotlib<jupyter_text>23 ![23](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0023.jpg)  24 ![24](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0024.jpg)  25 ![25](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0025.jpg)  26 ![26](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0026.jpg) 为什么要除n？  27 ![27](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0027.jpg) 对称阵的不同特征值对应的特征向量相互正交，那么也就是说对称阵的单位特征向量组成的矩阵是正交阵。正交阵:$Q^T * Q = I$，也就是说每一个列向量，自己乘自己，是1，和别人乘，是0。另外还有对角化的关系![这里写图片描述](http://img.blog.csdn.net/20160111223902249)这个感觉和SVD有那么点关联，不太确定。  28 ![28](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0028.jpg)  29 ![29](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0029.jpg) 期望是一阶原点矩，方差是二阶中心矩。  30 ![30](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0030.jpg)  31 ![31](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0031.jpg)  32 ![32](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0032.jpg)  33 ![33](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0033.jpg)  34 ![34](http://7xqhfk.com1.z0.glb.clouddn.com/zbml/lec02/0034.jpg)<jupyter_code>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)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20552831029870708, 'fraction_numerical': 0.12015158270173874, 'mean_word_length': 3.70335429769392, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '41192', 'license': 'MIT', 'n_tokens_mistral': 2615, 'n_tokens_neox': 2397, 'n_words': 250, 'path': 'ml-zb/lec02.ipynb', 'repo_name': 'JasonWayne/course-notes', 'size': '4486'}