def usage_feature(d: int) -> Iterator[Tuple[str, List[float]]]: """Computes usage difference between day d and each of the previous 6 days (features 14-19). Usage is defined as the ratio between the number of times it appears in every deck and the total of cards that appears in every deck. Simple stats: only about 1000 different cards are played in tournaments """ card_id_to_usages = defaultdict(list) # Gets all cards that have been played for two weeks. all_cards = set( Tournament.get_played_cards(date.today() - timedelta(days=d), date.today() - timedelta(days=d + 13))) for i in range(7): played_cards = Tournament.get_played_cards( date.today() - timedelta(days=d + i), date.today() - timedelta(days=d + i + 6)) total = sum(played_cards.values()) for card_name in all_cards: usage = played_cards.get(card_name, 0) / total for card in Card.objects.filter(name=card_name): card_id_to_usages[card.id].append(usage) for card_id, usages in card_id_to_usages.items(): if len(usages) > 1: yield card_id, [usages[0] - usage for usage in usages[1:]] else: yield card_id, usages
def compute_statistics() -> None: """Compute all the statistics for every relevant cards. This step needs to be done after all steps have finished. """ logger.info('Computing cards statistics') class Classifier: # TODO: implement me :'( @staticmethod def predict(_): return 0 for card in Card.objects.filter(is_relevant=True): logger.debug('Predicting price for card {}...'.format(card)) features = Features.objects.get(card=card, date=date.today()) predicted_price = Classifier.predict(features.features) logger.debug('Computing price ratio for card {}...'.format(card)) current_prices = Price.objects.filter(card=card, date=date.today()) price_ratio = predicted_price / current_prices.mean_price logger.debug('Computing playing ratio for card {}...'.format(card)) played_cards = Tournament.get_played_cards( date.today(), date.today() - timedelta(days=Statistics.PLAYING_WINDOW)) playing_ratio = played_cards[card.name.name] / sum( played_cards.values()) statistics, created = Statistics.objects.get_or_create( card=card, date=date.today(), defaults={ 'predicted_price': predicted_price, 'price_ratio': price_ratio, 'playing_ratio': playing_ratio }) if created: logger.debug('Created statistics {}'.format(statistics))