發家致富靠AI:使用keras預測NBA比賽賺錢,回報率136%

  投注者和博彩者沒有太多共同點——人們可以把他們的關係描述爲一場競爭、決鬥、戰爭。但在夢中,他們卻爲同樣的幻想而垂涎三尺:一個完美的預測模型,使用它能夠精確地預測出未來遊戲的結果。通過深入學習,這或許是可能的——或者至少比以前的數據科學技術更容易。

 

   基本假設是NBA市場效率低下(價格或投注線並不能反映出所有可用信息),而且可能比大多數市場效率更低,因爲鐵桿球迷傾向於只賭他們最喜歡的球隊。如果你能對市場的低效率下賭注,你就能賺錢。我們識別低效率的方法之一是通過數據分析。

   儘管許多嘗試這一挑戰的模型都是準確的,但大多數模型離盈利還差得很遠。原因很簡單:博彩公司也非常準確。即使你能達到博彩公司的準確性,你也會因爲5%的投注費而失敗。

 

   圖表是365net的預測線與實際的贏取百分比。一個成功的模型必須能夠通過完美的迴歸分析預測博彩公司的微小波動。

   我的模型是用帶有Tensorflow的Python構建的,它分析了過去11個NBA賽季,並且在很多方面與其他的深度學習模型相似(後者經常被試圖用於解決這個問題)。但是我們的模型有一個關鍵的區別——它使用了一個自定義的損失函數來剔除與博彩公司的相關性。我們正在挑選博彩公司錯誤預測獲勝百分比的遊戲。

  

去相關損失公式-這很重要!!!!!!!

源碼

 

模型結構

    我用nba_api Python庫抓取了得分記錄。數據存儲在MongoDB集合中。在過去的11個賽季中,每名球員每局共存儲42個統計數據,從罰球率到防守得分再到偷球次數不等。下注數據是從betexplorer中收集的。找到高質量的投注線比訓練模型困難得多。你可以向斯洛文尼亞盧布雅那大學的什特魯姆貝爾教授尋求幫助。              

對於每一場比賽,樣本是用賽季初每名球員最近8場比賽的平均數計算出來的。根據平均比賽時間選出前8名選手。

模型

預處理

  1. import os  
  2. import numpy as np  
  3. from tqdm import tqdm  
  4. from pymongo import MongoClient  
  5. bookies = {  
  6.     "44":"Betfair",  
  7.     "16":"bet365",  
  8.     "18":"Pinnacle",  
  9.     "5":"Unibet"  
  10. }  
  11. client = MongoClient()  
  12. db = client.nba  
  13. games = db.games  
  14.   
  15. seasons = [f"002{str(i).zfill(2)}" for i in range(8, 20)]  
  16. # load the samples into memory  
  17. x, y = [], []  
  18. def normalize_sample(nparr):  
  19.     for feature in range(nparr.shape[-1]): # iterate over the features  
  20.         f = np.nan_to_num(nparr[:, :, feature])  
  21.         nparr[:, :, feature] = (f-f.min())/(f.max()-f.min())  
  22.     return nparr  
  23.   
  24. for season in seasons:  
  25.     for filename in tqdm(os.listdir(f"samples/{season}")[120:]):  
  26.         if ".npy" in filename:  
  27.             game = list(games.find({"GAME_ID":filename.strip(".npy"), "bet365":{"$exists":"True"}}))  
  28.             if not game:  
  29.                 continue  
  30.             game = game[0]  
  31.             closing_odds = 1/float(game["bet365"].split()[1].split("v")[0])  
  32.             home_win = int(game["HOME"] == game["WINNER"])  
  33.             sample = np.load(f"samples/{season}/{filename}")  
  34.             x.append((normalize_sample(sample), closing_odds))  
  35.             y.append(home_win)  
  36. x = np.array(x)  
  37. y = np.array(y)  
  38.   
  39. import random  
  40. print(x.shape, y.shape)  
  41. diff = len(y)//2 - np.count_nonzero(y == 0)  
  42. for i in tqdm(range(diff)):  
  43.     while True:  
  44.             a = random.randint(1, len(y)-1)  
  45.             if y[a] == 1:  
  46.                 x = np.delete(x, a, 0)  
  47.                 y = np.delete(y, a, 0)  
  48.                 break  
  49. print(len(x), len(y))  

模型

  1. from keras import backend as K  
  2. from keras.models import Model  
  3. from keras.models import Sequential  
  4. from keras.layers import Input, Dense, Dropout, Conv2D, Flatten, Activation, concatenate  
  5. from keras.optimizers import Adam  
  6.   
  7. c = 0.6  
  8. def decorrelation_loss(neuron):  
  9.     def loss(y_actual, y_predicted):  
  10.         return K.mean(  
  11.                 K.square(y_actual-y_predicted) - c * K.square(y_predicted - neuron))  
  12.     return loss  
  13.   
  14. # split the two input streams  
  15. box_scores_train, odds_train = map(list, zip(*x_train))  
  16. box_scores_test, odds_test = map(list, zip(*x_test))  
  17.   
  18. # box model turns stats into a vector  
  19. box_model = Sequential()  
  20. shape = box_scores_train[0].shape  
  21. print(shape)  
  22. box_model.add(Conv2D(filters=32, kernel_size=(1, 8), input_shape=shape,  
  23.                  data_format="channels_first", activation="relu"))  
  24. box_model.add(Flatten())  
  25.   
  26. box_input = Input(shape=shape)  
  27. box_encoded = box_model(box_input)  
  28.   
  29. odds_input = Input(shape=(1,), dtype="float32") #(opening or closing weight)  
  30. merged = concatenate([odds_input, box_encoded])  
  31. output = Dense(32, activation="relu")(merged)  
  32. output = Dropout(0.5)(output)  
  33. output = Dense(8, activation="relu")(output)  
  34. output = Dropout(0.5)(output)  
  35. signal = Dense(1, activation="sigmoid")(output)  
  36.   
  37. opt = Adam(lr=0.0001)  
  38. nba_model = Model(inputs=[box_input, odds_input], outputs=signal)  
  39. print(nba_model.summary())  
  40.   
  41. nba_model.compile(optimizer=opt,  
  42.                   #loss="binary_crossentropy",  
  43.               loss=decorrelation_loss(odds_input), # Call the loss function with the selected layer  
  44.               metrics=['accuracy'])  
  45. nba_model.fit([box_scores_train, odds_train], y_train,  
  46.               batch_size=16,validation_data=([box_scores_test, odds_test], y_test), verbose=1,epochs=20)  

該模型是Conv2D和稠密層的組合,具有大量的dropout。模型獨一無二的部分是去相關性損失函數,在我的第一篇論文中提到過。儘管Keras本身並不支持具有神經元值的損失函數,但將函數包裝在函數中是一種有用的解決方法。我在GTX 1660Ti上訓練了20個世代的網絡,直到網絡收斂。

結果

 

使用一種非常原始的賭博策略,即10%的平衡*模型置信度,並且僅在模型的置信度大於0.6的遊戲上賭博,我們就產生了向上的平衡趨勢。有趣的是,這個模型只賭了大約10%的遊戲。除了整個2017-18賽季的慘敗,我們的模型表現非常好,從最初的100美元投資到現在的136美元,峯值爲292美元。

展望與未來

  這只是這個模型的開始。有了令人鼓舞的結果,我想制定一個更具活力的投注策略。   唯一有用的可能是下注代碼和模型。             

使用NoSQL是一個錯誤,我應該堅持使用SQLite,但是學習一種新技術是很好的。編寫一個自定義損失函數是一個非常寶貴的經驗,並將在未來的深入學習項目中派上用場。

Caleb Cheng

翻譯人:tensor-zhang

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章