forked from sergey-alekseev/bsu-ssm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_2.rb
74 lines (60 loc) · 1.51 KB
/
2_2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# encoding: utf-8
N = 1000
# Матожидание
def m(a)
a.inject(:+).to_f / N
end
# Бернулли
def bernulli(p)
rand() < p ? 1 : 0
end
p = 0.7
a = (0...N).to_a.map { bernulli(p) }
p "Бернулли: #{a}"
# Проверка
mu = p
d = p * (1-p)
m = m(a)
ksi1 = m - mu
s2 = a.map { |ai| (ai - m) ** 2 }.inject(:+) / (N - 1)
ksi2 = s2 - d
puts "ksi1: #{ksi1}, ksi2: #{ksi2}"
# Биномиальное
def binomial(m, p)
a = (0...m).to_a.map { rand() }
x = a.map{ |ai| l(p - ai) }.inject(:+)
end
def l(z)
z <= 0 ? 0 : 1
end
m, p = 5, 0.25
a = (0...N).to_a.map { binomial(m, p) }
p "Биномиальное: #{a}"
# Проверка
mu = m * p
d = m * p * (1-p)
m = m(a)
ksi1 = m - mu
s2 = a.map { |ai| (ai - m) ** 2 }.inject(:+) / (N - 1)
ksi2 = s2 - d
puts "ksi1: #{ksi1}, ksi2: #{ksi2}"
# Однородная цепь Маркова
S = [ 0, 1, 2 ]
T = 1000
Pi = [ 0.33333, 0.33333, 0.33334 ]
P = [ [ 0.1, 0.2, 0.7 ], [ 0.3, 0.4, 0.3 ], [ 0.6, 0.2, 0.2 ] ]
def markov(p)
q = [ 0, p[0], p[0..1].inject(:+), p[0..2].inject(:+) ]
a = rand()
(1..3).each { |i| return S[i-1] if (q[i-1]...q[i]).member?(a) }
end
def count(a)
h = Hash.new
a.uniq.each { |e| h[e] = a.count(e).to_f / a.count }
h
end
p "Markov Pi: #{count( (0...T).to_a.map { markov(Pi) } )}"
a = [ markov(Pi) ]
(1...T).each { |i| a << markov(P[S.index(a[i-1])]) }
puts "Вывести средние доли нулей, единиц и двоек в n смоделированных последовательностях."
p count(a)