课程19 Variance 编程
Mean
#Complete the mean function to make it return the mean of a list of numbers
data1=[49., 66, 24, 98, 37, 64, 98, 27, 56, 93, 68, 78, 22, 25, 11]
def mean(data):
#Insert your code here
return sum(data)/len(data)
Mode
#Complete the mode function to make it return the mode of a list of numbers
data1=[1,2,5,10,-20,5,5]
def mode(data):
#Insert your code here
modecnt=0
for i in range(len(data)):
icount=data.count(data[i])
if icount>modecnt:
mode=data[i]
modecnt=icount
return mode
print mode(data1)
Variance
#Complete the variance function to make it return the variance of a list of numbers
data3=[13.04, 1.32, 22.65, 17.44, 29.54, 23.22, 17.65, 10.12, 26.73, 16.43]
def mean(data):
return sum(data)/len(data)
def variance(data):
#Insert your code here
mu=mean(data)
return mean([(x-mu)**2 for x in data])
课程20 Problem set 3 Estimators
练习1 MLE proof (optional ) -- 不理解
Standard score
用公式表示为:z=(x-μ)/σ;其中z为标准分数;x为某一具体分数,μ为平均数,σ为标准差
练习3 Scale Data
数据n倍,均值n倍,标准差n倍, 方差n方倍,z 分数不变。
练习7 Expected Variance
Variance - correction factor n/(n-1)
练习12 Likelihood challenge
#Compute the likelihood of observing a sequence of die rolls
#Likelihood is the probability of getting the specific set of rolls
#in the given order
#Given a multi-sided die whose labels and probabilities are
#given by a Python dictionary called dist and a sequence (list, tuple, string)
#of rolls called data, complete the function likelihood
#Note that an element of a dictionary can be retrieved by dist[key] where
#key is one of the dictionary's keys (e.g. 'A', 'Good').
def likelihood(dist,data):
#Insert your answer here
p=1
for d in data:
p*=p*dist[d]
return p
课程21 Outliers 异常值
有意忽略数据
Quartile interquartille
Percentile
忽略数据,计算均值,四分位,Trimmed mean(切尾平均数 截尾均值),百分位
课程22 Binomial Distribution 二项分布
组合计算 阶乘 概率
课程23 Central Limit Theorem
杨辉三角
课程24 编程 Bell Curve
#Write a function flip that simulates flipping n fair coins.
#It should return a list representing the result of each flip as a 1 or 0
#To generate randomness, you can use the function random.random() to get
#a number between 0 or 1. Checking if it's less than 0.5 can help your
#transform it to be 0 or 1
import random
from math import sqrt
def mean(data):
return float(sum(data))/len(data)
def variance(data):
mu=mean(data)
return sum([(x-mu)**2 for x in data])/len(data)
def stddev(data):
return sqrt(variance(data))
def flip(N):
#Insert your code here
return [random.random()>0.5 for i in range(N)]
N=1000
f=flip(N)
print mean(f)
print stddev(f)
#Write a function sample that simulates N sets of coin flips and
#returns a list of the proportion of heads in each set of N flips
#It may help to use the flip and mean functions that you wrote before
import random
from math import sqrt
from plotting import *
def mean(data):
return float(sum(data))/len(data)
def variance(data):
mu=mean(data)
return sum([(x-mu)**2 for x in data])/len(data)
def stddev(data):
return sqrt(variance(data))
def flip(N):
return [random.random()>0.5 for x in range(N)]
def sample(N):
#Insert your code here
return [mean(flip(N)) for x in range(N)]
N=1000
outcomes=sample(N)
histplot(outcomes,nbins=30)
print mean(outcomes)
print stddev(outcomes)