def _inverse_cdf(self): """ This module will calculate the inverse of CDF which wil be used to getting the ensembla of X and Y from the ensemble of U and V The statistics module is used to estimate teh CDF, which uses the kernel method of cdf estimation. To estimate the inverse of the CDF, interpolation method is used, first cdf is estiamted at 100 points, now interpolation function is generated to relate cdf at 100 points to the data. """ # if U and V are nore already generated if self.U is None: self.generate_uv(n) #estimate teh inverse cdf of x and y x2, x1 = st.cpdf(self.X, kernal="Epanechnikov", n=100) self._inverse_cdf_x = interp1d(x2,x1) y2, y1 = st.cpdf(self.Y, kernal="Epanechnikov", n=100) self._inverse_cdf_y = interp1d(y2,y1) X1 = self._inverse_cdf_x(self.U) Y1 = self._inverse_cdf_y(self.V) return X1, Y1
def _inverse_cdf(self): """ This module will calculate the inverse of CDF which will be used in getting the ensemble of X and Y from the ensemble of U and V The statistics module is used to estimate the CDF, which uses kernel methold of cdf estimation To estimate the inverse of CDF, interpolation method is used, first cdf is estimated at 100 points, now interpolation function is generated to relate cdf at 100 points to data """ x2, x1 = st.cpdf(self.X, kernel='Epanechnikov', n=100) self._inv_cdf_x = interp1d(x2, x1) y2, y1 = st.cpdf(self.Y, kernel='Epanechnikov', n=100) self._inv_cdf_y = interp1d(y2, y1)
def restrict(self,x,grid,domain): edges = scipy.r_[domain[0],(grid[1:]+grid[:-1])/2.,domain[-1]] #concatenate # estimating the cumulative density to make the estimation conservative cum = statistics.cpdf(x,edges[1:-1],h=self.h) rho = scipy.zeros_like(grid) rho[0]=cum[0]/(edges[1]-edges[0]) rho[1:-1]=(cum[1:]-cum[:-1])/(edges[2:-1]-edges[1:-2]) rho[-1]=(1.-cum[-1])/(edges[-1]-edges[-2]) return rho
def _inverse_cdf(self): """ This module will calculate the inverse of CDF which will be used in getting the ensemble of X and Y from the ensemble of U and V The statistics module is used to estimate the CDF, which uses kernel methold of cdf estimation To estimate the inverse of CDF, interpolation method is used, first cdf is estimated at 100 points, now interpolation function is generated to relate cdf at 100 points to data """ x2, x1 = st.cpdf(self.X, kernel = 'Epanechnikov', n = 100) self._inv_cdf_x = interp1d(x2, x1) y2, y1 = st.cpdf(self.Y, kernel = 'Epanechnikov', n = 100) self._inv_cdf_y = interp1d(y2, y1)
def restrict(self, x, grid, domain): edges = scipy.r_[domain[0], (grid[1:] + grid[:-1]) / 2., domain[-1]] #concatenate # estimating the cumulative density to make the estimation conservative cum = statistics.cpdf(x, edges[1:-1], h=self.h) rho = scipy.zeros_like(grid) rho[0] = cum[0] / (edges[1] - edges[0]) rho[1:-1] = (cum[1:] - cum[:-1]) / (edges[2:-1] - edges[1:-2]) rho[-1] = (1. - cum[-1]) / (edges[-1] - edges[-2]) return rho
def bias_correction(oc, mc, mp, nonzero = True): """ Input: oc: observed current mc: modeled current mp: modeled prediction Output: mp_adjusted: adjusted modeled prediction """ # convert the input arrays into one dimension oc = oc.flatten() mc = mc.flatten() mp = mp.flatten() # Instead of directly inverting the CDF, linear interpolation using # interp1d is used to invert the CDF. F_oc, OC = st.cpdf(oc, n=1000) if nonzero: OC[OC<0] = 0 f = interp1d(F_oc, OC, bounds_error=False) F1 = st.cpdf(mc, mp) mp_adjusted = f(F1) if nonzero: mp_adjusted[mp_adjusted>0] = mp_adjusted[mp_adjusted>0] + np.sum( mp_adjusted[mp_adjusted<0])/(np.sum(mp_adjusted>0)) mp_adjusted[mp_adjusted<0] = 0 return mp_adjusted
def th_dry_wet(rsm, fwet): """ Parameters: rsm: 2d numpy array relative soil moisture fwet: float fraction of the pixels undergoing wetting Output: th: float threshold of the soil moisture for the pixels undergoing drying/wetting """ f_x, x = st.cpdf(rsm[~np.isnan(rsm)].flatten(), h=0.05) foo = interp1d(f_x, x) if fwet<min(f_x): th = x.min() elif fwet>max(f_x): th = x.max() else: th = foo(fwet) return th
def th_dry_wet(rsm, fwet): """ Parameters: rsm: 2d numpy array relative soil moisture fwet: float fraction of the pixels undergoing wetting Output: th: float threshold of the soil moisture for the pixels undergoing drying/wetting """ f_x, x = st.cpdf(rsm[~np.isnan(rsm)].flatten(), h=0.05) foo = interp1d(f_x, x) if fwet < min(f_x): th = x.min() elif fwet > max(f_x): th = x.max() else: th = foo(fwet) return th
import scikits.statsmodels.api as sm import matplotlib.pyplot as plt import statistics # generate some data data = np.random.randn(100) # estimate ecdf ecdf = sm.tools.tools.ECDF(data) # plot the ecdf using step function plt.clf() plt.step(ecdf.x, ecdf.y) plt.xlabel('data', fontsize=20) plt.ylabel('Empirical CDF', fontsize=15) plt.savefig('/home/tomer/articles/python/tex/images/ecdf.png') # evaluate value of ecdf at some data poing (say at 0) print(ecdf(0)) # using kernels to estimate the pdf and cdf y, x = statistics.cpdf(data, kernel='Epanechnikov') # plot the ecdf using step function plt.clf() plt.plot(ecdf.x, ecdf.y, label='Ordinary') plt.plot(x, y, label='Kernel') plt.xlabel('data', fontsize=20) plt.ylabel('Empirical CDF', fontsize=15) plt.legend(loc='best') plt.savefig('/home/tomer/articles/python/tex/images/kernel.png')
import scikits.statsmodels.api as sm import matplotlib.pyplot as plt import statistics # generate some data data = np.random.randn(100) # estimate ecdf ecdf = sm.tools.tools.ECDF(data) # plot the ecdf using step function plt.clf() plt.step(ecdf.x, ecdf.y) plt.xlabel('data', fontsize=20) plt.ylabel('Empirical CDF', fontsize=15) plt.savefig('/home/tomer/articles/python/tex/images/ecdf.png') # evaluate value of ecdf at some data poing (say at 0) print(ecdf(0)) # using kernels to estimate the pdf and cdf y,x = statistics.cpdf(data, kernel = 'Epanechnikov') # plot the ecdf using step function plt.clf() plt.plot(ecdf.x, ecdf.y, label='Ordinary') plt.plot(x, y, label='Kernel') plt.xlabel('data', fontsize=20) plt.ylabel('Empirical CDF', fontsize=15) plt.legend(loc='best') plt.savefig('/home/tomer/articles/python/tex/images/kernel.png')