2026. május 25., hétfő

Backtest statisztikából

import sqlite3
import random

DB_NAME = "skandinav_lotto.db"


# ----------------------------
# ADATBETÖLTÉS
# ----------------------------
def load_draws():
conn = sqlite3.connect(DB_NAME)
cur = conn.cursor()

cur.execute("""
SELECT numbers
FROM draws
ORDER BY id ASC
""")

rows = cur.fetchall()
conn.close()

return [list(map(int, r[0].split(","))) for r in rows]


# ----------------------------
# STATISZTIKA
# ----------------------------
def calc_stats(history):
last_seen = {}
freq = {}

for i, draw in enumerate(history):
for n in draw:
last_seen[n] = i
freq[n] = freq.get(n, 0) + 1

return last_seen, freq


# ----------------------------
# SÚLYOZÁS
# ----------------------------
def weights(last_seen, freq, t):
w = {}

for n in range(1, 36):
cold = t - last_seen.get(n, -1)
f = freq.get(n, 0)

weight = cold - f * 0.5

if weight < 0:
weight = 0.1

w[n] = weight

return w


# ----------------------------
# SZELVÉNY GENERÁLÁS
# ----------------------------
def generate(weights):
nums = list(weights.keys())
w = list(weights.values())

result = set()

while len(result) < 7:
result.add(random.choices(nums, weights=w, k=1)[0])

return sorted(result)


# ----------------------------
# BACKTEST
# ----------------------------
def backtest(draws):
total_hits = 0
count = 0

print("📊 BACKTEST INDUL...\n")

# csak múltból jósolunk
for i in range(10, len(draws)):
history = draws[:i]
actual = draws[i]

last_seen, freq = calc_stats(history)
w = weights(last_seen, freq, len(history))

prediction = set(generate(w))
actual_set = set(actual)

hits = len(prediction & actual_set)

total_hits += hits
count += 1

print(f"{i}. sorsolás -> találat: {hits}")

avg = total_hits / count if count else 0

print("\n----------------------")
print("📈 ÁTLAG TALÁLAT:", round(avg, 2))


# ----------------------------
# MAIN
# ----------------------------
def main():
draws = load_draws()

if len(draws) < 15:
print("Túl kevés adat backtesthez!")
return

backtest(draws)


if __name__ == "__main__":
main()

--------------

Nincsenek megjegyzések:

Megjegyzés küldése