chartbastan/backend/app/ml/prediction_calculator.py
2026-02-01 09:31:38 +01:00

147 lines
4.5 KiB
Python

"""
Prediction Calculator Module.
This module provides functions to calculate match predictions based on
energy scores from sentiment analysis.
"""
from typing import Dict, Any
def calculate_confidence_meter(home_energy: float, away_energy: float) -> float:
"""
Calculate the Confidence Meter (0-100%) based on energy difference.
The Confidence Meter represents how confident we are in the prediction
based on the difference in energy scores between the two teams.
Formula: min(100, abs(home_energy - away_energy) * 2)
Args:
home_energy: Energy score of the home team (float, any value)
away_energy: Energy score of the away team (float, any value)
Returns:
Confidence score between 0.0 and 100.0
Examples:
>>> calculate_confidence_meter(50.0, 50.0)
0.0
>>> calculate_confidence_meter(60.0, 50.0)
20.0
>>> calculate_confidence_meter(100.0, 50.0)
100.0
"""
energy_diff = abs(home_energy - away_energy)
confidence = min(100.0, energy_diff * 2.0)
return confidence
def determine_winner(home_energy: float, away_energy: float) -> str:
"""
Determine the predicted winner based on energy scores.
Args:
home_energy: Energy score of the home team
away_energy: Energy score of the away team
Returns:
'home' if home team has higher energy,
'away' if away team has higher energy,
'draw' if energies are equal
Examples:
>>> determine_winner(60.0, 40.0)
'home'
>>> determine_winner(40.0, 60.0)
'away'
>>> determine_winner(50.0, 50.0)
'draw'
"""
if home_energy > away_energy:
return 'home'
elif away_energy > home_energy:
return 'away'
else:
return 'draw'
def calculate_prediction(home_energy: float, away_energy: float) -> Dict[str, Any]:
"""
Calculate a complete match prediction based on energy scores.
This function combines confidence calculation and winner determination
to provide a comprehensive prediction result.
Args:
home_energy: Energy score of the home team
away_energy: Energy score of the away team
Returns:
Dictionary containing:
- confidence: Confidence score (0.0 - 100.0)
- predicted_winner: 'home', 'away', or 'draw'
- home_energy: Original home energy score
- away_energy: Original away energy score
Examples:
>>> calculate_prediction(65.0, 45.0)
{'confidence': 40.0, 'predicted_winner': 'home',
'home_energy': 65.0, 'away_energy': 45.0}
"""
confidence = calculate_confidence_meter(home_energy, away_energy)
predicted_winner = determine_winner(home_energy, away_energy)
return {
'confidence': confidence,
'predicted_winner': predicted_winner,
'home_energy': home_energy,
'away_energy': away_energy
}
def validate_prediction_result(result: Dict[str, Any]) -> bool:
"""
Validate that a prediction result contains all required fields and valid values.
Args:
result: Dictionary to validate
Returns:
True if valid, False otherwise
Examples:
>>> validate_prediction_result({'confidence': 75.0, 'predicted_winner': 'home',
... 'home_energy': 65.0, 'away_energy': 45.0})
True
>>> validate_prediction_result({'confidence': -10.0, 'predicted_winner': 'home',
... 'home_energy': 65.0, 'away_energy': 45.0})
False
"""
# Check required fields
required_fields = ['confidence', 'predicted_winner', 'home_energy', 'away_energy']
if not all(field in result for field in required_fields):
return False
# Validate confidence
confidence = result['confidence']
if not isinstance(confidence, (int, float)):
return False
if confidence < 0.0 or confidence > 100.0:
return False
# Validate predicted_winner
winner = result['predicted_winner']
if winner not in ['home', 'away', 'draw']:
return False
# Validate energy scores (should be non-negative)
home_energy = result['home_energy']
away_energy = result['away_energy']
if not isinstance(home_energy, (int, float)) or not isinstance(away_energy, (int, float)):
return False
if home_energy < 0.0 or away_energy < 0.0:
return False
return True