@@ -103,4 +103,99 @@ def jacobian(self, x: np.ndarray) -> np.ndarray:
103103 y [~ ind1 & ~ ind2 , 0 ] = 3 * np .exp (3 * x [~ ind1 & ~ ind2 , 0 ])
104104 return y
105105
106+ class ConditionalInteraction4Regions (Base ):
107+ def __init__ (self ):
108+ """
109+ $f(x_1, x_2, x_3) =
110+ \b egin{cases}
111+ -x_1^2 + e^{x_3}, & \t ext{if } x_2 < 0 \t ext{ and } x_3 < 0 \\
112+ x_1^2 + e^{x_3}, & \t ext{if } x_2 < 0 \t ext{ and } x_3 \geq 0 \\
113+ -x_1^4 + e^{x_3}, & \t ext{if } x_2 \geq 0 \t ext{ and } x_3 < 0 \\
114+ x_1^4 + e^{x_3}, & \t ext{if } x_2 \geq 0 \t ext{ and } x_3 \geq 0
115+ \end{cases}
116+ """
117+ super ().__init__ (name = self .__class__ .__name__ )
118+
119+ def predict (self , x : np .ndarray ) -> np .ndarray :
120+ """Predict.
121+
122+ Args:
123+ x : Input data, shape (N, 4)
124+
125+ Returns:
126+ Output of the model, shape (N,)
127+ """
128+ y = np .exp (x [:, 3 ])
129+
130+ mask1 = (x [:, 1 ] < 0 ) & (x [:, 2 ] < 0 )
131+ mask2 = (x [:, 1 ] < 0 ) & (x [:, 2 ] >= 0 )
132+ mask3 = (x [:, 1 ] >= 0 ) & (x [:, 2 ] < 0 )
133+ mask4 = (x [:, 1 ] >= 0 ) & (x [:, 2 ] >= 0 )
134+
135+ y [mask1 ] += - x [mask1 , 0 ] ** 2
136+ y [mask2 ] += x [mask2 , 0 ] ** 2
137+ y [mask3 ] += - x [mask3 , 0 ] ** 4
138+ y [mask4 ] += x [mask4 , 0 ] ** 4
139+
140+ return y
141+
142+ def jacobian (self , x : np .ndarray ) -> np .ndarray :
143+ """Calculate the Jacobian of the model.
144+
145+ Args:
146+ x : Input data, shape (N, 4)
147+
148+ Returns:
149+ Jacobian of the model, shape (N, 4)
150+ """
151+ y = np .zeros (x .shape )
152+
153+ y [:, 3 ] = np .exp (x [:, 3 ])
154+
155+ mask1 = (x [:, 1 ] < 0 ) & (x [:, 2 ] < 0 )
156+ mask2 = (x [:, 1 ] < 0 ) & (x [:, 2 ] >= 0 )
157+ mask3 = (x [:, 1 ] >= 0 ) & (x [:, 2 ] < 0 )
158+ mask4 = (x [:, 1 ] >= 0 ) & (x [:, 2 ] >= 0 )
159+
160+ y [mask1 , 0 ] = - 2 * x [mask1 , 0 ]
161+ y [mask2 , 0 ] = 2 * x [mask2 , 0 ]
162+ y [mask3 , 0 ] = - 4 * x [mask3 , 0 ] ** 3
163+ y [mask4 , 0 ] = 4 * x [mask4 , 0 ] ** 3
164+
165+ return y
106166
167+ class GeneralInteraction (Base ):
168+ def __init__ (self ):
169+ """Define a simple model.
170+
171+ $f(x_1, x_2, x_3) = x_1 x_2^2 + e^{x_3}$
172+
173+ """
174+ super ().__init__ (name = self .__class__ .__name__ )
175+
176+ def predict (self , x : np .ndarray ) -> np .ndarray :
177+ """Predict.
178+
179+ Args:
180+ x : Input data, shape (N, 3)
181+
182+ Returns:
183+ Output of the model, shape (N,)
184+ """
185+ y = x [:, 0 ] * x [:, 1 ] ** 2 + np .exp (x [:, 2 ])
186+ return y
187+
188+ def jacobian (self , x : np .ndarray ) -> np .ndarray :
189+ """Calculate the Jacobian of the model.
190+
191+ Args:
192+ x : Input data, shape (N, 3)
193+
194+ Returns:
195+ Jacobian of the model, shape (N, 3)
196+ """
197+ y = np .zeros_like (x )
198+ y [:, 0 ] = x [:, 1 ] ** 2
199+ y [:, 1 ] = 2 * x [:, 0 ] * x [:, 1 ]
200+ y [:, 2 ] = np .exp (x [:, 2 ])
201+ return y
0 commit comments