def pv(self, mkt_dict_, engine_, unit_=None): """calculate option PV with market data and engine""" _rate, _spot, _vol, _div, _method, _param, _sign, _strike, _t = self._prepare_risk_data( mkt_dict_, engine_) _unit = unit_ or self.unit if _method == EngineMethod.BS.value: _d1 = (log(_spot / _strike) + (_rate - _div + _vol**2 / 2) * _t) / _vol / sqrt(_t) _d2 = _d1 - _vol * sqrt(_t) return _sign * ( _spot * exp(-_div * _t) * norm.cdf(_sign * _d1) - _strike * exp(-_rate * _t) * norm.cdf(_sign * _d2)) * _unit elif _method == EngineMethod.MC.value: from utils.monte_carlo import MonteCarlo _iteration = self._check_iter( _param[EngineParam.MCIteration.value]) _spot = MonteCarlo.stock_price(_iteration, isp=_spot, rate=_rate, div=_div, vol=_vol, t=_t) _price = [max(_sign * (_s - _strike), 0) for _s in _spot] return average(_price) * exp(-_rate * _t) * _unit
def gamma(self, mkt_dict_, engine_, unit_=None): """calculate option GAMMA with market data and engine""" _rate, _spot, _vol, _div, _method, _param, _sign, _strike, _t = self._prepare_risk_data( mkt_dict_, engine_) _unit = unit_ or self.unit if _method == EngineMethod.BS.value: _d1 = (log(_spot / _strike) + (_rate + _vol**2 / 2) * _t) / _vol / sqrt(_t) return exp(-_d1**2 / 2) / sqrt( 2 * pi) / _spot / _vol / sqrt(_t) * exp(-_div * _t) * _unit elif _method == EngineMethod.MC.value: from utils.monte_carlo import MonteCarlo _iteration = self._check_iter( _param[EngineParam.MCIteration.value]) _spot = MonteCarlo.stock_price(_iteration, isp=_spot, rate=_rate, div=_div, vol=_vol, t=_t) _step = 0.01 _gamma = [ ((max(_sign * (_s + 2 * _step - _strike), 0) - max(_sign * (_s - _strike), 0)) - (max(_sign * (_s - _strike), 0) - max(_sign * (_s - 2 * _step - _strike), 0))) / (4 * _step**2) for _s in _spot ] return average(_gamma) * exp(-_rate * _t) * _unit
def asym_sigmoidal(self, x, asym=1.0, mod_asym=1.0, xmid=None, lscale=1.0, rscale=1.0): if xmid is None: xmid = ma.median(x) return np.where(x <= xmid, (asym * mod_asym) / (1 + ma.exp((xmid - x) / lscale)), asym / (1 + ma.exp((xmid - x) / rscale)))
def test_log_returns_to_prices(self): prices_values = array([1, exp(1), exp(2), exp(-1), exp(2)]) prices_dates = pd.date_range('2015-01-01', periods=5) expected = PricesSeries(data=prices_values, index=prices_dates) returns_tms = LogReturnsSeries(data=[1, 1, -3, 3], index=expected.index[1::]) actual = returns_tms.to_prices() assert_series_equal(expected, actual)
def asym_sigmoidal(self, x, asym=1.0, mod_asym=1.0, xmid=None, lscale=1.0, rscale=1.0): if xmid is None: xmid = ma.median(x) return np.where(x <= xmid, (asym * mod_asym) / (1 + ma.exp( (xmid - x) / lscale)), asym / (1 + ma.exp((xmid - x) / rscale)))
def _pfromz_MA(z, lapse_rate, P_bott, T_bott, z_bott): """Pressure given altitude in a constant lapse rate layer. The dry gas constant is used in calculations requiring the gas constant. See the docstring for press2alt for references. Input Arguments: * z: Geopotential altitude [m]. * lapse_rate: -dT/dz [K/m] over the layer. * P_bott: Pressure [hPa] at the base of the layer. * T_bott: Temperature [K] at the base of the layer. * z_bott: Geopotential altitude [m] of the base of the layer. Output: * Pressure [hPa] for each element given in the input arguments. All input arguments can be either a scalar or an MA array. All arguments that are MA arrays, however, are of the same size and shape. If every input argument is a scalar, the output is a scalar. If any of the input arguments is an MA array, the output is an MA array of the same size and shape. """ #jfp was import Numeric as N import numpy as N #jfp was import MA import numpy.ma as MA from atmconst import AtmConst const = AtmConst() if MA.size(lapse_rate) == 1: #jfp was if MA.array(lapse_rate)[0] == 0.0: if MA.array(lapse_rate) == 0.0: return P_bott * \ MA.exp( -const.g / (const.R_d*T_bott) * (z-z_bott) ) else: exponent = const.g / (const.R_d * lapse_rate) return P_bott * \ ( (1.0 - (lapse_rate * (z-z_bott) / T_bott))**exponent ) else: exponent = const.g / (const.R_d * lapse_rate) P = P_bott * \ ( (1.0 - (lapse_rate * (z-z_bott) / T_bott))**exponent ) P_at_0 = P_bott * \ MA.exp( -const.g / (const.R_d*T_bott) * (z-z_bott) ) zero_lapse_mask = MA.filled(MA.where(lapse_rate == 0., 1, 0), 0) zero_lapse_mask_indices_flat = N.nonzero(N.ravel(zero_lapse_mask)) P_flat = MA.ravel(P) MA.put( P_flat, zero_lapse_mask_indices_flat \ , MA.take(MA.ravel(P_at_0), zero_lapse_mask_indices_flat) ) return MA.reshape(P_flat, P.shape)
def test_print_solved(self): potential = lambda x: 2 * (exp(-2 * x) - 2 * exp(-x) + 1 ) # 0.5 * x ** 2 + 0.5 * x ** 4 matrix_size = 501 left_boundary = -10 right_boundary = 10 solver = Schrodinger_equation_solver(potential, matrix_size, left_boundary, right_boundary) solver.solve() solver.print_energies(1, 10) solver.print_wave_functions((1, 2, 3, 4, 5)) solver.print_probability_density_function((1, 2, 3, 4, 5))
def test_testUfuncs1(self): # Test various functions such as sin, cos. (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d assert_(eq(np.cos(x), cos(xm))) assert_(eq(np.cosh(x), cosh(xm))) assert_(eq(np.sin(x), sin(xm))) assert_(eq(np.sinh(x), sinh(xm))) assert_(eq(np.tan(x), tan(xm))) assert_(eq(np.tanh(x), tanh(xm))) with np.errstate(divide='ignore', invalid='ignore'): assert_(eq(np.sqrt(abs(x)), sqrt(xm))) assert_(eq(np.log(abs(x)), log(xm))) assert_(eq(np.log10(abs(x)), log10(xm))) assert_(eq(np.exp(x), exp(xm))) assert_(eq(np.arcsin(z), arcsin(zm))) assert_(eq(np.arccos(z), arccos(zm))) assert_(eq(np.arctan(z), arctan(zm))) assert_(eq(np.arctan2(x, y), arctan2(xm, ym))) assert_(eq(np.absolute(x), absolute(xm))) assert_(eq(np.equal(x, y), equal(xm, ym))) assert_(eq(np.not_equal(x, y), not_equal(xm, ym))) assert_(eq(np.less(x, y), less(xm, ym))) assert_(eq(np.greater(x, y), greater(xm, ym))) assert_(eq(np.less_equal(x, y), less_equal(xm, ym))) assert_(eq(np.greater_equal(x, y), greater_equal(xm, ym))) assert_(eq(np.conjugate(x), conjugate(xm))) assert_(eq(np.concatenate((x, y)), concatenate((xm, ym)))) assert_(eq(np.concatenate((x, y)), concatenate((x, y)))) assert_(eq(np.concatenate((x, y)), concatenate((xm, y)))) assert_(eq(np.concatenate((x, y, x)), concatenate((x, ym, x))))
def calculate_n_leaching(avail_n, n_added, dr_list, fc, calib_n_leach): """ Estimate the amount of N leached by the draining water during this time step. Args: avail_n: Grid of available N at end of previous step (kg/ha). n_added: N added during this step (kg/ha). dr_list: List of drainage grids from drainage.estimate_drainage(). fc: Soil field capacity grid. calib_n_leach: Calibrating parameter determining the leaching rate. Returns: List of grids: [leached_n, new_avail_n]. """ import numpy.ma as ma # Extract individual parameters from drainage_list snow_pk, wat_lev, surf_ro, lat_dr, vert_dr, tot_dr = dr_list # Add today's N to the existing amount in the soil intermed_n = avail_n + n_added intermed_n[intermed_n<0]=0 # Calculate the fraction of the available N that leaches exponent = -1*calib_n_leach*(lat_dr+vert_dr)/fc leach_frac = 1 - ma.exp(exponent) # Calculate the amount of N that leaches leached_n = leach_frac*intermed_n # Calculate amount of N left new_avail_n = intermed_n - leached_n return [leached_n, new_avail_n]
def geometric_mean(array, axis=0): '''return the geometric mean of an array removing all zero-values but retaining total length ''' non_zero = ma.masked_values(array, 0) log_a = ma.log(non_zero) return ma.exp(log_a.mean(axis=axis))
def stats(op, infiles, band, log, area_weighted): whats, years = get_domain(infiles) df = pd.DataFrame(columns=whats, index=sorted(years)) for arg in infiles: with rasterio.open(arg) as src: data = src.read(band, masked=True) if log: data = ma.exp(data) if area_weighted: data *= rcs(data.shape[0], src.res, *src.bounds) data.mask = np.logical_or(data.mask, ma.where(np.isnan(data), True, False)) if op == 'sum': op = 'total' res = eval("%s(data)" % op) if re.search(r'-hpd-(\d){4}.tif', arg): print('%s: %8.4f %8.4f' % (os.path.basename(arg), res, np.log(res + 1) / 10.02083)) else: print('%s: %8.4f' % (os.path.basename(arg), res)) scenario, what, year = os.path.splitext(arg)[0].split('-') df.ix[int(year), what] = res print(df) df.plot.bar() plt.savefig('lu-comp.png') plt.show()
def find_an_approximation(self, function_table: dict) -> Function: try: SLNX = sum(log(x) for x in function_table.keys()) SLNXX = sum(log(x) * log(x) for x in function_table.keys()) SLNY = sum(log(y) for y in function_table.values()) SLNXY = sum(log(x) * log(y) for x, y in function_table.items()) n = len(function_table) except ValueError: return None try: b, a = self.solve_matrix22([[n, SLNX], [SLNX, SLNXX]], [SLNY, SLNXY]) if a is None: return None a = exp(a) fun = lambda x: a * (x**b) s = sum( (fun(x) - function_table[x])**2 for x in function_table.keys()) root_mean_square_deviation = sqrt(s / n) f = Function(fun, f'Ñ„ = {round(a, 3)}*x^({round(b, 3)})', s, root_mean_square_deviation) self.print_approximation_table(function_table, f, self.function_type) return f except TypeError: return None
def _apply_function(func, arg): # type: (QuilParser.FunctionContext, Any) -> Any if isinstance(arg, Expression): if func.SIN(): return parameters.quil_sin(arg) elif func.COS(): return parameters.quil_cos(arg) elif func.SQRT(): return parameters.quil_sqrt(arg) elif func.EXP(): return parameters.quil_exp(arg) elif func.CIS(): return parameters.quil_cis(arg) else: raise RuntimeError("Unexpected function to apply: " + func.getText()) else: if func.SIN(): return sin(arg) elif func.COS(): return cos(arg) elif func.SQRT(): return sqrt(arg) elif func.EXP(): return exp(arg) elif func.CIS(): return cos(arg) + complex(0, 1) * sin(arg) else: raise RuntimeError("Unexpected function to apply: " + func.getText())
def saturation_vapor_pressure(temperature): r'''Calculate the saturation water vapor (partial) pressure Parameters ---------- temperature : array_like The temperature in degrees Celsius. Returns ------- array_like The saturation water vapor (partial) presure in mb. See Also -------- vapor_pressure, dewpoint Notes ----- Instead of temperature, dewpoint may be used in order to calculate the actual (ambient) water vapor (partial) pressure. The formula used is that from Bolton 1980 [2] for T in degrees Celsius: .. math:: 6.112 e^\frac{17.67T}{T + 243.5} References ---------- .. [2] Bolton, D., 1980: The Computation of Equivalent Potential Temperature. Mon. Wea. Rev., 108, 1046-1053. ''' return sat_pressure_0c * exp(17.67 * temperature / (temperature + 243.5))
def zonal_avg(data,Log=False): """ Compute the zonal average of field on POP gx3v5 grid. Shape of input data is expected to be either [nfoo,nlat,nlon] or [nlat,nlon]. Log=True computes the geometric average. Output: arrays zavg and lat """ print 'computing zonal average' # get lat and lon for new regular grid # fpin = Nio.open_file('/home/ivan/Python/data/lat_t.nc','r') fpin = Nio.open_file('/home/emunoz/Python/mapping/model_grid/lat_t.nc','r') lat_t = fpin.variables['lat_t'][:] lat_t_edges = fpin.variables['lat_t_edges'][:] fpin.close() # fpin = Nio.open_file('/home/ivan/Python/data/gx3v5.nc','r') fpin = Nio.open_file('/home/emunoz/Python/mapping/model_grid/gx3v5.nc','r') lon_t = N.sort(fpin.variables['TLONG'][0,:]) ulon = N.sort(fpin.variables['ULONG'][0,:]) lon_t_edges = N.concatenate((ulon,ulon[0,N.newaxis]+360.),0) # get gx3v5 lat and lon tlon = fpin.variables['TLONG'][:] tlat = fpin.variables['TLAT'][:] fpin.close() # compute area of cells in new regular grid area = grid_area(lon_t_edges,lat_t_edges) nlat = lat_t.shape[0] nlon = lon_t.shape[0] if data.ndim == 3: new_data = MA.zeros((data.shape[0],nlat,nlon),dtype=float) elif data.ndim == 2: new_data = MA.zeros((nlat,nlon),dtype=float) else: print 'Check field dimensions' sys.exit() # geometric mean? if Log: work = MA.log(data) else: work = data # remap data to new regular grid for i in range(nlat): #print 'lat = %.2f'%(lat_t[i]) for j in range(nlon): new_data[:,i,j] = extract_loc(lon_t[j],lat_t[i],tlon,tlat,work) # compute zonal average if Log: za_data = (MA.exp(MA.average(new_data,axis=-1, weights=N.resize(area,new_data.shape)))) else: za_data = (MA.average(new_data,axis=-1, weights=N.resize(area,new_data.shape))) return za_data, lat_t
def stock_price(cls, iteration_=1, **kwargs): """generate stock spot through stochastic process""" _rand = rand_norm(0, 1, iteration_) _isp, _rate, _div, _vol, _t = parse_kwargs( kwargs, ['isp', 'rate', 'div', 'vol', 't'], 0) return _isp * exp((_rate - _div - _vol**2 / 2) * _t + _vol * sqrt(_t) * _rand)
def relative_humidity(p, q, t, A=17.625, B=-30.11, C=610.94, masked=False): """ From Mark G. Lawrence, BAMS Feb 2005, eq. (6) RH = relative_humidity(p,q,t,A,B,C) inputs: p = pressure (Pa) q = specific humidity (kg/kg) t = temperature (K) keywords: A, B and C are optional fitting parameters from Alduchov and Eskridge (1996). Masked = False (if True, perform operation on masked arrays) output: RH = relative humidity (0-1) p, q and t can be arrays. """ if masked == False: es = C * exp(A * (t - 273.15) / (B + t)) ws = 0.62198 * es / (maximum(p, es) - (1 - 0.62198) * es) RH = q / ws else: es = C * ma.exp(A * (t - 273.15) / (B + t)) ws = 0.62198 * es / (maximum(p, es) - (1 - 0.62198) * es) RH = q / ws return RH
def findEout(self, numSamples=1000): e_out = 0 dataSamples = [self.createDataPoint() for _ in range(numSamples)] for x, y in dataSamples: e_out += log(1 + exp(-1 * multiply(y, np.dot(transpose(self.w), x)))) e_out /= float(numSamples) return e_out
def profit_discount(self, mkt_dict_, time_): """get instrument pnl for given spot""" _rate, _spot = tuple( self._load_market( mkt_dict_, [EnvParam.RiskFreeRate.value, EnvParam.UdSpotForPrice.value])) return self.payoff(_spot) * exp( -_rate * time_) - self.unit * self.price
def transform(self, a): sign = np.sign(a) masked = ma.masked_inside(a, -self.linthresh, self.linthresh, copy=False) exp = sign * self.linthresh * ma.exp(sign * masked / self.linthresh - 1) if masked.mask.any(): return ma.where(masked.mask, a, exp) else: return exp
def to_simple_returns(self) -> "SimpleReturnsSeries": from qf_lib.containers.series.simple_returns_series import SimpleReturnsSeries simple_rets_values = [exp(log_ret) - 1 for log_ret in self.values] simple_returns_tms = SimpleReturnsSeries( index=self.index.copy(), data=simple_rets_values).__finalize__(self) return simple_returns_tms
def delta(self, mkt_dict_, engine_, unit_=None): """calculate option DELTA with market data and engine""" _rate, _spot, _vol, _div, _method, _param, _sign, _strike, _t = self._prepare_risk_data(mkt_dict_, engine_) _unit = unit_ or self.unit if _method == EngineMethod.BS.value: _d1 = (log(_spot / _strike) + (_rate + _vol ** 2 / 2) * _t) / _vol / sqrt(_t) return _sign * norm.cdf(_sign * _d1) * exp(-_div * _t) * _unit elif _method == EngineMethod.MC.value: from utils.monte_carlo import MonteCarlo _iteration = _param.get(EngineParam.MCIteration.value) if not _iteration: raise ValueError("iteration not specified") if not isinstance(_iteration, int): raise ValueError("type <int> is required for iteration, not {}".format(type(_iteration))) _spot = MonteCarlo.stock_price(_iteration, isp=_spot, rate=_rate, div=_div, vol=_vol, t=_t) _step = 0.01 _delta = [(max(_sign * (_s + _step - _strike), 0) - max(_sign * (_s - _step - _strike), 0)) / (_step * 2) for _s in _spot] return average(_delta) * exp(-_rate * _t) * _unit
def thelenTensonForce(self, tendonLength, tendonSlackLength): self.tendonLength = tendonLength self.tendonSlackLength = tendonSlackLength self.relativeTendonLength = ( self.tendonLength - self.tendonSlackLength ) / self.tendonSlackLength if self.relativeTendonLength <= 0 : self.tendonForce = 0 elif self.relativeTendonLength > 0 and self.relativeTendonLength <= self.tendonStrainLinear : self.tendonForce = self.tendonNormalizedMaximalForceLinear * (exp(self.tendonNonlinearShapeFactor * self.relativeTendonLength / self.tendonStrainLinear) - 1) / ( exp( self.tendonNonlinearShapeFactor ) - 1) else :# self.relativeTendonLength > self.tendonStrainLinear : self.tendonForce = self.tendonLinearShapeFactor * (self.relativeTendonLength - self.tendonStrainLinear ) + self.tendonNormalizedMaximalForceLinear return self.tendonForce + 0.001 * (1 + self.relativeTendonLength)
def thelenPassiveForce(self, muscleLength, muscleOptimaleLength): self.muscleLength = muscleLength self.muscleOptimaleLength = muscleOptimaleLength self.normalizedMuscleLength = self.muscleLength / self.muscleOptimaleLength if self.normalizedMuscleLength <= 1 + self.passiveNormalizedMaximalForce : passiveMuscleForce = (exp(self.passiveExpoShapeFactor*(self.normalizedMuscleLength-1)/self.passiveNormalizedMaximalForce)) / (exp(self.passiveExpoShapeFactor)) elif self.normalizedMuscleLength > 1 + self.passiveNormalizedMaximalForce : passiveMuscleForce = 1+ self.passiveExpoShapeFactor/self.passiveNormalizedMaximalForce * (self.normalizedMuscleLength-(1+self.passiveNormalizedMaximalForce)) else :# self.relativeTendonLength > self.tendonStrainLinear : passiveMuscleForce = 0 return passiveMuscleForce
def _apply_function(func, arg): # type: (QuilParser.FunctionContext, Any) -> Any if func.SIN(): return sin(arg) elif func.COS(): return cos(arg) elif func.SQRT(): return sqrt(arg) elif func.EXP(): return exp(arg) elif func.CIS(): return cos(arg) + complex(0, 1) * sin(arg) else: raise RuntimeError("Unexpected function to apply: " + str(func))
def entropyScore(dist, labels, label, cache=None): indices = np.where(labels == label)[0] if cache is not None: tindices = tuple(indices) if tindices in cache: return cache[tindices] if CYTHON: if conf.clustering == 'FastDBSCAN': result = cchen.entropyScoreDistIndex(dist, indices) else: result = cchen.entropyScore(dist, indices) else: if dist is None: return None # E(X) = - S_j( 1/m log( S_i( exp(-d(x_i, x_j, t ) 1/m ) ) # = - 1/m S_j( log( 1/m S_i( exp(-d(x_i, x_j, t ) ) ) s1 = 0 for j in indices: s2 = 0 for i in indices: if conf.clustering == 'FastDBSCAN': s2 = s2 + exp(-dist.get(i,j)) else: s2 = s2 + exp(-dist[i,j]) s2 = s2 / len(indices) s1 = s1 + log(s2) s1 = s1 /len(indices) result = -s1 if cache is not None: cache[tindices] = result return result
def runLogisticRegression(self): diff = 1 while diff > 0.01: permutation = np.random.permutation(self.N) newWeights = self.w.copy() for i in permutation: x, y = self.trainingData[i] gradient = divide( multiply(-1.0, multiply(x, y)), (1.0 + exp(multiply(y, np.dot(transpose(self.w), x))))) newWeights = subtract(newWeights, multiply(self.learningRate, gradient)) self.epoch += 1 diff = norm(self.w - newWeights) self.w = newWeights
def geoMean(array): ''' Generate the geometric mean of a list or array, removing all zero-values but retaining total length ''' if isinstance(array, pandas.core.frame.DataFrame): array = array.as_matrix() else: pass non_zero = ma.masked_values(array, 0) log_a = ma.log(non_zero) geom_mean = ma.exp(log_a.mean()) return geom_mean
def sigmodFunction(dataArr, theta): z = dot(dataArr, theta) p = 1e-5 hx = 1 / (1 + exp(-z)) # print "sigmod Function ä¸hx 的值为:",hx hx_new = [] for f in hx.flat: if f == 1: hx_new.append([f - p]) elif f == 0: hx_new.append([f + p]) else: hx_new.append([f]) hx = asarray(hx_new) return hx
def sigmodGradient(z): gz = 1.0 / (1 + exp(-z)) p = 1e-5 m, n = shape(gz) gz_new = [] for f in gz.flat: if f == 1: gz_new.append([f - p]) elif f == 0: gz_new.append([f + p]) else: gz_new.append([f]) gz = asarray(gz_new) gz = reshape(gz, (m, n)) return gz * (1 - gz)
def sigmodFunction(z2): p = 1e-5 hx = 1.0 / (1 + exp(-z2)) # print "sigmod Function ä¸hx 的值为:",hx m, n = shape(hx) hx_new = [] for f in hx.flat: if f == 1: hx_new.append([f - p]) elif f == 0: hx_new.append([f + p]) else: hx_new.append([f]) hx = asarray(hx_new) hx = reshape(hx, (m, n)) return hx
def vapor_pressure(temp): ''' Calculate the saturation water vapor (partial) pressure given *temperature*. temp : scalar or array The temperature in degrees Celsius. Returns : scalar or array The saturation water vapor (partial) presure in millibars, with the same shape as *temp*. Instead of temperature, dewpoint may be used in order to calculate the actual (ambient) water vapor (partial) pressure. ''' return sat_pressure_0c * exp(17.67 * temp / (temp + 243.5))
def saturation_vapor_pressure(temp): """ Calculate the saturation water vapor (partial) pressure given *temperature*. temp : scalar or array The temperature in degrees Celsius. Returns : scalar or array The saturation water vapor (partial) presure in millibars, with the same shape as *temp*. Instead of temperature, dewpoint may be used in order to calculate the actual (ambient) water vapor (partial) pressure. """ return sat_pressure_0c * exp(17.67 * temp / (temp + 243.5))
def compute_centroid(im,sigw=None,nb_iter=4): """ Computes centroid. #TODO: would be interesting to compare with Sam's moments based computation Calls: * gaussfitter.gaussfit """ if sigw is None: param=gaussfitter.gaussfit(im,returnfitimage=False) #print param sigw = (param[3]+param[4])/2 sigw = float(sigw) n1 = im.shape[0] n2 = im.shape[1] rx = array(range(0,n1)) ry = array(range(0,n2)) Wc = ones((n1,n2)) centroid = zeros((1,2)) # Four iteration loop to compute the centroid i=0 for i in range(0,nb_iter): xx = npma.outerproduct(rx-centroid[0,0],ones(n2)) yy = npma.outerproduct(ones(n1),ry-centroid[0,1]) W = npma.exp(-(xx**2+yy**2)/(2*sigw**2)) centroid = zeros((1,2)) # Estimate Centroid Wc = copy(W) if i == 0:Wc = ones((n1,n2)) totx=0.0 toty=0.0 cx=0 cy=0 for cx in range(0,n1): centroid[0,0] += (im[cx,:]*Wc[cx,:]).sum()*(cx) totx += (im[cx,:]*Wc[cx,:]).sum() for cy in range(0,n2): centroid[0,1] += (im[:,cy]*Wc[:,cy]).sum()*(cy) toty += (im[:,cy]*Wc[:,cy]).sum() centroid = centroid*array([1/totx,1/toty]) return (centroid,Wc)
def compute_centroid(im, sigw=None, nb_iter=4): """ Computes centroid. #TODO: would be interesting to compare with Sam's moments based computation Calls: * gaussfitter.gaussfit """ if sigw is None: param = gaussfitter.gaussfit(im, returnfitimage=False) #print param sigw = (param[3] + param[4]) / 2 sigw = float(sigw) n1 = im.shape[0] n2 = im.shape[1] rx = array(range(0, n1)) ry = array(range(0, n2)) Wc = ones((n1, n2)) centroid = zeros((1, 2)) # Four iteration loop to compute the centroid i = 0 for i in range(0, nb_iter): xx = npma.outerproduct(rx - centroid[0, 0], ones(n2)) yy = npma.outerproduct(ones(n1), ry - centroid[0, 1]) W = npma.exp(-(xx**2 + yy**2) / (2 * sigw**2)) centroid = zeros((1, 2)) # Estimate Centroid Wc = copy(W) if i == 0: Wc = ones((n1, n2)) totx = 0.0 toty = 0.0 cx = 0 cy = 0 for cx in range(0, n1): centroid[0, 0] += (im[cx, :] * Wc[cx, :]).sum() * (cx) totx += (im[cx, :] * Wc[cx, :]).sum() for cy in range(0, n2): centroid[0, 1] += (im[:, cy] * Wc[:, cy]).sum() * (cy) toty += (im[:, cy] * Wc[:, cy]).sum() centroid = centroid * array([1 / totx, 1 / toty]) return (centroid, Wc)
def predictRes(x, theta, y, threshold): dataArr = asarray(x) labelArr = asarray(y) n = shape(y)[0] # print theta z = dot(x, theta) p = 1.0 / (1 + exp(-1 * z)) p[p < threshold] = 0 p[p >= threshold] = 1 # print p res = p - labelArr count = 0 for r in res.flat: if r != 0: count += 1 print count print n return (n - count * 1.0) / n
def ljung_box_pierce(cross_correlation_array, length, n_lag): """Calculate Ljung-Box-Pierce statistics Args: cross_correlation_array: array of spatial maps of correlations, for each lag length: length of the time series n_lag: integer, maximum temporal lag to sum Return: 2d array, same shape as value_map[0, :, :], contains lbp statistics """ statistic = ma.empty_like(cross_correlation_array[0]) statistic[np.logical_not(statistic.mask)] = 0 for i in range(n_lag + 1): statistic += (ma.exp(2 * ma.log(cross_correlation_array[i]) - math.log(length - i))) return statistic
def compute_centroid(im,sigw,nb_iter=4): n1 = im.shape[0] n2 = im.shape[1] rx = array(range(0,n1)) ry = array(range(0,n2)) Wc = ones((n1,n2)) centroid = zeros((1,2)) # Four iteration loop to compute the centroid i=0 for i in range(0,nb_iter): xx = npma.outerproduct(rx-centroid[0,0],ones(n2)) yy = npma.outerproduct(ones(n1),ry-centroid[0,1]) W = npma.exp(-(xx**2+yy**2)/(2*sigw**2)) centroid = zeros((1,2)) # Estimate Centroid Wc = copy(W) if i == 0:Wc = ones((n1,n2)) totx=0.0 toty=0.0 cx=0 cy=0 for cx in range(0,n1): centroid[0,0] += (im[cx,:]*Wc[cx,:]).sum()*(cx) totx += (im[cx,:]*Wc[cx,:]).sum() for cy in range(0,n2): centroid[0,1] += (im[:,cy]*Wc[:,cy]).sum()*(cy) toty += (im[:,cy]*Wc[:,cy]).sum() centroid = centroid*array([1/totx,1/toty]) return (centroid,Wc)
def thelenActiveMuscleForce(self, muscleLength, muscleOptimaleLength): self.muscleOptimalLength = muscleOptimaleLength self.muscleLength = muscleLength return exp(- (muscleLength / self.muscleOptimalLength - 1) ** 2 / self.kShapeActive)
def run_model(self): """ Run the model :return: """ shape = self._shape tew = self._tew taw = self._taw start_time = datetime.now() pkcb = zeros(shape) swe = zeros(shape) tot_snow = zeros(shape) tot_mass = zeros(shape) cum_mass = zeros(shape) ref_et = zeros(shape) a_min = ones(shape) * 0.45 a_max = ones(shape) * 0.90 a = a_max pA = a_min days = list(rrule.rrule(rrule.DAILY, dtstart=self._start, until=self._end)) nsteps = len(days) plt_day = zeros(nsteps) plt_rain = zeros(nsteps) plt_eta = zeros(nsteps) plt_snow_fall = zeros(nsteps) plt_ro = zeros(nsteps) plt_dr = zeros(nsteps) plt_de = zeros(nsteps) plt_drew = zeros(nsteps) plt_temp = zeros(nsteps) plt_dp_r = zeros(nsteps) plt_ks = zeros(nsteps) plt_pdr = zeros(nsteps) plt_etrs = zeros(nsteps) plt_kcb = zeros(nsteps) plt_ppt = zeros(nsteps) plt_ke = zeros(nsteps) plt_kr = zeros(nsteps) plt_mlt = zeros(nsteps) plt_swe = zeros(nsteps) plt_tempm = zeros(nsteps) plt_fs1 = zeros(nsteps) plt_mass = zeros(nsteps) p_mo_et = zeros(shape) p_mo_precip = zeros(shape) p_mo_ro = zeros(shape) p_mo_deps = self._dr + self._de + self._drew p_mo_infil = zeros(shape) p_mo_etrs = zeros(shape) p_yr_et = zeros(shape) p_yr_precip = zeros(shape) p_yr_ro = zeros(shape) p_yr_deps = self._dr + self._de + self._drew p_yr_infil = zeros(shape) p_yr_etrs = zeros(shape) start_month = self._start_month end_month = self._end_month for i, dday in enumerate(days): if i > 0: pkcb = kcb doy = dday.timetuple().tm_yday year = dday.year month = dday.month day = dday.day msg = 'Time : {} day {}_{}'.format(datetime.now() - start_time, doy, year) logging.debug(msg) # -------------- kcb ------------------- if year == 2000: ndvi = self.calculate_ndvi_2000(doy) elif year == 2001: ndvi = self.calculate_ndvi_2001(doy) else: ndvi = self.calculate_ndvi(year, doy) kcb = ndvi * 1.25 kcb = maximum(kcb, self._min_val) kcb = where(isnan(kcb), pkcb, kcb) # -------------- PRISM ------------------- ppt, ppt_tom, max_temp, min_temp, mid_temp = self.load_prism(dday) # -------------- PM ------------------- # PM data to etrs name = os.path.join('PM{}'.format(year), 'PM_NM_{}_{:03n}'.format(year, doy)) etrs = tif_to_array(self._pm_data_root, name) etrs = maximum(etrs, self._min_val) name = os.path.join('PM{}'.format(year), 'RLIN_NM_{}_{:03n}'.format(year, doy)) rlin = tif_to_array(self._pm_data_root, name) rlin = maximum(rlin, zeros(shape)) name = os.path.join('rad{}'.format(year), 'RTOT_{}_{:03n}'.format(year, doy)) rg = tif_to_array(self._pm_data_root, name) rg = maximum(rg, zeros(shape)) if i == 0: # Total evaporable water is depth of water in the evaporable # soil layer, i.e., the water available to both stage 1 and 2 evaporation rew = minimum((2 + (tew / 3.)), 0.8 * tew) # del tew1, tew2 # you should have all these from previous model runs pDr = self._dr pDe = self._de pDrew = self._drew dr = self._dr de = self._de drew = self._drew nom = 2 if start_month <= doy <= end_month else 6 ksat = self._ksat * nom / 24. kc_max_1 = kcb + 0.0001 min_val = ones(shape) * 0.0001 kc_max = maximum(min_val, kc_max_1) self._nlcd_plt_hgt = self._nlcd_plt_hgt * 0.5 + 1 numr = maximum(kcb - self._kc_min, min_val * 10) denom = maximum((kc_max - self._kc_min), min_val * 10) fcov_ref = (numr / denom) ** self._nlcd_plt_hgt fcov_min = minimum(fcov_ref, ones(shape)) fcov = maximum(fcov_min, min_val * 10) few = maximum(1 - fcov, 0.01) # exposed ground pKr = kr kr = minimum(((tew - de) / (tew - rew)), ones(shape)) kr = where(isnan(kr), pKr, kr) pKs = ks ks_ref = where(((taw - pDr) / (0.6 * taw)) < zeros(shape), ones(shape) * 0.001, ((taw - pDr) / (0.6 * taw))) ks_ref = where(isnan(ks), pKs, ks_ref) ks = minimum(ks_ref, ones(shape)) # Ke evaporation reduction coefficient; stage 1 evaporation fsa = where(isnan((rew - drew) / (KE_MAX * etrs)), zeros(shape), (rew - drew) / (KE_MAX * etrs)) fsb = minimum(fsa, ones(shape)) fs1 = maximum(fsb, zeros(shape)) ke = where(drew < rew, minimum((fs1 + (1 - fs1) * kr) * (kc_max - ks * kcb), few * kc_max), zeros(shape)) transp = (ks * kcb) * etrs et_init = (ks * kcb + ke) * etrs eta = maximum(et_init, zeros(shape)) evap_init = ke * etrs evap_min = maximum(evap_init, zeros(shape)) evap = minimum(evap_min, kc_max) # Load temp, find swe, melt, and precipitation, load Ksat # Use SNOTEL data for precip and temps: # df_snow : (stel_date, stel_snow, stel_precip, stel_tobs, stel_tmax, stel_tmin, stel_tavg, stel_snwd) snow_fall = where(mid_temp <= 0.0, ppt, zeros(shape)) rain = where(mid_temp >= 0.0, ppt, zeros(shape)) pA = a a = where(snow_fall > 3.0, ones(shape) * a_max, a) a = where(snow_fall <= 3.0, a_min + (pA - a_min) * exp(-0.12), a) a = where(snow_fall == 0.0, a_min + (pA - a_min) * exp(-0.05), a) a = where(a < a_min, a_min, a) swe += snow_fall mlt_init = maximum(((1 - a) * rg * 0.2) + (mid_temp - 1.8) * 11.0, zeros(shape)) # use calibrate coefficients mlt = minimum(swe, mlt_init) swe -= mlt # Find depletions pDr = dr pDe = de pDrew = drew watr = rain + mlt deps = dr + de + drew ro = zeros(shape) ro = where(watr > ksat + deps, watr - ksat - deps, ro) ro = maximum(ro, zeros(shape)) dp_r = zeros(shape) id1 = where(watr > deps, ones(shape), zeros(shape)) id2 = where(ksat > watr - deps, ones(shape), zeros(shape)) dp_r = where(id1 + id2 > 1.99, maximum(watr - deps, zeros(shape)), dp_r) dp_r = where(watr > ksat + deps, ksat, dp_r) dp_r = maximum(dp_r, zeros(shape)) drew_1 = minimum((pDrew + ro + (evap - (rain + mlt))), rew) drew = maximum(drew_1, zeros(shape)) diff = maximum(pDrew - drew, zeros(shape)) de_1 = minimum((pDe + (evap - (rain + mlt - diff))), tew) de = maximum(de_1, zeros(shape)) diff = maximum(((pDrew - drew) + (pDe - de)), zeros(shape)) dr_1 = minimum((pDr + ((transp + dp_r) - (rain + mlt - diff))), taw) dr = maximum(dr_1, zeros(shape)) # Create cumulative rasters to show net over entire run infil += dp_r infil = maximum(infil, zeros(shape)) prev_et = et ref_et += etrs et = et + evap + transp et_ind = et / ref_et et = where(isnan(et) == True, prev_et, et) et = where(et > ref_et, ref_et / 2., et) et = maximum(et, ones(shape) * 0.001) precip = precip + rain + snow_fall precip = maximum(precip, zeros(shape)) runoff += ro runoff = maximum(runoff, zeros(shape)) snow_ras = swe + snow_fall - mlt snow_ras = maximum(snow_ras, zeros(shape)) tot_snow += snow_fall mo_date = calendar.monthrange(year, month) if day == mo_date[1]: infil_mo = infil - p_mo_infil infil_mo = maximum(infil_mo, zeros(shape)) ref_et_mo = etrs - p_mo_etrs et_mo = et - p_mo_et et_mo = where(isnan(et_mo) == True, p_mo_et, et_mo) et_mo = where(et_mo > ref_et, ref_et / 2., et_mo) et_mo = maximum(et_mo, ones(shape) * 0.001) precip_mo = precip - p_mo_precip precip_mo = maximum(precip_mo, zeros(shape)) runoff_mo = ro - p_mo_ro runoff_mo = maximum(runoff_mo, zeros(shape)) snow_ras_mo = swe snow_ras_mo = maximum(snow_ras_mo, zeros(shape)) deps_mo = drew + de + dr delta_s_mo = p_mo_deps - deps_mo outputs = (('infil', infil_mo), ('et', et_mo), ('precip', precip_mo), ('runoff', runoff_mo), ('snow_ras', snow_ras_mo), ('delta_s_mo', delta_s_mo), ('deps_mo', deps_mo)) self.save_month_step(outputs, month, year) p_mo_et = et p_mo_precip = precip p_mo_ro = ro p_mo_deps = deps_mo p_mo_infil = infil p_mo_etrs = etrs if day == 31 and month == 12: infil_yr = infil - p_yr_infil infil_yr = maximum(infil_yr, zeros(shape)) ref_et_yr = etrs - p_yr_etrs et_yr = et - p_yr_et et_yr = where(isnan(et_yr) == True, p_yr_et, et_yr) et_yr = where(et_yr > ref_et, ref_et / 2., et_yr) et_yr = maximum(et_yr, ones(shape) * 0.001) precip_yr = precip - p_yr_precip precip_yr = maximum(precip_yr, zeros(shape)) runoff_yr = ro - p_yr_ro runoff_yr = maximum(runoff_yr, zeros(shape)) snow_ras_yr = swe snow_ras_yr = maximum(snow_ras_yr, zeros(shape)) deps_yr = drew + de + dr delta_s_yr = p_yr_deps - deps_yr outputs = (('infil', infil_yr), ('et', et_yr), ('precip', precip_yr), ('runoff', runoff_yr), ('snow_ras', snow_ras_yr), ('delta_s_yr', delta_s_yr), ('deps_yr', deps_yr)) p_yr_et = et p_yr_precip = precip p_yr_ro = ro p_yr_deps = deps_yr # this was originally p_mo_deps = deps_yr im assuming this is a typo p_yr_infil = infil p_yr_etrs = etrs self.save_year_step(outputs, month, year) # Check MASS BALANCE for the love of WATER!!! mass = rain + mlt - (ro + transp + evap + dp_r + ((pDr - dr) + (pDe - de) + (pDrew - drew))) tot_mass += abs(mass) cum_mass += mass plt_day[i] = dday plt_rain[i] = rain[S, E] plt_eta[i] = eta[S, E] plt_snow_fall[i] = snow_fall[S, E] plt_ro[i] = ro[S, E] plt_dr[i] = dr[S, E] plt_de[i] = de[S, E] plt_drew[i] = drew[S, E] plt_temp[i] = mid_temp[S, E] plt_dp_r[i] = dp_r[S, E] plt_ks[i] = ks[S, E] plt_pdr[i] = pDr[S, E] plt_etrs[i] = etrs[S, E] plt_kcb[i] = kcb[S, E] plt_ppt[i] = ppt[S, E] plt_ke[i] = ke[S, E] plt_kr[i] = kr[S, E] plt_mlt[i] = mlt[S, E] plt_swe[i] = swe[S, E] plt_tempm[i] = max_temp[S, E] plt_fs1[i] = fs1[S, E] plt_mass[i] = mass[S, E]
def logist(x, loc_, scale_): return exp((loc_ - x) / scale_) / (scale_ * (1 + exp((loc_ - x) / scale_)) ** 2)
def sigmoid(inX): return 1.0 / (1 + exp(-inX))
def py_sigmoidal(x, asym=1.0, xmid=None, scale=1.0): if xmid is None: xmid = ma.median(x) return asym / (1 + ma.exp((xmid - x) / scale))
def sigmoidal(self, x, asym=1.0, xmid=None, xmod=0, scale=1.0): if xmid is None: xmid = ma.median(x) xmid = xmid + xmod return asym / (1 + ma.exp((xmid - x) / scale))