File size: 1,105 Bytes
4f55ca2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from abc import ABC, abstractmethod
from typing import Dict

import numpy as np

from ibydmt.utils import _get_cls, _register_cls


class Bet(ABC):
    def __init__(self):
        pass

    @abstractmethod
    def compute(self, *args, **kwargs):
        pass


_BETS: Dict[str, Bet] = {}


def register_bet(name):
    return _register_cls(name, dict=_BETS)


def get_bet(name):
    return _get_cls(name, dict=_BETS)


@register_bet("sign")
class Sign(Bet):
    def __init__(self, config):
        super().__init__()
        self.m = config.get("m", 0.5)
        self.prev_g = []

    def compute(self, g):
        return self.m * np.sign(g)


@register_bet("tanh")
class Tanh(Bet):
    def __init__(self, config):
        super().__init__()
        self.alpha = config.get("alpha", 0.20)
        self.prev_g = []

    def compute(self, g):
        if len(self.prev_g) < 2:
            scale = 1
        else:
            l, u = np.quantile(self.prev_g, [self.alpha / 2, 1 - self.alpha / 2])
            scale = u - l

        self.prev_g.append(g)

        return np.tanh(g / np.clip(scale, 1e-04, None))