@@ -7,6 +7,7 @@ Common metrics required in machine learning modules.
77 - ``rmse``: Root mean squared error
88 - ``mae``: Mean absolute error
99 - ``mape``: Mean absolute percentage error
10+ - ``aic``: Akaike information criterion
1011
1112Credits
1213-------
@@ -15,11 +16,13 @@ Credits
1516 Authors:
1617 - Diptesh
1718
18- Date: Sep 10 , 2021
19+ Date: Dec 19 , 2021
1920"""
2021
2122import numpy as _np
2223
24+ from libc.math cimport log
25+
2326# =============================================================================
2427# --- User defined functions
2528# =============================================================================
@@ -181,3 +184,48 @@ cpdef mape(list y, list y_hat):
181184 op += abs (1 - (b * a ** - 1.0 ))
182185 op = op * arr_len ** - 1.0
183186 return op
187+
188+
189+ cpdef double aic(list y, list y_hat, int k = 1 , str method = " linear" ):
190+ """
191+ Compute `Akaike information criterion
192+ <https://en.wikipedia.org/wiki/Akaike_information_criterion>`_.
193+
194+ Parameters
195+ ----------
196+ y : list
197+
198+ Actual values.
199+
200+ y_hat : list
201+
202+ Predicted values.
203+
204+ method : str, optional
205+
206+ Type of regression (the default is linear).
207+
208+ Returns
209+ -------
210+ op : float
211+
212+ Akaike information criterion.
213+
214+ """
215+ cdef double op = 0.0
216+ cdef double sse = 0.0
217+ cdef double a = 0.0
218+ cdef double b = 0.0
219+ cdef int arr_len = 0
220+ cdef double small_sample = 0.0
221+ small_sample = arr_len * k ** - 1
222+ arr_len = len (y)
223+ if method == " linear" :
224+ for i in range (0 , arr_len, 1 ):
225+ a = y[i]
226+ b = y_hat[i]
227+ sse += (a - b) ** 2
228+ op = 2 * k - 2 * log(sse)
229+ if small_sample <= 40 :
230+ op += (2 * k * (k + 1 )) * (arr_len - k - 1 ) ** - 1
231+ return op
0 commit comments