def default_arg(arg=None): ##{{{ """ Dictionary of arguments of __init__ Parameters ---------- arg : None or dict dictionary of arguments already fixed Returns ------- default: dict Arguments of __init__, elements of "arg" are kept """ default = { "tails": "upper", "link_scale": sdt.IdLink(), "link_shape": sdt.IdLink(), "method": "MLE", "verbose": False } if arg is not None: for key in arg: default[key] = arg[key] return default
def to_netcdf( self ):##{{{ ncargs = { "ns_law_name" : "GEVPr" } ncargs[ "ns_law_param_loc_cst" ] = False ncargs[ "ns_law_param_loc_link" ] = sdt.IdLink() ncargs[ "ns_law_param_scale_cst" ] = False ncargs[ "ns_law_param_scale_link" ] = sdt.IdLink() ncargs[ "ns_law_param_shape_cst" ] = False ncargs[ "ns_law_param_shape_link" ] = sdt.IdLink() ncargs[ "ns_law_param_alpha_cst" ] = False ncargs[ "ns_law_param_alpha_link" ] = sdt.IdLink() return ncargs
def __init__(self, tails="upper", link_loc=sdt.IdLink(), link_scale=sdt.ExpLink(), link_shape=sdt.LogitLink(-0.5, 0.5), method="MLE", no_test=True, verbose=False): ##{{{ """ """ NSAbstractModel.__init__(self) self._tails = 1 if tails == "upper" else -1 self._gev = sd.GEVLaw(method=method) self._link = { "loc": link_loc, "scale": link_scale, "shape": link_shape } self._no_test = no_test self._verbose = verbose self._loc0 = None self._loc1 = None self._scale0 = None self._scale1 = None self._shape = None self.loc = None self.scale = None self.shape = None self._loct = None self._scalet = None self._shapet = None
def __init__( self , name , is_cst , link , name_tex = None ):##{{{ self.name = name self.link = link if link is not None else sdt.IdLink() self.is_cst = is_cst self.n_params = 1 if self.is_cst else 2 self.coef_ = None self._paramst = None self.name_tex = name_tex if name_tex is None: self.name_tex = "\mathrm{" + self.name + "}"
def __init__(self, link_loc=sdt.IdLink(), link_scale=sdt.ExpLink(), method="MLE", verbose=False): ##{{{ """ Initialization of the NS Gaussian Model """ NSAbstractModel.__init__(self) self._norm = sd.Normal(method=method) self._verbose = verbose self._link = {"loc": link_loc, "scale": link_scale} self.mu0 = None self.mu1 = None self.scale0 = None self.scale1 = None self._mut = None self._scalet = None
def from_netcdf(ifile, ns_law=None): ##{{{ with nc.Dataset(ifile, "r") as ncFile: ## Extract dimensions ##=================== time = np.ma.getdata(ncFile.variables["time"][:]) sample = ncFile.variables["sample"][:] forcing = ncFile.variables["forcing"][:] models = ncFile.variables["models"][:] ns_param = ncFile.variables["ns_param"][:] stats = ncFile.variables["stat"][:] reference = np.ma.getdata(ncFile.variables["reference"][:]) if ns_law is None: ## Extract ns_law attributes ##========================== ns_law_attr = [] for p in ncFile.__dict__: if "ns_law" in p: ns_law_attr.append(p) ## Transform ns_law attributes to ns_law params ##============================================= ns_law_kwargs = {} for p in ns_law_attr: if "ns_law_param" in p: pn, pk = p.split("_")[-2:] if pk == "cst": ns_law_kwargs[pn + "_cst"] = ncFile.__dict__[p] == str(True) if pk == "link": if ncFile.__dict__[p] == str(sdt.ExpLink()): ns_law_kwargs["l_" + pn] = sdt.ExpLink() else: ns_law_kwargs["l_" + pn] = sdt.IdLink() ns_law = None if ncFile.__dict__["ns_law_name"] == "Normal": ns_law = Normal(**ns_law_kwargs) elif ncFile.__dict__["ns_law_name"] == "GEV": ns_law = GEV(**ns_law_kwargs) elif ncFile.__dict__["ns_law_name"] == "GEVMin": ns_law = GEVMin(**ns_law_kwargs) ## Set climatology ##================= clim = Climatology(time, models, ns_law) clim.X = xr.DataArray(np.ma.getdata(ncFile.variables["X"][:]), coords=[time, sample, forcing, models], dims=["time", "sample", "forcing", "models"]) clim.ns_params = xr.DataArray(np.ma.getdata( ncFile.variables["ns_params"][:]), coords=[ns_param, sample, models], dims=["ns_params", "sample", "models"]) clim.stats = xr.DataArray(np.ma.getdata(ncFile.variables["stats"][:]), coords=[time, sample, stats, models], dims=["time", "sample", "stats", "models"]) clim.mm_params.mean = np.ma.getdata(ncFile.variables["mm_mean"][:]) clim.mm_params.cov = np.ma.getdata(ncFile.variables["mm_cov"][:]) ## Set event event = Event(ncFile.event_name, reference.dtype.type(ncFile.event_time), clim.X.dtype.type(ncFile.event_anomaly), reference, ncFile.event_type, ncFile.event_side, ncFile.event_variable, ncFile.event_unit) return clim, event
if __name__ == "__main__": ## Define dataset ##=============== t,X,_,_ = sdt.Dataset.covariates(2000) loc = 1 - 2 * X scale = 0.5 + 0.5 * X shape = np.repeat( -0.2 , t.size ) Y = - sc.genextreme.rvs( loc = loc , scale = scale , c = -shape ) ## mod = nsm.GEVMin( l_scale = sdt.IdLink() ) mod.fit( Y , X ) mod.set_covariable( X , t ) ub = mod.upper_boundt(t) lb = mod.lower_boundt(t) Y2 = mod.rvs(t) print(mod.get_params()) ## Plot ##===== nrow,ncol = 2,2 fig = plt.figure( figsize = (12,8) ) ax = fig.add_subplot( nrow , ncol , 1 ) ax.plot( t , X , color = "red" ) ax.plot( t , Y , color = "blue" , linestyle = "" , marker = "." )