PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/compute_genes_psi_id/pymodules/python2.7/lib/python/statsmodels-0.5.0-py2.7-linux-x86_64.egg/statsmodels/sandbox/nonparametric/dgp_examples.py

https://gitlab.com/pooja043/Globus_Docker_2
Python | 213 lines | 181 code | 8 blank | 24 comment | 0 complexity | 762554a673e6cf53197b0c75bde39d1c MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. """Examples of non-linear functions for non-parametric regression
  3. Created on Sat Jan 05 20:21:22 2013
  4. Author: Josef Perktold
  5. """
  6. import numpy as np
  7. ## Functions
  8. def fg1(x):
  9. '''Fan and Gijbels example function 1
  10. '''
  11. return x + 2 * np.exp(-16 * x**2)
  12. def fg1eu(x):
  13. '''Eubank similar to Fan and Gijbels example function 1
  14. '''
  15. return x + 0.5 * np.exp(-50 * (x - 0.5)**2)
  16. def fg2(x):
  17. '''Fan and Gijbels example function 2
  18. '''
  19. return np.sin(2 * x) + 2 * np.exp(-16 * x**2)
  20. def func1(x):
  21. '''made up example with sin, square
  22. '''
  23. return np.sin(x * 5) / x + 2. * x - 1. * x**2
  24. ## Classes with Data Generating Processes
  25. doc = {'description':
  26. '''Base Class for Univariate non-linear example
  27. Does not work on it's own.
  28. needs additional at least self.func
  29. ''',
  30. 'ref': ''}
  31. class _UnivariateFunction(object):
  32. #Base Class for Univariate non-linear example.
  33. #Does not work on it's own. needs additionally at least self.func
  34. __doc__ = '''%(description)s
  35. Parameters
  36. ----------
  37. nobs : int
  38. number of observations to simulate
  39. x : None or 1d array
  40. If x is given then it is used for the exogenous variable instead of
  41. creating a random sample
  42. distr_x : None or distribution instance
  43. Only used if x is None. The rvs method is used to create a random
  44. sample of the exogenous (explanatory) variable.
  45. distr_noise : None or distribution instance
  46. The rvs method is used to create a random sample of the errors.
  47. Attributes
  48. ----------
  49. x : ndarray, 1-D
  50. exogenous or explanatory variable. x is sorted.
  51. y : ndarray, 1-D
  52. endogenous or response variable
  53. y_true : ndarray, 1-D
  54. expected values of endogenous or response variable, i.e. values of y
  55. without noise
  56. func : callable
  57. underlying function (defined by subclass)
  58. %(ref)s
  59. ''' #% doc
  60. def __init__(self, nobs=200, x=None, distr_x=None, distr_noise=None):
  61. if x is None:
  62. if distr_x is None:
  63. x = np.random.normal(loc=0, scale=self.s_x, size=nobs)
  64. else:
  65. x = distr_x.rvs(size=nobs)
  66. x.sort()
  67. self.x = x
  68. if distr_noise is None:
  69. noise = np.random.normal(loc=0, scale=self.s_noise, size=nobs)
  70. else:
  71. noise = distr_noise.rvs(size=nobs)
  72. if hasattr(self, 'het_scale'):
  73. noise *= self.het_scale(self.x)
  74. #self.func = fg1
  75. self.y_true = y_true = self.func(x)
  76. self.y = y_true + noise
  77. def plot(self, scatter=True, ax=None):
  78. '''plot the mean function and optionally the scatter of the sample
  79. Parameters
  80. ----------
  81. scatter: bool
  82. If true, then add scatterpoints of sample to plot.
  83. ax : None or matplotlib axis instance
  84. If None, then a matplotlib.pyplot figure is created, otherwise
  85. the given axis, ax, is used.
  86. Returns
  87. -------
  88. fig : matplotlib figure
  89. This is either the created figure instance or the one associated
  90. with ax if ax is given.
  91. '''
  92. if ax is None:
  93. import matplotlib.pyplot as plt
  94. fig = plt.figure()
  95. ax = fig.add_subplot(1, 1, 1)
  96. if scatter:
  97. ax.plot(self.x, self.y, 'o', alpha=0.5)
  98. xx = np.linspace(self.x.min(), self.x.max(), 100)
  99. ax.plot(xx, self.func(xx), lw=2, color='b', label='dgp mean')
  100. return ax.figure
  101. doc = {'description':
  102. '''Fan and Gijbels example function 1
  103. linear trend plus a hump
  104. ''',
  105. 'ref':
  106. '''
  107. References
  108. ----------
  109. Fan, Jianqing, and Irene Gijbels. 1992. "Variable Bandwidth and Local
  110. Linear Regression Smoothers."
  111. The Annals of Statistics 20 (4) (December): 2008-2036. doi:10.2307/2242378.
  112. '''}
  113. class UnivariateFanGijbels1(_UnivariateFunction):
  114. __doc__ = _UnivariateFunction.__doc__ % doc
  115. def __init__(self, nobs=200, x=None, distr_x=None, distr_noise=None):
  116. self.s_x = 1.
  117. self.s_noise = 0.7
  118. self.func = fg1
  119. super(self.__class__, self).__init__(nobs=nobs, x=x,
  120. distr_x=distr_x,
  121. distr_noise=distr_noise)
  122. doc['description'] =\
  123. '''Fan and Gijbels example function 2
  124. sin plus a hump
  125. '''
  126. class UnivariateFanGijbels2(_UnivariateFunction):
  127. __doc__ = _UnivariateFunction.__doc__ % doc
  128. def __init__(self, nobs=200, x=None, distr_x=None, distr_noise=None):
  129. self.s_x = 1.
  130. self.s_noise = 0.5
  131. self.func = fg2
  132. super(self.__class__, self).__init__(nobs=nobs, x=x,
  133. distr_x=distr_x,
  134. distr_noise=distr_noise)
  135. class UnivariateFanGijbels1EU(_UnivariateFunction):
  136. '''
  137. Eubank p.179f
  138. '''
  139. def __init__(self, nobs=50, x=None, distr_x=None, distr_noise=None):
  140. if distr_x is None:
  141. from scipy import stats
  142. distr_x = stats.uniform
  143. self.s_noise = 0.15
  144. self.func = fg1eu
  145. super(self.__class__, self).__init__(nobs=nobs, x=x,
  146. distr_x=distr_x,
  147. distr_noise=distr_noise)
  148. class UnivariateFunc1(_UnivariateFunction):
  149. '''
  150. made up, with sin and quadratic trend
  151. '''
  152. def __init__(self, nobs=200, x=None, distr_x=None, distr_noise=None):
  153. if x is None and distr_x is None:
  154. from scipy import stats
  155. distr_x = stats.uniform(-2, 4)
  156. else:
  157. nobs = x.shape[0]
  158. self.s_noise = 2.
  159. self.func = func1
  160. super(UnivariateFunc1, self).__init__(nobs=nobs, x=x,
  161. distr_x=distr_x,
  162. distr_noise=distr_noise)
  163. def het_scale(self, x):
  164. return np.sqrt(np.abs(3+x))