hkjc 0.3.19__py3-none-any.whl → 0.3.21__py3-none-any.whl

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.
hkjc/features.py CHANGED
@@ -1,4 +1,4 @@
1
- """ Polars expressions to commonly-used analysis features, subject to frequent changes.
1
+ """ Polars expressions for commonly-used analysis features, subject to frequent changes.
2
2
  """
3
3
  import polars as pl
4
4
 
hkjc/harville_model.py CHANGED
@@ -198,10 +198,12 @@ class HarvilleModel:
198
198
  lambda_qin (float): Weight for Qin pool loss
199
199
  lambda_quinella (float): Weight for Quinella pool loss
200
200
  lambda_banker (float): Weight for Banker pool loss
201
+ takeout_rate (float): House take out rate (e.g., 0.175 = 17.5%)
201
202
  """
202
203
 
203
204
  def __init__(self, n_horses: int, lambda_win: float = LAMBDA_DEFAULTS['WIN'], lambda_qin: float = LAMBDA_DEFAULTS['QIN'],
204
- lambda_quinella: float = LAMBDA_DEFAULTS['QPL'], lambda_banker: float = LAMBDA_DEFAULTS['PLA']) -> None:
205
+ lambda_quinella: float = LAMBDA_DEFAULTS['QPL'], lambda_banker: float = LAMBDA_DEFAULTS['PLA'],
206
+ takeout_rate: float = 0.175) -> None:
205
207
  """
206
208
  Initialize model.
207
209
 
@@ -211,6 +213,9 @@ class HarvilleModel:
211
213
  lambda_qin: Weight for Qin odds (prob pair finishes 1st-2nd)
212
214
  lambda_quinella: Weight for Quinella odds (prob pair in top 3)
213
215
  lambda_banker: Weight for Banker odds (prob horse in top 3)
216
+ takeout_rate: House take out rate as decimal (default 0.175 = 17.5%).
217
+ The observed odds include the house's take, which makes
218
+ them higher than true odds. This parameter adjusts for that.
214
219
 
215
220
  Raises:
216
221
  ValueError: If n_horses > 20 (exponential complexity warning)
@@ -223,9 +228,51 @@ class HarvilleModel:
223
228
  self.lambda_qin = lambda_qin
224
229
  self.lambda_quinella = lambda_quinella
225
230
  self.lambda_banker = lambda_banker
231
+ self.takeout_rate = takeout_rate
226
232
  self._eval_count = 0
227
233
  self.result = None
228
234
 
235
+ def _adjust_for_takeout(self, probs: Optional[np.ndarray]) -> Optional[np.ndarray]:
236
+ """
237
+ Adjust observed probabilities to remove house takeout rate.
238
+
239
+ Observed odds from the betting market include the house's take, causing
240
+ the sum of implied probabilities to exceed 1.0. This method adjusts them
241
+ to represent true probabilities.
242
+
243
+ Args:
244
+ probs: Observed probabilities (can be 1D or 2D array)
245
+
246
+ Returns:
247
+ Adjusted probabilities with takeout removed, or None if input is None
248
+ """
249
+ if probs is None:
250
+ return None
251
+
252
+ # Multiply by (1 - takeout_rate) to remove the house edge
253
+ adjusted = probs * (1.0 - self.takeout_rate)
254
+
255
+ return adjusted
256
+
257
+ def _probs_to_market_odds(self, probs: np.ndarray) -> np.ndarray:
258
+ """
259
+ Convert fitted probabilities to market odds including takeout rate.
260
+
261
+ This converts true probabilities (which sum to 1.0) back to decimal odds
262
+ as they would appear in the betting market, which includes the house's
263
+ takeout rate. The resulting odds can be directly compared to observed odds.
264
+
265
+ Args:
266
+ probs: Fitted probabilities (1D or 2D array)
267
+
268
+ Returns:
269
+ Market odds (decimal format) with takeout reintroduced
270
+ """
271
+
272
+ # Convert true probabilities to market odds with takeout
273
+ # Market odds are worse (higher) than fair odds due to house edge
274
+ return (1.0 - self.takeout_rate) / probs
275
+
229
276
  def _loss(self, theta: np.ndarray, W_obs: Optional[np.ndarray],
230
277
  Qin_obs: Optional[np.ndarray], Q_obs: Optional[np.ndarray],
231
278
  b_obs: Optional[np.ndarray]) -> float:
@@ -290,26 +337,34 @@ class HarvilleModel:
290
337
 
291
338
  Returns:
292
339
  Dictionary containing:
293
- - theta: Fitted strength parameters (n,)
294
- - W_fitted: Fitted Win probabilities (n,)
295
- - Qin_fitted: Fitted Qin probabilities (n, n)
296
- - Q_fitted: Fitted Quinella probabilities (n, n)
297
- - b_fitted: Fitted Banker probabilities (n,)
298
- - P_fitted: Full place probability matrix (n, n), P[i,j] =
299
- prob horse i finishes in position j
300
- - loss: Final loss value
301
340
  - success: Whether optimization converged
302
341
  - message: Optimizer status message
303
342
  - n_eval: Number of loss function evaluations
343
+ - loss: Final loss value
344
+ - prob_fit: Dictionary of fitted probabilities (sum to 1.0)
345
+ - theta: Fitted strength parameters (n,)
346
+ - W: Win probabilities (n,)
347
+ - Qin: Qin probabilities (n, n)
348
+ - Q: Quinella probabilities (n, n)
349
+ - b: Banker probabilities (n,)
350
+ - P: Full place probability matrix (n, n), P[i,j] =
351
+ prob horse i finishes in position j
352
+ - odds_fit: Dictionary of fitted market odds (directly comparable to observed)
353
+ - WIN: Win odds (n,)
354
+ - QIN: Qin odds (n, n)
355
+ - QPL: Quinella Place odds (n, n)
356
+ - PLA: Place odds (n,)
304
357
 
305
358
  Raises:
306
359
  ValueError: If no odds provided or shapes don't match n_horses
307
360
 
308
361
  Example:
309
- >>> opt = HarvilleOptimizer(n_horses=10)
362
+ >>> opt = HarvilleModel(n_horses=10, takeout_rate=0.175)
310
363
  >>> results = opt.fit(W_obs=win_probs, Q_obs=quinella_probs)
311
- >>> print(f"Fitted strengths: {results['theta']}")
364
+ >>> print(f"Fitted strengths: {results['prob_fit']['theta']}")
312
365
  >>> print(f"Converged: {results['success']}")
366
+ >>> # Compare fitted odds to observed odds
367
+ >>> diff = results['odds_fit']['WIN'] - observed_win_odds
313
368
  """
314
369
  if W_obs is None and Qin_obs is None and Q_obs is None and b_obs is None:
315
370
  raise ValueError("At least one type of odds must be provided")
@@ -323,6 +378,12 @@ class HarvilleModel:
323
378
  if b_obs is not None and b_obs.shape != (self.n,):
324
379
  raise ValueError(f"b_obs must be ({self.n},)")
325
380
 
381
+ # Adjust observed probabilities for house takeout rate
382
+ W_obs = self._adjust_for_takeout(W_obs)
383
+ Qin_obs = self._adjust_for_takeout(Qin_obs)
384
+ Q_obs = self._adjust_for_takeout(Q_obs)
385
+ b_obs = self._adjust_for_takeout(b_obs)
386
+
326
387
  if theta_init is None:
327
388
  if W_obs is not None:
328
389
  theta_init = W_obs / W_obs.sum()
@@ -356,27 +417,41 @@ class HarvilleModel:
356
417
 
357
418
  W_fitted, Qin_fitted, Q_fitted, b_fitted, P_fitted = _compute_probabilities(theta_opt)
358
419
 
420
+ # Convert fitted probabilities to market odds (with takeout reintroduced)
421
+ WIN_odds_fitted = self._probs_to_market_odds(W_fitted)
422
+ PLA_odds_fitted = self._probs_to_market_odds(b_fitted)
423
+ QIN_odds_fitted = self._probs_to_market_odds(Qin_fitted)
424
+ QPL_odds_fitted = self._probs_to_market_odds(Q_fitted)
425
+
359
426
  self.result = {
360
- 'theta': theta_opt,
361
- 'W_fitted': W_fitted,
362
- 'Qin_fitted': Qin_fitted,
363
- 'Q_fitted': Q_fitted,
364
- 'b_fitted': b_fitted,
365
- 'P_fitted': P_fitted,
366
- 'loss': result.fun,
367
427
  'success': result.success,
368
428
  'message': result.message,
369
- 'n_eval': self._eval_count
429
+ 'n_eval': self._eval_count,
430
+ 'loss': result.fun,
431
+ 'prob_fit': {
432
+ 'theta': theta_opt,
433
+ 'W': W_fitted,
434
+ 'Qin': Qin_fitted,
435
+ 'Q': Q_fitted,
436
+ 'b': b_fitted,
437
+ 'P': P_fitted
438
+ },
439
+ 'odds_fit': {
440
+ 'WIN': WIN_odds_fitted,
441
+ 'QIN': QIN_odds_fitted,
442
+ 'QPL': QPL_odds_fitted,
443
+ 'PLA': PLA_odds_fitted
444
+ }
370
445
  }
371
446
 
372
447
  return self.result
373
448
 
374
- def fit_harville_to_odds(odds : dict[str, np.ndarray], lambdas : dict[str, float] = None) -> dict:
449
+ def fit_harville_to_odds(odds : dict[str, np.ndarray], lambdas : dict[str, float] = None, takeout_rate: float = 0.175) -> dict:
375
450
  """
376
451
  Fit Harville model to observed betting odds.
377
452
 
378
- At least one odds type must be provided. All odds should be probabilities
379
- (not decimal/fractional odds). Matrices should be symmetric where applicable.
453
+ At least one odds type must be provided. All odds should be decimal odds
454
+ (not probabilities). Matrices should be symmetric where applicable.
380
455
 
381
456
  Args:
382
457
  odds: Dictionary of odds arrays with types as keys.:
@@ -384,20 +459,35 @@ def fit_harville_to_odds(odds : dict[str, np.ndarray], lambdas : dict[str, float
384
459
  lambdas: Optional dictionary of lambda weights for each odds type.
385
460
  Keys can be 'WIN', 'QIN', 'QPL', 'PLA'. Defaults to
386
461
  {'WIN': 1.0, 'QIN': 2.0, 'QPL': 1.5, 'PLA': 0.7}
462
+ takeout_rate: House take out rate as decimal (default 0.175 = 17.5%).
463
+ The house keeps this percentage of the betting pool, causing
464
+ observed odds to be higher than fair odds.
387
465
 
388
466
  Returns:
389
467
  Dictionary containing:
390
- - theta: Fitted strength parameters (n,)
391
- - W_fitted: Fitted Win probabilities (n,)
392
- - Qin_fitted: Fitted Qin probabilities (n, n)
393
- - Q_fitted: Fitted Quinella probabilities (n, n)
394
- - b_fitted: Fitted Banker probabilities (n,)
395
- - P_fitted: Full place probability matrix (n, n), P[i,j] =
396
- prob horse i finishes in position j
397
- - loss: Final loss value
398
468
  - success: Whether optimization converged
399
469
  - message: Optimizer status message
400
470
  - n_eval: Number of loss function evaluations
471
+ - loss: Final loss value
472
+ - prob_fit: Dictionary of fitted probabilities (sum to 1.0)
473
+ - theta: Fitted strength parameters (n,)
474
+ - W: Win probabilities (n,)
475
+ - Qin: Qin probabilities (n, n)
476
+ - Q: Quinella probabilities (n, n)
477
+ - b: Banker probabilities (n,)
478
+ - P: Full place probability matrix (n, n), P[i,j] =
479
+ prob horse i finishes in position j
480
+ - odds_fit: Dictionary of fitted market odds (directly comparable to observed)
481
+ - WIN: Win odds (n,)
482
+ - QIN: Qin odds (n, n)
483
+ - QPL: Quinella Place odds (n, n)
484
+ - PLA: Place odds (n,)
485
+
486
+ Example:
487
+ >>> odds = {'WIN': np.array([3.5, 4.2, 5.0, 8.5, 12.0])}
488
+ >>> result = fit_harville_to_odds(odds, takeout_rate=0.175)
489
+ >>> print(result['prob_fit']['theta']) # True winning probabilities
490
+ >>> print(result['odds_fit']['WIN']) # Fitted market odds (compare to input)
401
491
  """
402
492
  n_horses = None
403
493
  W_obs = None
@@ -443,7 +533,8 @@ def fit_harville_to_odds(odds : dict[str, np.ndarray], lambdas : dict[str, float
443
533
  lambda_win=merged_lambdas['WIN'],
444
534
  lambda_qin=merged_lambdas['QIN'],
445
535
  lambda_quinella=merged_lambdas['QPL'],
446
- lambda_banker=merged_lambdas['PLA']
536
+ lambda_banker=merged_lambdas['PLA'],
537
+ takeout_rate=takeout_rate
447
538
  )
448
539
  result = ho.fit(W_obs=W_obs, Qin_obs=Qin_obs, Q_obs=Q_obs, b_obs=b_obs)
449
540
  return result
hkjc/historical.py CHANGED
@@ -20,7 +20,7 @@ incidents = ['DISQ', 'DNF', 'FE', 'ML', 'PU', 'TNP', 'TO',
20
20
  def _soupify(url: str) -> BeautifulSoup:
21
21
  """Fetch and parse a webpage and return BeautifulSoup object
22
22
  """
23
- response = requests.get(url, timeout=30)
23
+ response = requests.get(url, timeout=180)
24
24
  response.raise_for_status()
25
25
  return BeautifulSoup(response.content, 'html.parser')
26
26
 
hkjc/processing.py CHANGED
@@ -42,7 +42,15 @@ def _historical_process_single_date_venue(date: str, venue_code: str) -> List[pl
42
42
 
43
43
 
44
44
  def generate_historical_data(start_date: str, end_date: str) -> pl.DataFrame:
45
- """Generate historical race dataset from start_date to end_date"""
45
+ """Generate historical race dataset from start_date to end_date (inclusive).
46
+
47
+ Args:
48
+ start_date (str): Date in 'YYYY-MM-DD' format.
49
+ end_date (str): Date in 'YYYY-MM-DD' format.
50
+
51
+ Returns:
52
+ pl.DataFrame: DataFrame with all records.
53
+ """
46
54
  _validate_date(start_date)
47
55
  _validate_date(end_date)
48
56
  start_dt = dt.strptime(start_date, '%Y-%m-%d')
@@ -50,7 +58,7 @@ def generate_historical_data(start_date: str, end_date: str) -> pl.DataFrame:
50
58
 
51
59
  dfs = []
52
60
 
53
- for date in tqdm(pl.date_range(start_dt, end_dt, interval='1d', eager=True), leave=False, desc='Scanning for horse IDs ...'):
61
+ for date in tqdm(pl.date_range(start_dt, end_dt, interval='1d', eager=True, closed='both'), leave=False, desc='Scanning for horse IDs ...'):
54
62
  for venue_code in ['ST', 'HV']:
55
63
  dfs += _historical_process_single_date_venue(date, venue_code)
56
64
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hkjc
3
- Version: 0.3.19
3
+ Version: 0.3.21
4
4
  Summary: Library for scrapping HKJC data and perform basic analysis
5
5
  Requires-Python: >=3.11
6
6
  Requires-Dist: beautifulsoup4>=4.14.2
@@ -1,14 +1,14 @@
1
1
  hkjc/__init__.py,sha256=XSm9N6YbZ2SzyxjO9aR26ctB4Z1-VeBImuroSgncUfk,737
2
- hkjc/features.py,sha256=1mcF9Pq2LsnQ8yIv2F8Uyg1HXz7LdNcPf9RDzFN3sbA,297
3
- hkjc/harville_model.py,sha256=MZjPLS-1nbEhp1d4Syuq13DtraKnd7TlNqBmOOCwxgc,15976
4
- hkjc/historical.py,sha256=Yujb4Q2cTkvVvvZxVaSoawbwwxfzq03lprG_s_4H7Dk,7682
2
+ hkjc/features.py,sha256=LicwtKBpMzpz_dSX9bjoCLLaRUu8oeZo1AloTe7v7sI,298
3
+ hkjc/harville_model.py,sha256=WSA_1EcNOHKGraP6WVHJ3FXZPGrDrjKhJc_q70KKx80,20188
4
+ hkjc/historical.py,sha256=aONchf7CMNs2B-WVDS_GWg8g0U0ZEH-FjbfhdJwc_N0,7683
5
5
  hkjc/live.py,sha256=CfMeHRQfhKSmhQaexM99sdP0KRbIEqg2DIvNPc1gohk,10696
6
- hkjc/processing.py,sha256=uNjM5eeH9Mj8Dg9-9K7z-7xeufaXJT42F49zUHzj0h0,6968
6
+ hkjc/processing.py,sha256=hQnHxl6HYlFOeSLSOCVsemgTKcwt9_tYUQI-itpvjUg,7188
7
7
  hkjc/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
8
  hkjc/speedpro.py,sha256=Y2Z3GYGeePc4sM-ZnCHXCI1N7L-_j9nrMqS3CC5BBSo,2031
9
9
  hkjc/utils.py,sha256=4CA_FPf_U3GvzoLkqBX0qDPZgrSvKJKvbP7VWqd5FiA,6323
10
10
  hkjc/strategy/place_only.py,sha256=lHPjTSj8PzghxncNBg8FI4T4HJigekB9a3bV7l7VtPA,2079
11
11
  hkjc/strategy/qpbanker.py,sha256=MQxjwsfhllKZroKS8w8Q3bi3HMjGc1DAyBIjNZAp3yQ,4805
12
- hkjc-0.3.19.dist-info/METADATA,sha256=npklDb_gSAZHliXMNDZYOlRIuV_Klkzgw4ELdW9zzjc,480
13
- hkjc-0.3.19.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
14
- hkjc-0.3.19.dist-info/RECORD,,
12
+ hkjc-0.3.21.dist-info/METADATA,sha256=YuIC0EvFVS3Z-8cwdzczMV7qQxMYvIKtO442iUQu5Jg,480
13
+ hkjc-0.3.21.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
14
+ hkjc-0.3.21.dist-info/RECORD,,
File without changes