# SKKU-통계 by SGLee

## 3163 days 전, admin 작성

기초통계 관련 Sage 명령어 정리 by SGLee

1. mean(v) # vlist. 평균

sage: mean([pi, e])
 1/2*pi + 1/2*e 1/2*pi + 1/2*e
sage: mean([]) # list에 정보가 없으면 NaN
 NaN NaN
sage: mean([I, sqrt(2), 3/5])
 1/3*sqrt(2) + 1/3*I + 1/5 1/3*sqrt(2) + 1/3*I + 1/5
sage: mean([RIF(1.0103,1.0103), RIF(2)])
 1.5051500000000000? 1.5051500000000000?
sage: mean(range(4))
 3/2 3/2
sage: v = finance.TimeSeries([1..100]) sage: mean(v)
 50.5 50.5

2. median(v) # vlist. 중앙값

sage: median([1,2,3,4,5])
 3 3
sage: median([e, pi])
 1/2*pi + 1/2*e 1/2*pi + 1/2*e
sage: median(['sage', 'linux', 'python'])
 'python' 'python'
sage: median([])
 NaN NaN

3. mode(v) # vlist. 최빈값

sage: v = [1,2,4,1,6,2,6,7,1] sage: mode(v)
 [1] [1]
sage: v.count(1) # 1이 몇 번 나오는지
 3 3
sage: mode([])
 [] []
sage: mode([1,2,3,4,5])
 [1, 2, 3, 4, 5] [1, 2, 3, 4, 5]
sage: mode([3,1,2,1,2,3])
 [1, 2, 3] [1, 2, 3]
sage: mode(['sage', 4, I, 3/5, 'sage', pi])
 ['sage'] ['sage']

4. moving_average(v, n) # 이동평균

sage: moving_average([1..10], 1)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

sage: moving_average([1..10], 4)

[5/2, 7/2, 9/2, 11/2, 13/2, 15/2, 17/2]

sage: moving_average([], 1)

[]

sage: moving_average([pi, e, I, sqrt(2), 3/5], 2)

[1/2*pi + 1/2*e, 1/2*e + 1/2*I, 1/2*sqrt(2) + 1/2*I, 1/2*sqrt(2) + 3/10]

sage: moving_average([pi, e, I, sqrt(2), 3/5], 2)
 [1/2*pi + 1/2*e, 1/2*e + 1/2*I, 1/2*sqrt(2) + 1/2*I, 1/2*sqrt(2) + 3/10] [1/2*pi + 1/2*e, 1/2*e + 1/2*I, 1/2*sqrt(2) + 1/2*I, 1/2*sqrt(2) + 3/10]

5. std(v, bias=False) # 표준편차

sage: std([1..6], bias=True)

1/2*sqrt(35/3)

sage: std([1..6], bias=False)

sqrt(7/2)

sage: std([e, pi])

sqrt(1/2)*sqrt((pi - e)^2)

sage: std([])

NaN

sage: std([I, sqrt(2), 3/5])

sqrt(1/450*(10*sqrt(2) - 5*I - 3)^2 + 1/450*(5*sqrt(2) - 10*I + 3)^2 + 1/450*(5*sqrt(2) + 5*I - 6)^2)

sage: std([RIF(1.0103, 1.0103), RIF(2)])

0.6998235813403261?

sage: std([I, sqrt(2), 3/5])
 sqrt(1/450*(5*sqrt(2) + 5*I - 6)^2 + 1/450*(5*sqrt(2) - 10*I + 3)^2 + 1/450*(10*sqrt(2) - 5*I - 3)^2) sqrt(1/450*(5*sqrt(2) + 5*I - 6)^2 + 1/450*(5*sqrt(2) - 10*I + 3)^2 + 1/450*(10*sqrt(2) - 5*I - 3)^2)

6. variance(v, bias=False) # 분산

sage: variance([1..6])

7/2

sage: variance([1..6], bias=True)

35/12

sage: variance([e, pi])

1/2*(pi - e)^2

sage: variance([])

NaN

sage: variance([I, sqrt(2), 3/5])

1/450*(10*sqrt(2) - 5*I - 3)^2 + 1/450*(5*sqrt(2) - 10*I + 3)^2 + 1/450*(5*sqrt(2) + 5*I - 6)^2

sage: variance([RIF(1.0103, 1.0103), RIF(2)])

0.4897530450000000?

sage: variance([1..6])
 7/2 7/2

7. GeneralDiscreteDistribution(P) # P는 확률 list. 이산확률분포 생성

sage: P = [0.3, 0.4, 0.3] # P(0)=0.3, P(1)=0.4, P(2)=0.3

sage: X = GeneralDiscreteDistribution(P)

sage: X.get_random_element() # random

2

sage: from sage.gsl.probability_distribution import GeneralDiscreteDistribution

sage: P = [0.3, 0.4, 0.3]

sage: X = GeneralDiscreteDistribution(P)

sage: h, b = X.generate_histogram_data(bins = 10) # histogram data 생성

sage: h # random

[1.5249999999999995, 0.0, 0.0, 0.0, 0.0, 1.8649999999999995, 0.0, 0.0, 0.0, 1.6099999999999994]

sage: b # random

[0.0, 0.20000000000000001, 0.40000000000000002, 0.60000000000000009, 0.80000000000000004, 1.0, 1.2000000000000002, 1.4000000000000001, 1.6000000000000001, 1.8, 2.0]

sage: P = [0.3, 0.4, 0.3] # P(0)=0.3, P(1)=0.4, P(2)=0.3 sage: X = GeneralDiscreteDistribution(P) sage: X.get_random_element() # random
 1 1

8. # 균등분포 Uniform distribution on the interval [a, b]:

sage: a = 0

sage: b = 2

sage: T = RealDistribution('uniform', [a, b])

sage: T.get_random_element() # random

0.416921074037

sage: T.distribution_function(0)

0.5

sage: T.cum_distribution_function(1)

0.5

sage: T.cum_distribution_function_inv(.5)

1.0

sage: a = 0 sage: b = 2 sage: T = RealDistribution('uniform', [a, b]) sage: T.get_random_element() # random
 0.497020253912 0.497020253912

9. 가우스 분포 The standard gaussian distribution has sigma = 1:

sage: sigma = 1

sage: T = RealDistribution('gaussian', sigma)

sage: T.get_random_element() # random

0.818610064197

sage: T.distribution_function(0)

0.398942280401

sage: T.cum_distribution_function(1)

0.841344746069

sage: T.cum_distribution_function_inv(.5)

0.0

sage: sigma = 1 sage: T = RealDistribution('gaussian', sigma) sage: T.get_random_element() # random
 -0.183537311896 -0.183537311896

10. t-분포 The t-distribution has one parameter nu:

sage: nu = 1

sage: T = RealDistribution('t', nu)

sage: T.get_random_element() # random

-0.994514581164

sage: T.distribution_function(0)

0.318309886184

sage: T.cum_distribution_function(1)

0.75

sage: T.cum_distribution_function_inv(.5)

0.0

sage: nu = 1 sage: T = RealDistribution('t', nu) sage: T.get_random_element() # random
 -0.994514581164 -0.994514581164

11. F-분포 The F-distribution has two parameters nu1 and nu2:

sage: nu1 = 9; nu2 = 17

sage: F = RealDistribution('F', [nu1,nu2])

sage: F.get_random_element() # random

1.65211335491

sage: F.distribution_function(1)

0.669502550519

sage: F.cum_distribution_function(3.68)

0.98997177723

sage: F.cum_distribution_function_inv(0.99)

3.68224152405

sage: nu1 = 9; nu2 = 17

12. 카이제곱분포 The chi-squared distribution has one parameter nu:

sage: nu = 1

sage: T = RealDistribution('chisquared', nu)

sage: T.get_random_element() # random

0.103230507883

sage: T.distribution_function(0)

+infinity

sage: T.cum_distribution_function(1)

0.682689492137

sage: T.cum_distribution_function_inv(.5)

0.45493642312

13. 지수분포 The exponential power distribution has two parameters a and b:

sage: a = 1

sage: b = 2.5

sage: T = RealDistribution('exppow', [a, b])

sage: T.get_random_element() # random

0.570108609774

sage: T.distribution_function(0)

0.563530248993

sage: T.cum_distribution_function(1)

0.940263052543

sage: a = 1 sage: b = 2.5 sage: T = RealDistribution('exppow', [a, b]) sage: T.get_random_element() # random
 0.690946431418 0.690946431418

14. 회귀직선

R = [[1,2],[3.45,4],[6,5],[4,3]]

R = [[1,2],[3.45,4],[6,5],[4,3]] var('a,b') model(x) = a*x+b print find_fit(R,model) points(R)+plot(model(a=find_fit(R,model)[0].rhs(),b=find_fit(R,model)[1].rhs()),(x,0,10),color='red')
 [a == 0.56881365890949032, b == 1.4451606559020047]  [a == 0.56881365890949032, b == 1.4451606559020047] 

http://sage.math.canterbury.ac.nz/home/pub/76/

http://sage.math.canterbury.ac.nz/home/pub/72/