import numpy as np
# constants
M = 1.0
k = 180 # number of timeslots
x1 = [1,0,0] # prob. of No goal scored per timeslot.
x2 = [0,1,0] # prob. of Home Team scoring per timeslot.
x3 = [0,0,1] # prob. of Away Team scoring per timeslot.
# seven scores
final_scores = [[2,1],[3,3],[1,2],[1,1],[2,1],[4,0],[2,3]]
# time slots with goals
Home_Goal = [2, 3]
Away_Goal = [4]
# numpy arrays of the data
final_scores = np.array(final_scores) # team_1 is [:,0], team_2 is [:,1]
home_goal = np.array(Home_Goal)
away_goal = np.array(Away_Goal)
# fudge factor
adj_scores = final_scores * M # shape --> (# of scores, 2)
# calculate prob_1
slot_goal_probability = adj_scores / k # xG_n / k
slot_draw_probability = 1 - slot_goal_probability.sum(axis = 1) #1-(xG_1+xG_2)/k
# y for all scores
y = np.concatenate((slot_draw_probability[:,None], slot_goal_probability), axis=1)
# ssd for x2, x3, x1
home_ssd = np.sum(np.square(x2 - y), axis=1)
away_ssd = np.sum(np.square(x3 - y), axis=1)
draw_ssd = np.sum(np.square(x1 - y), axis=1)
ssd = np.zeros((y.shape[0],k))
ssd += draw_ssd[:,None] # all time slices a draw
ssd[:,home_goal] = home_ssd[:,None] # time slots with goal for home games
ssd[:,away_goal] = away_ssd[:,None] # time slots with goal for away games
Сумма вероятностей (в вашем примере prob_1) для каждой оценки:
>>> y.sum(axis=1)
array([1., 1., 1., 1., 1., 1., 1.])
ssd
имеет форму (количество оценок, 180) - она содержит вероятность временного интервала длявсе баллы.
>>> ssd.sum(axis=1)
array([5.92222222, 6. , 5.93333333, 5.93333333, 5.92222222,
5.95555556, 5.96666667])
>>> for thing in ssd.sum(axis=1):
print(thing)
5.922222222222222
6.000000000000001
5.933333333333332
5.933333333333337
5.922222222222222
5.955555555555557
5.966666666666663
>>>
Тест y
с вашей функцией:
>>> y
array([[0.98333333, 0.01111111, 0.00555556],
[0.96666667, 0.01666667, 0.01666667],
[0.98333333, 0.00555556, 0.01111111],
[0.98888889, 0.00555556, 0.00555556],
[0.98333333, 0.01111111, 0.00555556],
[0.97777778, 0.02222222, 0. ],
[0.97222222, 0.01111111, 0.01666667]])
>>> for prob in y:
print(sum(sum_squared_diff(prob, x1, x2, x3)))
5.922222222222252
6.000000000000045
5.933333333333363
5.933333333333391
5.922222222222252
5.955555555555599
5.966666666666613
>>>
Некоторые, надеюсь, незначительные различия.Я приведу их к ошибкам с плавающей запятой или округлению в диапазоне 1e-14.
Может быть, кто-то увидит это и настроит это немного с большей оптимизацией в своем собственном ответе.Как только я это разработал, я не стал искать дальнейших улучшений.
Основы Numpy:
Индексирование
Вещание