Top Level Namespace

Defined Under Namespace

Modules: GeneratorInstructions, MigrationGenerator, ModelUpdater Classes: Sigma, SigmaGenerator

Instance Method Summary collapse

Instance Method Details

#define_match_data(difficult) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/generators/templates/resource_model.rb', line 54

def define_match_data(difficult)
  @expectation          = (difficult == 0) ? 0 : difficult > 0
  @resource_probability = probability(@expectation)

  if difficult == 0 || difficult < 2.5*Sigma::SCALE/100 && difficult > 0
    @difficult = 2.5*Sigma::SCALE/100
  elsif difficult > -2.5*Sigma::SCALE/100 && difficult < 0
    @difficult = -2.5*Sigma::SCALE/100
  else
    @difficult = difficult.abs
  end
  @alpha = (@difficult / Sigma::SCALE).abs
end

#lost(match_difficult) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/generators/templates/resource_model.rb', line 40

def lost(match_difficult)
  define_match_data(match_difficult)

  if !@expectation
    skill_lost = self.skill/@alpha/Sigma::SCALE/(Sigma::SCALE+@difficult.abs/@alpha)
  else
    skill_lost = self.skill * @alpha
  end
  self.skill = self.skill - skill_lost
  update_sigma(false)
  self.increment :losses
  self.save
end

#matchesObject



21
22
23
# File 'lib/generators/templates/resource_model.rb', line 21

def matches
  self.wins + self.losses + self.draws
end

#positionObject



17
18
19
# File 'lib/generators/templates/resource_model.rb', line 17

def position
  self.class.ranking.index(self) + 1
end

#probability(expectation) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/generators/templates/resource_model.rb', line 85

def probability(expectation)
  result       = (expectation == true) ? 'wins' : nil
  result       ||= (expectation == false) ? 'losses' : 'draws'

  exp_result   = (expectation == true) ? :we : nil
  exp_result   ||= (expectation == false) ? :le : :de

  w = self.wins   * 100.0 / ((matches == 0) ? 1 : matches)
  l = self.losses * 100.0 / ((matches == 0) ? 1 : matches)
  d = self.draws  * 100.0 / ((matches == 0) ? 1 : matches)

  expectations = {
                    'wins'   =>  { we:0, le:0, de:0 },
                    'losses' =>  { we:0, le:0, de:0 },
                    'draws'  =>  { we:0, le:0, de:0 }
                 }

  expectations.each do |k, v|
    mult = (k == 'wins') ? w : nil
    mult ||= (k == 'losses') ? l : d

    v[:we] = mult*(self.expectations['win_expectation'][k]*100.0/((self[k] == 0) ? 1 : self[k]))
    v[:le] = mult*(self.expectations['lost_expectation'][k]*100.0/((self[k] == 0) ? 1 : self[k]))
    v[:de] = mult*(self.expectations['draw_expectation'][k]*100.0/((self[k] == 0) ? 1 : self[k]))
  end

  all_probabilities = expectations[result][:we]+expectations[result][:le]+expectations[result][:de]
  probability       = expectations[result][exp_result] / ((all_probabilities == 0) ? 1 : all_probabilities)
  (probability == 0) ? 0.25 : probability
end

#ratingObject



13
14
15
# File 'lib/generators/templates/resource_model.rb', line 13

def rating
  self.skill - 3 * self.doubt
end

#update_sigma(exp) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/generators/templates/resource_model.rb', line 68

def update_sigma(exp)
  exp_result    = (@expectation == true) ? 'win_expectation' : nil
  exp_result    ||= (@expectation == false) ? 'lost_expectation' : 'draw_expectation'
  result        = (exp == true) ? 'wins' : nil
  result        ||= (exp == false) ? 'losses' : 'draws'
  expectations  = self.expectations[exp_result][result]

  self.expectations[exp_result][result] = expectations + 1
  if @expectation == exp
    salpha     = (1 - @resource_probability) * @alpha
    self.doubt = self.doubt - self.doubt * salpha
  else
    salpha     = @resource_probability * @alpha
    self.doubt = self.doubt + self.doubt * salpha
  end
end

#won(match_difficult) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/generators/templates/resource_model.rb', line 25

def won(match_difficult)
  define_match_data(match_difficult)

  if @expectation == true
    skill_won = self.skill/@alpha/Sigma::SCALE/(Sigma::SCALE+@difficult/@alpha)
  else
    skill_won = self.skill * @alpha
  end

  self.skill = self.skill + skill_won
  update_sigma(true)
  self.increment :wins
  self.save
end