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'''Additional functions 

2 

3prediction standard errors and confidence intervals 

4 

5 

6A: josef pktd 

7''' 

8 

9import numpy as np 

10from scipy import stats 

11 

12def atleast_2dcol(x): 

13 ''' convert array_like to 2d from 1d or 0d 

14 

15 not tested because not used 

16 ''' 

17 x = np.asarray(x) 

18 if (x.ndim == 1): 

19 x = x[:, None] 

20 elif (x.ndim == 0): 

21 x = np.atleast_2d(x) 

22 elif (x.ndim > 0): 

23 raise ValueError('too many dimensions') 

24 return x 

25 

26 

27def wls_prediction_std(res, exog=None, weights=None, alpha=0.05): 

28 '''calculate standard deviation and confidence interval for prediction 

29 

30 applies to WLS and OLS, not to general GLS, 

31 that is independently but not identically distributed observations 

32 

33 Parameters 

34 ---------- 

35 res : regression result instance 

36 results of WLS or OLS regression required attributes see notes 

37 exog : array_like (optional) 

38 exogenous variables for points to predict 

39 weights : scalar or array_like (optional) 

40 weights as defined for WLS (inverse of variance of observation) 

41 alpha : float (default: alpha = 0.05) 

42 confidence level for two-sided hypothesis 

43 

44 Returns 

45 ------- 

46 predstd : array_like, 1d 

47 standard error of prediction 

48 same length as rows of exog 

49 interval_l, interval_u : array_like 

50 lower und upper confidence bounds 

51 

52 Notes 

53 ----- 

54 The result instance needs to have at least the following 

55 res.model.predict() : predicted values or 

56 res.fittedvalues : values used in estimation 

57 res.cov_params() : covariance matrix of parameter estimates 

58 

59 If exog is 1d, then it is interpreted as one observation, 

60 i.e. a row vector. 

61 

62 testing status: not compared with other packages 

63 

64 References 

65 ---------- 

66 

67 Greene p.111 for OLS, extended to WLS by analogy 

68 

69 ''' 

70 # work around current bug: 

71 # fit does not attach results to model, predict broken 

72 #res.model.results 

73 

74 covb = res.cov_params() 

75 if exog is None: 

76 exog = res.model.exog 

77 predicted = res.fittedvalues 

78 if weights is None: 

79 weights = res.model.weights 

80 else: 

81 exog = np.atleast_2d(exog) 

82 if covb.shape[1] != exog.shape[1]: 

83 raise ValueError('wrong shape of exog') 

84 predicted = res.model.predict(res.params, exog) 

85 if weights is None: 

86 weights = 1. 

87 else: 

88 weights = np.asarray(weights) 

89 if weights.size > 1 and len(weights) != exog.shape[0]: 

90 raise ValueError('weights and exog do not have matching shape') 

91 

92 

93 # full covariance: 

94 #predvar = res3.mse_resid + np.diag(np.dot(X2,np.dot(covb,X2.T))) 

95 # predication variance only 

96 predvar = res.mse_resid/weights + (exog * np.dot(covb, exog.T).T).sum(1) 

97 predstd = np.sqrt(predvar) 

98 tppf = stats.t.isf(alpha/2., res.df_resid) 

99 interval_u = predicted + tppf * predstd 

100 interval_l = predicted - tppf * predstd 

101 return predstd, interval_l, interval_u