1
-
2
1
from dataclasses import dataclass , field
2
+ from abc import ABC , abstractmethod
3
3
from typing import Dict , Callable
4
4
5
+
5
6
@dataclass
6
- class PositiveCondition :
7
+ class Condition ( ABC ) :
7
8
factor : float
8
9
barrier : float
9
10
11
+ @abstractmethod
10
12
def evaluate (self , value : float ) -> bool :
11
- return value > self .factor * self .barrier
13
+ pass
14
+
15
+
12
16
@dataclass
13
- class NegativeCondition :
14
- factor : float
15
- barrier : float
17
+ class PositiveCondition ( Condition ) :
18
+ def evaluate ( self , value : float ) -> bool :
19
+ return value > self . factor * self . barrier
16
20
21
+
22
+ @dataclass
23
+ class NegativeCondition (Condition ):
17
24
def evaluate (self , value : float ) -> bool :
18
25
return value < - 1 * self .factor * self .barrier
19
-
26
+
27
+
20
28
@dataclass
21
29
class BarrierConditions :
22
30
"""
23
31
A class that generates and manages barrier conditions used for different labeling techniques.
24
32
Those conditions can be used to generate labels like this. Example for n=1:
25
- y =
33
+ y =
26
34
-1 if r_{t,t+n} < -barrier,
27
35
1 if r_{t,t+n} > -barrier,
28
36
0 else
@@ -31,26 +39,27 @@ class BarrierConditions:
31
39
Attributes:
32
40
n (int): The number of barrier conditions to be generated for negative and positive barriers.
33
41
barrier (float): The threshold value for the barrier.
34
- conditions (Dict[int, Callable[[float], bool]]): A dictionary holding condition functions for various barrier levels. Keys are sorted numerically.
42
+ conditions (Dict[int, Condition): A dictionary holding condition functions for various barrier levels.
43
+ Keys are sorted numerically.
35
44
"""
36
45
n : int
37
46
barrier : float
38
- conditions : Dict [int , Callable [[ float ], bool ] ] = field (default_factory = dict )
47
+ conditions : Dict [int , Condition ] = field (default_factory = dict )
39
48
40
49
def __post_init__ (self ):
41
50
"""
42
51
Calculate the conditions after the instance has been initialized.
43
52
"""
44
53
self .generate_conditions ()
45
54
self .sort_conditions ()
46
-
55
+
47
56
def generate_conditions (self ):
48
57
"""
49
58
Generates barrier conditions based on the specified number of conditions and threshold values.
50
59
"""
51
- for i in range (1 , self .n + 1 ):
60
+ for i in range (1 , self .n + 1 ):
52
61
self .conditions [- i ] = NegativeCondition (factor = i , barrier = self .barrier )
53
- self .conditions [i ] = PositiveCondition (factor = i , barrier = self .barrier )
62
+ self .conditions [i ] = PositiveCondition (factor = i , barrier = self .barrier )
54
63
55
64
def sort_conditions (self ):
56
65
"""
@@ -67,4 +76,4 @@ def __str__(self):
67
76
condition_strings .append (f"\t { key } : \t { condition } " )
68
77
69
78
conditions_str = "\n \t " .join (condition_strings )
70
- return f"BarrierConditions(n={ self .n } , barrier={ self .barrier } ):\n \t Conditions={{\n \t { conditions_str } \n \t }}"
79
+ return f"BarrierConditions(n={ self .n } , barrier={ self .barrier } ):\n \t Conditions={{\n \t { conditions_str } \n \t }}"
0 commit comments