def pv(imList, p, median, j): ''' calculate -2log(R_ell,j) and return P-value ''' imList = ee.List(imList) p = ee.Number(p) j = ee.Number(j) f = p one = ee.Number(1.0) # 1 - (1. + 1./(j*(j-1)))/(6.*p*n) rhoj = one.subtract( one.add(one.divide(j.multiply(j.subtract(one)))).divide(6 * 4.9)) # -(f/4.)*(1.-1./rhoj)**2' omega2j = one.subtract(one.divide(rhoj)).pow(2.0).multiply(f.divide(-4.0)) Z = ee.Image(ee.Image(log_det_sum(imList,j.subtract(1)))).multiply(j.subtract(1)) \ .add(log_det(imList,j)) \ .add(p.multiply(j).multiply(ee.Number(j).log())) \ .subtract(p.multiply(j.subtract(1)).multiply(j.subtract(1).log())) \ .subtract(ee.Image(log_det_sum(imList,j)).multiply(j)) \ .multiply(rhoj) \ .multiply(-2*4.9) # (1.-omega2j)*stats.chi2.cdf(Z,[f])+omega2j*stats.chi2.cdf(Z,[f+4]) P = ee.Image( chi2cdf(Z, f).multiply(one.subtract(omega2j)).add( chi2cdf(Z, f.add(4)).multiply(omega2j))) # 3x3 median filter return ee.Algorithms.If(median, P.focal_median(), P)
def ells_iter(current, prev): ell = ee.Number(current) prev = ee.Dictionary(prev) pv_arr = ee.List(prev.get('pv_arr')) k = ee.Number(prev.get('k')) enl = ee.Number(prev.get('enl')) median = prev.get('median') imList = ee.List(prev.get('imList')) # number of bands (degrees of freedom) p2 = ee.Image(imList.get(0)).bandNames().length() imList_ell = imList.slice(ell.subtract(1)) js = ee.List.sequence(2, k.subtract(ell).add(1)) first = ee.Dictionary({ 'median': median, 'imList': imList_ell, 'enl': enl, 'pvs': ee.List([]), 'Z': ee.Image.constant(0.0) }) result = ee.Dictionary(js.iterate(js_iter, first)) # list of P-values for R_ell,j, j = ell+1 ... k pvs = ee.List(result.get('pvs')) # omnibus test statistic -2lnQ_ell = sum_j(-2lnR_j) Z = ee.Image(result.get('Z')) # degrees of freedom f = k.subtract(ell).multiply(p2) p = p2.sqrt() one = ee.Number(1) # rho, w2 values rho = ee.Number(ee.Algorithms.If( p2.eq(2), one, one.subtract( p2.multiply(2).subtract(one) \ .multiply(k.subtract(one.divide(k))) \ .divide(k.subtract(one).multiply(p).multiply(6).multiply(enl)) ))) w2 = ee.Number(ee.Algorithms.If( p2.eq(2), 0, p2.multiply(p2.subtract(one)) \ .multiply(k.subtract(one.divide(k.pow(2)))) \ .divide(rho.pow(2).multiply(24).multiply(enl.pow(2))) \ .subtract(p2.multiply(k.subtract(one)).multiply(one.subtract(one.divide(rho)).pow(2).divide(4))) )) Z = Z.multiply(rho) PvQ = ee.Image.constant(1.0).subtract( chi2cdf(Z, f).multiply(ee.Number(1).subtract(w2))).subtract( chi2cdf(Z, f.add(4)).multiply(w2)) PvQ = ee.Algorithms.If(median, PvQ.focal_median(), PvQ) # put at end of current sequence pvs = pvs.add(PvQ) return ee.Dictionary({ 'k': k, 'median': median, 'enl': enl, 'imList': imList, 'pv_arr': pv_arr.add(pvs) })
def pv(imList, p, median, j): ''' calculate -2log(R_ell,j) and return P-value ''' imList = ee.List(imList).slice(0, j) p = ee.Number(p) Z = ee.Image(ee.Image(log_det_sum(imList,j.subtract(1)))).multiply(j.subtract(1)) \ .add(log_det(imList,j)) \ .add(p.multiply(j).multiply(ee.Number(j).log())) \ .subtract(p.multiply(j.subtract(1)).multiply(j.subtract(1).log())) \ .subtract(ee.Image(log_det_sum(imList,j)).multiply(j)) \ .multiply(-2*4.9) P = ee.Image(ee.Algorithms.If(p.eq(2), chi2cdf(Z, 2), chi2cdf(Z, 1))) return ee.Algorithms.If(median, P.focal_median(1.5, 'square', 'pixels', 1), P)
def pv(imList, median, j, enl): ''' calculate -2log(R_ell,j) and return P-value''' imList = ee.List(imList) p2 = ee.Image(imList.get(0)).bandNames().length() p = ee.Number(ee.Algorithms.If(p2.eq(2).Or(p2.eq(3)), p2, p2.sqrt())) j = ee.Number(j) f = p2 one = ee.Number(1.0) rhoj = ee.Number(ee.Algorithms.If( p2.eq(2), one, # 1 - (2*p2-1.)*(1.+1./(j*(j-1.))/6*p*n one.subtract( p2.multiply(2).subtract(one) \ .multiply(one.add(one.divide(j.multiply(j.subtract(one))))) \ .divide(p.multiply(6).multiply(enl)) ) )) omega2j = ee.Number(ee.Algorithms.If( p2.eq(2), 0, # (f/4)*(1-1./rhoj)**2 + (1./(24.*n**2))*p2(p2-1.)*(1.+(2.*j-1.)/(j**2*(j-1.)**2))/rh0j**2 f.multiply(one.subtract(one.divide(rhoj))).divide(4). \ add( one.divide(enl.pow(2).multiply(24)) \ .multiply(p2.multiply(p2.subtract(one))) \ .multiply(one.add(j.multiply(2)).subtract(one)) \ .divide(j.pow(2).multiply(j.subtract(one)).pow(2)) \ .divide(rhoj.pow(2)) ) )) # Zj = -2*lnRj Zj = ee.Image(log_det_sum(imList,j.subtract(1))) \ .multiply(j.subtract(1)) \ .add(log_det(imList,j)) \ .add(p.multiply(j).multiply(ee.Number(j).log())) \ .subtract(p.multiply(j.subtract(1)).multiply(j.subtract(1).log())) \ .subtract(ee.Image(log_det_sum(imList,j)).multiply(j)) \ .multiply(-2).multiply(enl) # (1.-omega2j)*stats.chi2.cdf(rhoj*Zj,[f])+omega2j*stats.chi2.cdf(rhoj*Zj,[f+4]) P = chi2cdf(Zj.multiply(rhoj),f).multiply(one.subtract(omega2j)) \ .add(chi2cdf(Zj.multiply(rhoj),f.add(4)).multiply(omega2j)) PV = ee.Image.constant(1.0).subtract(P) # 3x3 median filter return (ee.Algorithms.If(median, PV.focal_median(), PV), Zj)
def ells_iter(current,prev): ell = ee.Number(current) prev = ee.Dictionary(prev) pv_arr = ee.List(prev.get('pv_arr')) k = ee.Number(prev.get('k')) median = prev.get('median') p = prev.get('p') imList = ee.List(prev.get('imList')) imList_ell = imList.slice(ell.subtract(1)) js = ee.List.sequence(2,k.subtract(ell).add(1)) first = ee.Dictionary({'median':median,'p':p,'imList':imList_ell,'pvs':ee.List([]),'Z':ee.Image.constant(0.0)}) result = ee.Dictionary(js.iterate(js_iter,first)) # list of P-values for R_ell,j, j = ell+1 ... k pvs = ee.List(result.get('pvs')) # omnibus test statistic -2lnQ_ell = sum_j(-2lnR_j) Z = ee.Image(result.get('Z')) f = k.subtract(ell).multiply(ee.Number(p)) PvQ = ee.Image.constant(1.0).subtract(chi2cdf(Z,f)) PvQ = ee.Algorithms.If(median, PvQ.focal_median(),PvQ) pvs = pvs.add(PvQ) return ee.Dictionary({'k':k,'p':p,'median':median,'imList':imList,'pv_arr':pv_arr.add(pvs)})
from scipy.stats import chi2 import numpy as np ee.Initialize() x = 2 df = 5 print 'scipy: ', chi2.cdf(x, df) point = ee.Geometry.Point([50, 6]) img = ee.Image.constant(x) dfi = ee.Image.constant(df) #result = chi2cdf_old(img,df) result1 = chi2cdf(img, df) print 'chi2cdf: ', result1.reduceRegion(ee.Reducer.first(), geometry=point, scale=4000).getInfo() arr = np.mat(np.random.randn(100, 4)) cov = ee.Array((arr.T * arr).tolist()) print np.array(cov.getInfo()) L = ee.Array(cov.matrixCholeskyDecomposition().get('L')) print np.array(L.getInfo()) S = L.matrixMultiply(L.matrixTranspose()) print np.array(S.getInfo())