Ejemplo n.º 1
0
 def _checkOrth(self, T, TT, eps=0.0001, output=False):
     """check if the basis is orthogonal on a set of points x: TT == T*transpose(T) == c*Identity
     INPUT:  T: matrix of values of polynomials calculated at common reference points (x)
             TT = T * transpose(T)
             eps: max numeric error
     """
     TTd0 = (-1.*Numeric.identity(Numeric.shape(TT)[0])+1) * TT  # TTd0 = TT with 0s on the main diagonal
     s = Numeric.sum(Numeric.sum(Numeric.absolute(TTd0)))
     minT = MLab.min(MLab.min(T))
     maxT = MLab.max(MLab.max(T))
     minTTd0 = MLab.min(MLab.min(TTd0))
     maxTTd0 = MLab.max(MLab.max(TTd0))
     if not s < eps:
         out = "NOT ORTHOG, min(T), max(T):\t%f\t%f\n" % (minT, maxT)
         out += "  min(TTd0), max(TTd0), sum-abs-el(TTd0):\t%f\t%f\t%f" % (minTTd0, maxTTd0, s)
         if output:
             print out
             return False
         else:
             raise out
     elif output:
         out = "ORTHOGONAL, min(T), max(T):\t%f\t%f\n" % (minT, maxT)
         out += "  min(TTd0), max(TTd0), sum-abs-el(TTd0):\t%f\t%f\t%f" % (minTTd0, maxTTd0, s)
         print out
     return True
Ejemplo n.º 2
0
 def _checkOrth(self, T, TT, eps=0.0001, output=False):
     """check if the basis is orthogonal on a set of points x: TT == T*transpose(T) == c*Identity
     INPUT:  T: matrix of values of polynomials calculated at common reference points (x)
             TT = T * transpose(T)
             eps: max numeric error
     """
     TTd0 = (-1. * Numeric.identity(Numeric.shape(TT)[0]) +
             1) * TT  # TTd0 = TT with 0s on the main diagonal
     s = Numeric.sum(Numeric.sum(Numeric.absolute(TTd0)))
     minT = MLab.min(MLab.min(T))
     maxT = MLab.max(MLab.max(T))
     minTTd0 = MLab.min(MLab.min(TTd0))
     maxTTd0 = MLab.max(MLab.max(TTd0))
     if not s < eps:
         out = "NOT ORTHOG, min(T), max(T):\t%f\t%f\n" % (minT, maxT)
         out += "  min(TTd0), max(TTd0), sum-abs-el(TTd0):\t%f\t%f\t%f" % (
             minTTd0, maxTTd0, s)
         if output:
             print out
             return False
         else:
             raise out
     elif output:
         out = "ORTHOGONAL, min(T), max(T):\t%f\t%f\n" % (minT, maxT)
         out += "  min(TTd0), max(TTd0), sum-abs-el(TTd0):\t%f\t%f\t%f" % (
             minTTd0, maxTTd0, s)
         print out
     return True
Ejemplo n.º 3
0
def _create_matrix_plot(z_, r_x=0, r_y=0, colorcode=0,
                        min_area=100000, scale=1):

    import numpy.oldnumeric.mlab as ml
        
    # Scale z and find appropriate colormap.

    z = z_.copy()
    
    zmax = ml.max(ml.max(z))
    zmin = ml.min(ml.min(z))

    if (zmin < 0) and (0 < zmax) and (not colorcode):
        colormap = create_bipolar_colormap()
        zmax = ml.max(asarray([-zmin, zmax]))
        z += zmax
        z *= (len(colormap)-1)/(2*zmax)
    else:
        if colorcode == whiteblack:
            colormap = create_white_black_colormap()
        elif colorcode == blackwhite:
            colormap = create_black_white_colormap()    
        else:
            colormap = create_unipolar_colormap()

        z -= zmin
        if (zmax != zmin):
            z *= (len(colormap)-1)/(zmax-zmin)

    return _create_scaled_matrix_plot(colormap, z, r_x, r_y,
                                      min_area = min_area, scale = scale)
Ejemplo n.º 4
0
 def getAppxCoef2d_significant(self, arr2d, maxNumCoef, alpha):
     """Returns[:,j] approx. coeffcients with p-value < alpha; subsequent coef. are equal to 0.
     Reference: Ott, pp.606.
     
     The significance of the coef. estimated by comparing the variance drop of the model2 with the variance of the model1, where:
       model 1: complete model with maxNumCoef coef. different from 0
       model 2: reduced model with coef. in range (0,k) different from 0
     null hypothesis (H0): coef. k,k+1,...,maxNumCoef are equal to 0
         if H0 rejected (pval below some alpha (e.g. 0.05) -> there exist an important coef. among coef. k,k+1,...,maxNumCoef
     repeat the test for k=0,1,...maxNumCoef-1
     """
     assert len(arr2d.shape) == 2, "2d array expected"
     assert 0 < maxNumCoef <= self.k
     coefMax = self.getAppxCoef(arr2d, maxNumCoef)
     curveMax = self.getAppxCurve(coefMax)
     SSE1 = Numeric.add.reduce((arr2d - curveMax)**2, 1)
     MSE1 = SSE1 / (arr2d.shape[1] - maxNumCoef)
     #print "SSE1[0], MSE1[0]",SSE1[0], MSE1[0]
     pvals = Numeric.zeros((arr2d.shape[0], maxNumCoef), Numeric.Float)
     for k in range(
             maxNumCoef):  # test cofInd: [maxNum-1, maxNum-2, ..., minNum]
         #print "Keeping %i coeff" % (k)
         shpk = list(coefMax.shape)
         shpk[1] -= k
         coefk = Numeric.concatenate(
             (coefMax[:, :k], Numeric.zeros((shpk), Numeric.Float)), 1)
         curvek = self.getAppxCurve(coefk)
         SSE2 = Numeric.add.reduce((arr2d - curvek)**2, 1)
         MSdrop = (SSE2 - SSE1) / (maxNumCoef - k)
         F = MSdrop / MSE1
         #2007-10-11: F -> F.filled(???)
         pvals[:, k] = scipy.stats.fprob(
             (maxNumCoef - k), arr2d.shape[1] - maxNumCoef,
             F.filled(ApproxOrthPolyBasis._F_fillValue))
     pvals = Numeric.where(
         pvals > alpha,
         Numeric.resize(Numeric.arange(pvals.shape[1]), pvals.shape),
         pvals.shape[1])  # MAX where significant, idx where nonsignificant
     firstNonSignIdx = MLab.min(pvals,
                                1)  # idx of the first non-significant coef.
     coefSign = Numeric.zeros(coefMax.shape, Numeric.Float)
     for idx in range(coefSign.shape[1]):
         coefSign[:, idx] = Numeric.where(idx < firstNonSignIdx,
                                          coefMax[:, idx], 0)
     return coefSign
Ejemplo n.º 5
0
 def getAppxCoef2d_significant(self, arr2d, maxNumCoef, alpha):
     """Returns[:,j] approx. coeffcients with p-value < alpha; subsequent coef. are equal to 0.
     Reference: Ott, pp.606.
     
     The significance of the coef. estimated by comparing the variance drop of the model2 with the variance of the model1, where:
       model 1: complete model with maxNumCoef coef. different from 0
       model 2: reduced model with coef. in range (0,k) different from 0
     null hypothesis (H0): coef. k,k+1,...,maxNumCoef are equal to 0
         if H0 rejected (pval below some alpha (e.g. 0.05) -> there exist an important coef. among coef. k,k+1,...,maxNumCoef
     repeat the test for k=0,1,...maxNumCoef-1
     """
     assert len(arr2d.shape) == 2, "2d array expected"
     assert 0 < maxNumCoef <= self.k
     coefMax = self.getAppxCoef(arr2d, maxNumCoef)
     curveMax = self.getAppxCurve(coefMax)
     SSE1 = Numeric.add.reduce((arr2d-curveMax)**2,1)
     MSE1 = SSE1 / (arr2d.shape[1]-maxNumCoef)
     #print "SSE1[0], MSE1[0]",SSE1[0], MSE1[0]
     pvals = Numeric.zeros((arr2d.shape[0], maxNumCoef), Numeric.Float)
     for k in range(maxNumCoef):  # test cofInd: [maxNum-1, maxNum-2, ..., minNum]
         #print "Keeping %i coeff" % (k)
         shpk = list(coefMax.shape)
         shpk[1] -= k
         coefk = Numeric.concatenate((coefMax[:,:k], Numeric.zeros((shpk), Numeric.Float)),1)
         curvek = self.getAppxCurve(coefk)
         SSE2 = Numeric.add.reduce((arr2d-curvek)**2,1)
         MSdrop =(SSE2-SSE1) / (maxNumCoef-k)
         F = MSdrop / MSE1
         #2007-10-11: F -> F.filled(???)
         pvals[:,k] = scipy.stats.fprob((maxNumCoef-k), arr2d.shape[1]-maxNumCoef, F.filled(ApproxOrthPolyBasis._F_fillValue))
     pvals = Numeric.where(pvals > alpha, Numeric.resize(Numeric.arange(pvals.shape[1]),pvals.shape), pvals.shape[1])    # MAX where significant, idx where nonsignificant
     firstNonSignIdx = MLab.min(pvals, 1)    # idx of the first non-significant coef.
     coefSign = Numeric.zeros(coefMax.shape, Numeric.Float)
     for idx in range(coefSign.shape[1]):
         coefSign[:,idx] = Numeric.where(idx<firstNonSignIdx, coefMax[:,idx], 0)
     return coefSign