Source code for Policies.Experimentals.UCBlog10

# -*- coding: utf-8 -*-
r""" The UCB policy for bounded bandits, using :math:`\log10(t)` and not :math:`\log(t)` for UCB index.
Reference: [Lai & Robbins, 1985].
"""
from __future__ import division, print_function  # Python 2 compatibility

__author__ = "Lilian Besson"
__version__ = "0.1"

from math import sqrt, log10
import numpy as np
np.seterr(divide='ignore')  # XXX dangerous in general, controlled here!

# WARNING: this is a HUGE hack to fix a mystery bug on importing this policy
from sys import path
from os.path import dirname
path.insert(0, '/'.join(dirname(__file__).split('/')[:-1]))

try:
    from .IndexPolicy import IndexPolicy
except ImportError:
    from IndexPolicy import IndexPolicy


[docs]class UCBlog10(IndexPolicy): r""" The UCB policy for bounded bandits, using :math:`\log10(t)` and not :math:`\log(t)` for UCB index. Reference: [Lai & Robbins, 1985]. """
[docs] def computeIndex(self, arm): r""" Compute the current index, at time t and after :math:`N_k(t)` pulls of arm k: .. math:: I_k(t) = \frac{X_k(t)}{N_k(t)} + \sqrt{\frac{2 \log_{10}(t)}{N_k(t)}}. """ if self.pulls[arm] < 1: return float('+inf') else: return (self.rewards[arm] / self.pulls[arm]) + sqrt((2 * log10(self.t)) / self.pulls[arm])
[docs] def computeAllIndex(self): """ Compute the current indexes for all arms, in a vectorized manner.""" indexes = (self.rewards / self.pulls) + np.sqrt((2 * np.log10(self.t)) / self.pulls) indexes[self.pulls < 1] = float('+inf') self.index[:] = indexes