Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2Collection of tools for managing and analyzing linear models. 

3 

4Todo 

5----- 

6Consider what functionality we actually want here 

7 

8""" 

9 

10import numpy as np 

11from collections import namedtuple 

12 

13 

14ScatterData = namedtuple('ScatterData', ['target', 'predicted']) 

15 

16 

17def compute_correlation_matrix(A: np.ndarray) -> np.ndarray: 

18 """ 

19 Returns the correlation matrix for the rows in the fit matrix. 

20 

21 Notes 

22 ----- 

23 Naive implementation. 

24 

25 Parameters 

26 ---------- 

27 A 

28 fit matrix 

29 """ 

30 N = A.shape[0] 

31 C = np.zeros((N, N)) 

32 for i in range(N): 

33 for j in range(i+1, N): 

34 norm = np.linalg.norm(A[i, :]) * np.linalg.norm(A[j, :]) 

35 c_ij = np.dot(A[i, :], A[j, :]) / norm 

36 C[i, j] = c_ij 

37 C[j, i] = c_ij 

38 return C 

39 

40 

41def estimate_loocv(A: np.ndarray, y_targ: np.ndarray, y_pred: np.ndarray) -> float: 

42 """ 

43 Calculates the approximative LOO-CV-RMSE. 

44 

45 Parameters 

46 ---------- 

47 A 

48 Matrix in OLS problem y=Ax, should be inversible 

49 y_targ 

50 Target values for y 

51 y_pred 

52 OLS obtained prediction for y 

53 

54 Returns 

55 ------- 

56 float 

57 LOO-CV-RMSE 

58 """ 

59 if len(A[1, :]) > len(A[:, 1]): 

60 raise ValueError('Matrix is underdetermined') 

61 

62 H = A.dot(np.linalg.inv(A.T.dot(A))).dot(A.T) 

63 e = (y_targ - y_pred) / (1 - np.diag(H)) 

64 

65 return np.linalg.norm(e) / np.sqrt(len(e))