asianscore_quant 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +64 -0
- data/asianscore_quant.gemspec +27 -0
- data/lib/asianscore_quant/models.rb +237 -0
- data/lib/asianscore_quant/version.rb +5 -0
- data/lib/asianscore_quant.rb +8 -0
- data/test/test_asianscore_quant.rb +100 -0
- metadata +59 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 219fd184d2e9dddfdbd59afa1f4233de80b4f7e09d0848be8ff0bbb1e2bc57b8
|
|
4
|
+
data.tar.gz: 9b79feb15df18e5e613b90e618b30b39cd24bb9f5a58737bd193f942c1ce7043
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 54da2eb420c60417d6cf27ea5be9f3551c2b9207a6ef896a5ea742001e2645de0e7dc03b0620840975c554203995b0b62ad2f50096e835874e6266e7f325d362
|
|
7
|
+
data.tar.gz: 808d1c3c708e07e91b2c38838ef6a2f0eb9a445ae0d4eaf1039f5c9a78aa376c5d99f6c7c6ea9f310a116903c0bba08ebde73c045c920dfbd01436758b3c48f9
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 AsianScore Quantitative Research Lab
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# asianscore_quant
|
|
2
|
+
|
|
3
|
+
Zero-dependency Ruby quantitative sports analytics SDK and live prediction ledger client powered by [AsianScore](https://asianscore.com/).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
gem install asianscore_quant
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or add to your `Gemfile`:
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
gem "asianscore_quant", "~> 1.0"
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Quickstart
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
require "asianscore_quant"
|
|
21
|
+
|
|
22
|
+
# 1. Remove bookmaker overround (Multiplicative & Power devigging)
|
|
23
|
+
odds = [1.85, 3.60, 4.40]
|
|
24
|
+
fair_mult = AsianscoreQuant.devig_multiplicative(odds)
|
|
25
|
+
fair_power = AsianscoreQuant.devig_power(odds)
|
|
26
|
+
|
|
27
|
+
# 2. Fractional Kelly bankroll sizing (default quarter-Kelly, fraction: 0.25)
|
|
28
|
+
stake = AsianscoreQuant.kelly_fraction(fair_power[0], 1.95, fraction: 0.25)
|
|
29
|
+
|
|
30
|
+
# 3. Dixon-Coles low-score adjusted bivariate Poisson probabilities
|
|
31
|
+
probs = AsianscoreQuant.dixon_coles_probs(1.65, 1.15, rho: -0.08)
|
|
32
|
+
# => { home_win: ..., draw: ..., away_win: ..., over_2_5: ..., btts: ... }
|
|
33
|
+
|
|
34
|
+
# 4. Multi-class Brier score calibration evaluation
|
|
35
|
+
brier = AsianscoreQuant.brier_score([probs[:home_win], probs[:draw], probs[:away_win]], 0)
|
|
36
|
+
|
|
37
|
+
# 5. Fetch live fixtures and SHA-256 verifiable ledger summary
|
|
38
|
+
summary = AsianscoreQuant.fetch_live_summary
|
|
39
|
+
puts "Settled: #{summary.dig('ledger', 'settled')}, Avg Brier: #{summary.dig('ledger', 'avgBrier')}"
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## API Reference
|
|
43
|
+
|
|
44
|
+
- `AsianscoreQuant.devig_multiplicative(odds)` — Proportional implied probability normalization.
|
|
45
|
+
- `AsianscoreQuant.devig_power(odds)` — Power-law overround removal via binary search solver for $\sum (1/o_i)^k = 1$.
|
|
46
|
+
- `AsianscoreQuant.kelly_fraction(prob, decimal_odds, fraction: 0.25)` — Clamped fractional Kelly criterion stake sizing.
|
|
47
|
+
- `AsianscoreQuant.dixon_coles_probs(lambda_home, lambda_away, rho: -0.08)` — Full bivariate Poisson grid with Dixon-Coles $\tau$ low-score adjustment returning `:home_win`, `:draw`, `:away_win`, `:over_2_5`, and `:btts`.
|
|
48
|
+
- `AsianscoreQuant.brier_score(probs, actual_index)` — Multi-class Brier score (`(1/N) * sum((p_i - y_i)^2)`).
|
|
49
|
+
- `AsianscoreQuant.fetch_live_summary` — Pure stdlib (`net/http` + `json`) client for `https://asianscore.com/api/widget/summary`.
|
|
50
|
+
|
|
51
|
+
## Ecosystem & References
|
|
52
|
+
|
|
53
|
+
- **AsianScore Platform**: [https://asianscore.com/](https://asianscore.com/)
|
|
54
|
+
- **Interactive Quant Calculators**: [https://asianscore.com/tools.html](https://asianscore.com/tools.html)
|
|
55
|
+
- **SHA-256 Verifiable Prediction Ledger**: [https://asianscore.com/models.html#ledger](https://asianscore.com/models.html#ledger)
|
|
56
|
+
- **Zenodo Dataset DOI**: [https://doi.org/10.5281/zenodo.24441036](https://doi.org/10.5281/zenodo.24441036)
|
|
57
|
+
- **Python SDK (PyPI)**: [https://pypi.org/project/asianscore-quant/](https://pypi.org/project/asianscore-quant/)
|
|
58
|
+
- **Web Widget (npm)**: [https://www.npmjs.com/package/asianscore-widget](https://www.npmjs.com/package/asianscore-widget)
|
|
59
|
+
- **Documentation (Read the Docs)**: [https://asianscore-quant.readthedocs.io/en/latest/](https://asianscore-quant.readthedocs.io/en/latest/)
|
|
60
|
+
- **Go SDK (pkg.go.dev)**: [https://pkg.go.dev/github.com/AIYIXIU/asianscore-go](https://pkg.go.dev/github.com/AIYIXIU/asianscore-go)
|
|
61
|
+
|
|
62
|
+
## License
|
|
63
|
+
|
|
64
|
+
MIT License. See [LICENSE](LICENSE) for details.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "lib/asianscore_quant/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "asianscore_quant"
|
|
7
|
+
spec.version = AsianscoreQuant::VERSION
|
|
8
|
+
spec.authors = ["AsianScore Quantitative Research Lab"]
|
|
9
|
+
spec.email = ["rubygems@aimail.men"]
|
|
10
|
+
|
|
11
|
+
spec.summary = "Zero-dependency Ruby SDK for sports quantitative modeling & AsianScore live ledger"
|
|
12
|
+
spec.description = "Zero-dependency Ruby quantitative sports analytics toolkit providing multiplicative and power odds devigging, fractional Kelly bankroll sizing, Dixon-Coles bivariate Poisson match probabilities, multi-class Brier score calibration, and a live AsianScore summary API client."
|
|
13
|
+
spec.homepage = "https://asianscore.com/"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
spec.required_ruby_version = ">= 2.7.0"
|
|
16
|
+
|
|
17
|
+
spec.metadata = {
|
|
18
|
+
"homepage_uri" => "https://asianscore.com/",
|
|
19
|
+
"documentation_uri" => "https://asianscore.com/tools.html",
|
|
20
|
+
"source_code_uri" => "https://github.com/AIYIXIU/asianscore-ruby",
|
|
21
|
+
"changelog_uri" => "https://asianscore.com/models.html#ledger",
|
|
22
|
+
"bug_tracker_uri" => "https://github.com/AIYIXIU/asianscore-ruby/issues"
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
spec.files = Dir.glob("{lib,test}/**/*").select { |f| File.file?(f) } + %w[README.md LICENSE asianscore_quant.gemspec]
|
|
26
|
+
spec.require_paths = ["lib"]
|
|
27
|
+
end
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module AsianscoreQuant
|
|
8
|
+
# Zero-dependency sports quantitative modeling primitives and live summary client
|
|
9
|
+
# for AsianScore (https://asianscore.com/).
|
|
10
|
+
module Models
|
|
11
|
+
extend self
|
|
12
|
+
|
|
13
|
+
DEFAULT_SUMMARY_URL = "https://asianscore.com/api/widget/summary"
|
|
14
|
+
|
|
15
|
+
# Remove bookmaker overround proportionally from decimal odds.
|
|
16
|
+
#
|
|
17
|
+
# @param odds [Array<Numeric>] Decimal odds (> 1.0) for all mutually exclusive outcomes.
|
|
18
|
+
# @return [Array<Float>] Fair probabilities summing to 1.0.
|
|
19
|
+
def devig_multiplicative(odds)
|
|
20
|
+
implied = extract_implied_probs(odds)
|
|
21
|
+
total = implied.sum
|
|
22
|
+
implied.map { |p| p / total }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Remove bookmaker overround using the power method: solves sum((1/o)^k) = 1.0 via binary search.
|
|
26
|
+
#
|
|
27
|
+
# @param odds [Array<Numeric>] Decimal odds (> 1.0) for all mutually exclusive outcomes.
|
|
28
|
+
# @param tol [Float] Convergence tolerance for the root solver.
|
|
29
|
+
# @param max_iter [Integer] Maximum binary search iterations.
|
|
30
|
+
# @return [Array<Float>] Fair probabilities summing to 1.0.
|
|
31
|
+
def devig_power(odds, tol: 1e-12, max_iter: 200)
|
|
32
|
+
implied = extract_implied_probs(odds)
|
|
33
|
+
initial_sum = implied.sum
|
|
34
|
+
return implied.map { |p| p / initial_sum } if (initial_sum - 1.0).abs <= tol
|
|
35
|
+
|
|
36
|
+
if initial_sum > 1.0
|
|
37
|
+
low = 1.0
|
|
38
|
+
high = 2.0
|
|
39
|
+
high *= 2.0 while implied.sum { |p| p**high } > 1.0 && high < 1_000_000.0
|
|
40
|
+
else
|
|
41
|
+
low = 0.0
|
|
42
|
+
high = 1.0
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
max_iter.times do
|
|
46
|
+
mid = 0.5 * (low + high)
|
|
47
|
+
val = implied.sum { |p| p**mid }
|
|
48
|
+
if (val - 1.0).abs <= tol || (high - low) <= tol
|
|
49
|
+
low = high = mid
|
|
50
|
+
break
|
|
51
|
+
elsif val > 1.0
|
|
52
|
+
low = mid
|
|
53
|
+
else
|
|
54
|
+
high = mid
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
k = 0.5 * (low + high)
|
|
59
|
+
raw = implied.map { |p| p**k }
|
|
60
|
+
total = raw.sum
|
|
61
|
+
raw.map { |p| p / total }
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Compute fractional Kelly criterion stake sizing.
|
|
65
|
+
#
|
|
66
|
+
# @param prob [Numeric] Estimated fair probability in (0, 1).
|
|
67
|
+
# @param decimal_odds [Numeric] Available decimal odds (> 1.0).
|
|
68
|
+
# @param fraction [Numeric] Kelly multiplier (default 0.25 for quarter-Kelly).
|
|
69
|
+
# @return [Float] Recommended bankroll stake fraction (>= 0.0).
|
|
70
|
+
def kelly_fraction(prob, decimal_odds, fraction: 0.25)
|
|
71
|
+
p = prob.to_f
|
|
72
|
+
odds = decimal_odds.to_f
|
|
73
|
+
scale = fraction.to_f
|
|
74
|
+
|
|
75
|
+
raise ArgumentError, "fraction must be >= 0.0" if scale.negative?
|
|
76
|
+
return 0.0 if p <= 0.0 || p >= 1.0 || odds <= 1.0 || scale.zero?
|
|
77
|
+
|
|
78
|
+
b = odds - 1.0
|
|
79
|
+
full_kelly = (b * p - (1.0 - p)) / b
|
|
80
|
+
return 0.0 if full_kelly <= 0.0
|
|
81
|
+
|
|
82
|
+
full_kelly * scale
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Compute Dixon-Coles low-score adjusted bivariate Poisson match probabilities.
|
|
86
|
+
#
|
|
87
|
+
# @param lambda_home [Numeric] Expected goals for the home team (>= 0.0).
|
|
88
|
+
# @param lambda_away [Numeric] Expected goals for the away team (>= 0.0).
|
|
89
|
+
# @param rho [Numeric] Low-score correlation adjustment parameter (default -0.08).
|
|
90
|
+
# @param max_goals [Integer] Goal truncation ceiling per side (default 15).
|
|
91
|
+
# @return [Hash{Symbol => Float}] :home_win, :draw, :away_win, :over_2_5, and :btts probabilities.
|
|
92
|
+
def dixon_coles_probs(lambda_home, lambda_away, rho: -0.08, max_goals: 15)
|
|
93
|
+
lh = lambda_home.to_f
|
|
94
|
+
la = lambda_away.to_f
|
|
95
|
+
r = rho.to_f
|
|
96
|
+
|
|
97
|
+
raise ArgumentError, "expected goals must be >= 0.0" if lh.negative? || la.negative?
|
|
98
|
+
raise ArgumentError, "max_goals must be >= 1" if max_goals < 1
|
|
99
|
+
|
|
100
|
+
home_pmf = poisson_distribution(lh, max_goals)
|
|
101
|
+
away_pmf = poisson_distribution(la, max_goals)
|
|
102
|
+
|
|
103
|
+
home_win = 0.0
|
|
104
|
+
draw = 0.0
|
|
105
|
+
away_win = 0.0
|
|
106
|
+
over_2_5 = 0.0
|
|
107
|
+
btts = 0.0
|
|
108
|
+
total = 0.0
|
|
109
|
+
|
|
110
|
+
(0..max_goals).each do |h|
|
|
111
|
+
ph = home_pmf[h]
|
|
112
|
+
(0..max_goals).each do |a|
|
|
113
|
+
tau = dixon_coles_tau(h, a, lh, la, r)
|
|
114
|
+
prob = ph * away_pmf[a] * tau
|
|
115
|
+
prob = 0.0 if prob.negative?
|
|
116
|
+
|
|
117
|
+
total += prob
|
|
118
|
+
if h > a
|
|
119
|
+
home_win += prob
|
|
120
|
+
elsif h == a
|
|
121
|
+
draw += prob
|
|
122
|
+
else
|
|
123
|
+
away_win += prob
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
over_2_5 += prob if (h + a) > 2
|
|
127
|
+
btts += prob if h.positive? && a.positive?
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
raise ArgumentError, "degenerate probability grid" if total <= 0.0
|
|
132
|
+
|
|
133
|
+
{
|
|
134
|
+
home_win: home_win / total,
|
|
135
|
+
draw: draw / total,
|
|
136
|
+
away_win: away_win / total,
|
|
137
|
+
over_2_5: over_2_5 / total,
|
|
138
|
+
btts: btts / total
|
|
139
|
+
}
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Compute the multi-class Brier score (mean squared error across outcome probabilities).
|
|
143
|
+
#
|
|
144
|
+
# @param probs [Array<Numeric>] Predicted probability vector.
|
|
145
|
+
# @param actual_index [Integer] Zero-based index of the realized outcome.
|
|
146
|
+
# @return [Float] Multi-class Brier score in [0.0, 2.0 / n].
|
|
147
|
+
def brier_score(probs, actual_index)
|
|
148
|
+
raise ArgumentError, "probs must be a non-empty Array" unless probs.is_a?(Array) && !probs.empty?
|
|
149
|
+
unless actual_index.is_a?(Integer) && actual_index >= 0 && actual_index < probs.length
|
|
150
|
+
raise ArgumentError, "actual_index out of range"
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
squared_error = 0.0
|
|
154
|
+
probs.each_with_index do |prob, idx|
|
|
155
|
+
target = idx == actual_index ? 1.0 : 0.0
|
|
156
|
+
diff = prob.to_f - target
|
|
157
|
+
squared_error += diff * diff
|
|
158
|
+
end
|
|
159
|
+
squared_error / probs.length.to_f
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# Fetch live fixtures and SHA-256 verifiable ledger summary from AsianScore.
|
|
163
|
+
#
|
|
164
|
+
# @param url [String] Summary endpoint URL.
|
|
165
|
+
# @param timeout [Numeric] HTTP open and read timeout in seconds.
|
|
166
|
+
# @return [Hash] Parsed JSON payload.
|
|
167
|
+
def fetch_live_summary(url: DEFAULT_SUMMARY_URL, timeout: 15)
|
|
168
|
+
current_url = url
|
|
169
|
+
3.times do
|
|
170
|
+
uri = URI.parse(current_url)
|
|
171
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
172
|
+
http.use_ssl = (uri.scheme == "https")
|
|
173
|
+
http.open_timeout = timeout
|
|
174
|
+
http.read_timeout = timeout
|
|
175
|
+
|
|
176
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
|
177
|
+
request["User-Agent"] = "asianscore_quant/#{AsianscoreQuant::VERSION} (+https://asianscore.com/)"
|
|
178
|
+
request["Accept"] = "application/json"
|
|
179
|
+
|
|
180
|
+
response = http.request(request)
|
|
181
|
+
if response.is_a?(Net::HTTPRedirection) && response["location"]
|
|
182
|
+
current_url = URI.join(current_url, response["location"]).to_s
|
|
183
|
+
next
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
unless response.is_a?(Net::HTTPSuccess)
|
|
187
|
+
raise "AsianScore API returned HTTP #{response.code} #{response.message}"
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
return JSON.parse(response.body)
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
raise "Too many HTTP redirects while fetching #{url}"
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
private
|
|
197
|
+
|
|
198
|
+
def extract_implied_probs(odds)
|
|
199
|
+
raise ArgumentError, "odds must be a non-empty Array" unless odds.is_a?(Array) && !odds.empty?
|
|
200
|
+
|
|
201
|
+
odds.map do |o|
|
|
202
|
+
val = o.to_f
|
|
203
|
+
raise ArgumentError, "all decimal odds must be > 1.0" unless o.is_a?(Numeric) && val > 1.0
|
|
204
|
+
|
|
205
|
+
1.0 / val
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def poisson_distribution(lambda, max_goals)
|
|
210
|
+
pmf = Array.new(max_goals + 1, 0.0)
|
|
211
|
+
if lambda.zero?
|
|
212
|
+
pmf[0] = 1.0
|
|
213
|
+
return pmf
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
pmf[0] = Math.exp(-lambda)
|
|
217
|
+
(1..max_goals).each do |k|
|
|
218
|
+
pmf[k] = pmf[k - 1] * lambda / k.to_f
|
|
219
|
+
end
|
|
220
|
+
pmf
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def dixon_coles_tau(home_goals, away_goals, lambda_home, lambda_away, rho)
|
|
224
|
+
if home_goals.zero? && away_goals.zero?
|
|
225
|
+
1.0 - lambda_home * lambda_away * rho
|
|
226
|
+
elsif home_goals.zero? && away_goals == 1
|
|
227
|
+
1.0 + lambda_home * rho
|
|
228
|
+
elsif home_goals == 1 && away_goals.zero?
|
|
229
|
+
1.0 + lambda_away * rho
|
|
230
|
+
elsif home_goals == 1 && away_goals == 1
|
|
231
|
+
1.0 - rho
|
|
232
|
+
else
|
|
233
|
+
1.0
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
end
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
$LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
|
|
4
|
+
require "asianscore_quant"
|
|
5
|
+
|
|
6
|
+
begin
|
|
7
|
+
require "minitest/autorun"
|
|
8
|
+
TEST_BASE = Minitest::Test
|
|
9
|
+
rescue LoadError
|
|
10
|
+
# Pure-stdlib fallback runner when minitest is unavailable
|
|
11
|
+
class PureStdlibTest
|
|
12
|
+
def self.inherited(subclass)
|
|
13
|
+
(@subclasses ||= []) << subclass
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.run_all!
|
|
17
|
+
failures = 0
|
|
18
|
+
tests = 0
|
|
19
|
+
(@subclasses || []).each do |klass|
|
|
20
|
+
instance = klass.new
|
|
21
|
+
klass.instance_methods(false).grep(/^test_/).sort.each do |method_name|
|
|
22
|
+
tests += 1
|
|
23
|
+
instance.public_send(method_name)
|
|
24
|
+
rescue StandardError => e
|
|
25
|
+
failures += 1
|
|
26
|
+
warn "FAIL #{klass}##{method_name}: #{e.message}"
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
puts "#{tests} tests, #{failures} failures"
|
|
30
|
+
exit(1) if failures.positive?
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def assert(condition, msg = "Expected condition to be truthy")
|
|
34
|
+
raise msg unless condition
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def assert_in_delta(expected, actual, delta = 1e-6, msg = nil)
|
|
38
|
+
diff = (expected - actual).abs
|
|
39
|
+
raise(msg || "Expected #{actual} to be within #{delta} of #{expected}") if diff > delta
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def assert_raises(error_class)
|
|
43
|
+
yield
|
|
44
|
+
raise "Expected #{error_class} to be raised"
|
|
45
|
+
rescue error_class
|
|
46
|
+
true
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
at_exit { PureStdlibTest.run_all! }
|
|
51
|
+
TEST_BASE = PureStdlibTest
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
class TestAsianscoreQuant < TEST_BASE
|
|
55
|
+
def test_devig_multiplicative_sums_to_one
|
|
56
|
+
probs = AsianscoreQuant.devig_multiplicative([1.90, 3.50, 4.20])
|
|
57
|
+
assert_in_delta 1.0, probs.sum, 1e-12
|
|
58
|
+
assert probs[0] > probs[1] && probs[1] > probs[2]
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def test_devig_power_sums_to_one_and_handles_longshot_bias
|
|
62
|
+
odds = [1.45, 4.60, 7.50]
|
|
63
|
+
mult = AsianscoreQuant.devig_multiplicative(odds)
|
|
64
|
+
power = AsianscoreQuant.devig_power(odds)
|
|
65
|
+
|
|
66
|
+
assert_in_delta 1.0, power.sum, 1e-12
|
|
67
|
+
# Power devigging assigns higher fair probability to the favorite than multiplicative
|
|
68
|
+
assert power[0] > mult[0]
|
|
69
|
+
assert power[2] < mult[2]
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def test_devig_rejects_invalid_odds
|
|
73
|
+
assert_raises(ArgumentError) { AsianscoreQuant.devig_multiplicative([]) }
|
|
74
|
+
assert_raises(ArgumentError) { AsianscoreQuant.devig_power([1.0, 2.5]) }
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def test_kelly_fraction
|
|
78
|
+
# Fair prob 0.55 at even odds (2.0) -> full Kelly 0.10, quarter-Kelly 0.025
|
|
79
|
+
assert_in_delta 0.025, AsianscoreQuant.kelly_fraction(0.55, 2.0), 1e-12
|
|
80
|
+
assert_in_delta 0.10, AsianscoreQuant.kelly_fraction(0.55, 2.0, fraction: 1.0), 1e-12
|
|
81
|
+
# Negative edge should return 0.0
|
|
82
|
+
assert_in_delta 0.0, AsianscoreQuant.kelly_fraction(0.40, 2.0), 1e-12
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def test_dixon_coles_probs
|
|
86
|
+
res = AsianscoreQuant.dixon_coles_probs(1.65, 1.15, rho: -0.08)
|
|
87
|
+
total_1x2 = res[:home_win] + res[:draw] + res[:away_win]
|
|
88
|
+
|
|
89
|
+
assert_in_delta 1.0, total_1x2, 1e-12
|
|
90
|
+
assert res[:home_win] > res[:away_win]
|
|
91
|
+
assert res[:over_2_5] > 0.0 && res[:over_2_5] < 1.0
|
|
92
|
+
assert res[:btts] > 0.0 && res[:btts] < 1.0
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def test_brier_score
|
|
96
|
+
assert_in_delta 0.0, AsianscoreQuant.brier_score([1.0, 0.0, 0.0], 0), 1e-12
|
|
97
|
+
# ((0.7-1)^2 + 0.2^2 + 0.1^2) / 3 = 0.14 / 3
|
|
98
|
+
assert_in_delta 0.14 / 3.0, AsianscoreQuant.brier_score([0.7, 0.2, 0.1], 0), 1e-12
|
|
99
|
+
end
|
|
100
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: asianscore_quant
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- AsianScore Quantitative Research Lab
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-11-22 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Zero-dependency Ruby quantitative sports analytics toolkit providing
|
|
14
|
+
multiplicative and power odds devigging, fractional Kelly bankroll sizing, Dixon-Coles
|
|
15
|
+
bivariate Poisson match probabilities, multi-class Brier score calibration, and
|
|
16
|
+
a live AsianScore summary API client.
|
|
17
|
+
email:
|
|
18
|
+
- rubygems@aimail.men
|
|
19
|
+
executables: []
|
|
20
|
+
extensions: []
|
|
21
|
+
extra_rdoc_files: []
|
|
22
|
+
files:
|
|
23
|
+
- LICENSE
|
|
24
|
+
- README.md
|
|
25
|
+
- asianscore_quant.gemspec
|
|
26
|
+
- lib/asianscore_quant.rb
|
|
27
|
+
- lib/asianscore_quant/models.rb
|
|
28
|
+
- lib/asianscore_quant/version.rb
|
|
29
|
+
- test/test_asianscore_quant.rb
|
|
30
|
+
homepage: https://asianscore.com/
|
|
31
|
+
licenses:
|
|
32
|
+
- MIT
|
|
33
|
+
metadata:
|
|
34
|
+
homepage_uri: https://asianscore.com/
|
|
35
|
+
documentation_uri: https://asianscore.com/tools.html
|
|
36
|
+
source_code_uri: https://github.com/AIYIXIU/asianscore-ruby
|
|
37
|
+
changelog_uri: https://asianscore.com/models.html#ledger
|
|
38
|
+
bug_tracker_uri: https://github.com/AIYIXIU/asianscore-ruby/issues
|
|
39
|
+
post_install_message:
|
|
40
|
+
rdoc_options: []
|
|
41
|
+
require_paths:
|
|
42
|
+
- lib
|
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 2.7.0
|
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - ">="
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: '0'
|
|
53
|
+
requirements: []
|
|
54
|
+
rubygems_version: 3.5.22
|
|
55
|
+
signing_key:
|
|
56
|
+
specification_version: 4
|
|
57
|
+
summary: Zero-dependency Ruby SDK for sports quantitative modeling & AsianScore live
|
|
58
|
+
ledger
|
|
59
|
+
test_files: []
|