def __init__(self): self.sigma = Sigma() #sigma(M) # read in MXXL mass function fit parameters snap, redshift, A, a, p = \ np.loadtxt(par.mf_fits_file, skiprows=1, unpack=True) # interpolate parameters self._A = RegularGridInterpolator((redshift,), A, bounds_error=False, fill_value=None) self._a = RegularGridInterpolator((redshift,), a, bounds_error=False, fill_value=None) self._p = RegularGridInterpolator((redshift,), p, bounds_error=False, fill_value=None)
def __init__(self, k=None, k_min=np.exp(-5), k_max=np.exp(5), kbins=1000, lnM=None, lnM_min=np.log(1e11), lnM_max=np.log(1e15), lnMbins=100, transfer_fit="EH", hmf_fit="Tinker", bias_fit="Tinker", incl_baryons=True, **kwargs): #initializing cosmology inherited from the Universe class super(Matter, self).__init__(**kwargs) #initializing other stuff if k != None: self.k = k else: self.k_min = k_min self.k_max = k_max self.kbins = kbins self.gen_k() if lnM != None: self.lnM = lnM else: self.lnM_min = lnM_min self.lnM_max = lnM_max binfrac = 1. / lnMbins self.dlnM = binfrac * (lnM_max - lnM_min) self.gen_lnM() self.transfer_fit = transfer_fit self.hmf_fit = hmf_fit self.bias_fit = bias_fit self.incl_baryons = incl_baryons self.trackz = -1 self.master_sig = Sigma(1, 1.686, self.k, self.unnormal_p0_lin()) self.norm_P0 = self.master_sig.normalize_power(self.sig_8)
class MassFunction(object): def __init__(self): self.sigma = Sigma() #sigma(M) # read in MXXL mass function fit parameters snap, redshift, A, a, p = \ np.loadtxt(par.mf_fits_file, skiprows=1, unpack=True) # interpolate parameters self._A = RegularGridInterpolator((redshift,), A, bounds_error=False, fill_value=None) self._a = RegularGridInterpolator((redshift,), a, bounds_error=False, fill_value=None) self._p = RegularGridInterpolator((redshift,), p, bounds_error=False, fill_value=None) def A(self, redshift): return self._A(redshift) def a(self, redshift): return self._a(redshift) def p(self, redshift): return self._p(redshift) def mass_function(self, log_mass, redshift): ''' Fit to the MXXL mass function with the form of a Sheth-Tormen mass function ''' sigma = self.sigma.sigma(log_mass, redshift) dc=1 A = self.A(redshift) a = self.a(redshift) p = self.p(redshift) mf = A * np.sqrt(2*a/np.pi) mf *= 1 + (sigma**2 / (a * dc**2))**p mf *= dc / sigma mf *= np.exp(-a * dc**2 / (2*sigma**2)) return mf
from flask import Flask, jsonify, request app = Flask(__name__) from sigma import Sigma, IFD, DG from construct import Container camera = Sigma() # camera = PTPy(extension=sigma.Sigma, idVendor=0x1003, idProduct=0xc432) @app.route('/') def home(): return 'hoge' @app.route('/api/open_session') def open_session(): response = camera.open_session() return response @app.route('/api/close_session') def close_session(): response = camera.close_session() return response @app.route('/api/config') def config(): response = camera.config_api() return response.Data
key = None secret = None cfgFileName = dirName + '/../conf.cfg' if os.path.isfile(cfgFileName): file = open(cfgFileName, 'r+') config = json.load(file) file.close() if 'db' in config: db = config['db'] if 'user' in config: user = config['user'] if 'pswd' in config: pswd = config['pswd'] sigma = Sigma(key, secret, pair, db, user, pswd) sigma.invest = invest sigma.startIndent = startIndent sigma.totalIndent = totalIndent sigma.minProfitPercent = minProfitPercent sigma.incInvest = incInvest sigma.printCascade(cascade) #print(cascade['investOrders'][-1]) cascade = sigma.incCascade(cascade) sigma.printCascade(cascade) saveCascadeFile(cascadeFileName, cascade)
configFileName = dirName + '/../conf.cfg' if os.path.isfile(configFileName): file = open(configFileName, 'r+') config = json.load(file) file.close() if 'db' in config: db = config['db'] if 'user' in config: user = config['user'] if 'pswd' in config: pswd = config['pswd'] pair = 'btc_rur' sigma = Sigma(None, None, pair, db, user, pswd) sigma.invest = 16000.0 #57.5 #10.0 # sigma.totalIndent = 3.0 sigma.startIndent = 0.15 sigma.minProfitPercent = 1.1 sigma.incInvest = 1.5 cascade = sigma.createCascade() #'sell') sigma.printCascade(cascade) quit() cou = 0 for investOrder in cascade['investOrders']: investOrder['orderId'] = 666 if cou > len(cascade['investOrders']) / 2: #cascade['profitOrders'][cou]['orderId'] = 666
print('dbSigma: {0}, newSigma: {1}, externalSigma: {2}'.format(dbSigma, newSigma, sigma)) #quit() res, error = exchange.getTicker([pair]) if not res: print(error) quit() print('last price: {0}. sigma: {1}. dbSigma: {2}.'.format(res[pair]['last'], sigma, dbSigma)) print('{2} {1} - {3} - {0}'.format(res[pair]['last'] - 3 * sigma, res[pair]['last'] + 3 * sigma, pair, res[pair]['last'])) print('deep 3 sigma percent: {0}'.format(3 * sigma / res[pair]['last'] * 100)) from sigma import Sigma depth = raw_input('enter sigma depth (604800 for week, 2592000 for month): ') engine = Sigma(None, None, pair, db, user, pswd, depth) engine.invest = float(raw_input('enter invest: ')) engine.startIndent = float(raw_input('enter startIndent: ')) engine.minProfitPercent = float(raw_input('enter minProfitPercent: ')) engine.incInvest = float(raw_input('enter incInvest: ')) while True: engine.totalIndent = float(raw_input('enter totalIndent or 0 for exit: ')) if engine.totalIndent == 0: break cascade = engine.createCascade() engine.printCascade(cascade) quit() pair = 'btc_rur'
class Matter(Universe): """ The purpose of this class is to deliver all the objects necessary to compute the galaxy-galaxy or galaxy-matter power spectra. These are: ------------------------------------------------ Inputs : Cosmology = list of cosmological parameters inherited from the Universe k = array of wave numbers (unit = Mpc^-1) z = redshift M = list of halo masses (unit Msun/h) ------------------------------------------------- Returns: Plin(k, z) = 1d Array: linear matter power spectrum evaluated at array of k values at redshift z, dndlnm(z) = 1d Array: \frac{d\n}{d\ln(m)} Halo mass function evaluated at array of halo masses (M) and redshift z bh(m, z) = 1d Array: halo bias evaluated at array of halo masses (M) and redshift z ug(k, m, z)= 2d Array: Fourier transform of dark matter halo density profile (here we assume NFW) ------------------------------------------------ """ def __init__(self, k=None, k_min=np.exp(-5), k_max=np.exp(5), kbins=1000, lnM=None, lnM_min=np.log(1e11), lnM_max=np.log(1e15), lnMbins=100, transfer_fit="EH", hmf_fit="Tinker", bias_fit="Tinker", incl_baryons=True, **kwargs): #initializing cosmology inherited from the Universe class super(Matter, self).__init__(**kwargs) #initializing other stuff if k != None: self.k = k else: self.k_min = k_min self.k_max = k_max self.kbins = kbins self.gen_k() if lnM != None: self.lnM = lnM else: self.lnM_min = lnM_min self.lnM_max = lnM_max binfrac = 1. / lnMbins self.dlnM = binfrac * (lnM_max - lnM_min) self.gen_lnM() self.transfer_fit = transfer_fit self.hmf_fit = hmf_fit self.bias_fit = bias_fit self.incl_baryons = incl_baryons self.trackz = -1 self.master_sig = Sigma(1, 1.686, self.k, self.unnormal_p0_lin()) self.norm_P0 = self.master_sig.normalize_power(self.sig_8) def gen_k(self): """ array of wave numbers """ self.k = np.logspace(np.log(self.k_min), np.log(self.k_max), num=self.kbins) def gen_lnM(self): """ array of log halo masses """ self.lnM = np.arange(self.lnM_min, self.lnM_max, self.dlnM) def T(self): """ transfer function """ return transfnc_eh(self.k, Om=self.Om, h=self.h_transf, T_cmb=self.T_cmb, incl_baryons=self.incl_baryons) def delta_c(self, z): """ critical matter over density for spherical collapse at redshift z, taken from van der Bosch (2013) """ return 1.686470199841145 * (self.omegamz(z)**0.0055) def unnormal_p0_lin(self): """ basic power spectrum """ return (self.k**self.ns) * (self.T()**2) def normal_p0_lin(self): """ normalized linear power spectrum at redshift zero """ return self.norm_P0 * self.unnormal_p0_lin() def normal_pz_lin(self, z): """ linear power spectrum at redshift z """ return self.normal_p0_lin() * self.gf(z) def reset_mastersig(self, z): self.master_sig = Sigma(self.rho_mean(z), self.delta_c(z), self.k, self.normal_pz_lin(z)) def check_z(self, z): if z != self.trackz: self.reset_mastersig(z) self.trackz = z def _hmf_fit(self, z): """ Tinker, 2008 fitting function for halo mass function. """ A_0 = 0.186 a_0 = 1.47 b_0 = 2.57 c_0 = 1.19 alph = np.exp(-(0.75 / np.log(200. / 75))**1.2) A_z = A_0 * (1 + z)**(-0.14) a_z = a_0 * (1 + z)**(-0.06) b_z = b_0 * (1 + z)**(-alph) sig = np.sqrt(self.master_sig.sigma_squared_m(np.exp(self.lnM))) return A_z * ((sig / b_z)**(-a_z) + 1) * np.exp(-c_0 / sig**2) def hmf(self, z): """ Compute the halo mass function (dn/dlnM) for a variety of models given the cosmological parameters """ self.check_z(z) if self.hmf_fit == "Tinker": f = self._hmf_fit(z) n = - f * (self.rho_mean(z) / np.exp(self.lnM)) * \ self.master_sig.dlnsigma_dlnm(np.exp(self.lnM)) return n def c_minfunc(self, z, Mstar): sig = np.sqrt(self.master_sig.sigma_squared_m(Mstar)) return (sig - self.delta_c(z))**2 def c200(self, m, z): """ returns halo concentration """ # From v. d. Bosch, Miyatake... F = 0.001 K = 2.7 Mstar = F * m zc = minimize(self.c_minfunc, 0.5, args=(Mstar, ))['x'] return K * ((1 + zc) / (1 + z)) def u_g(self, z): """ returns a matrix of u_g(k|m, z) in k and m for each redshift """ self.check_z(z) umat = np.zeros((self.k.shape[0], self.lnM.shape[0])) for i, m in enumerate(np.exp(self.lnM)): c = self.c200(m, z) d200 = (200. / 3) * (c**3 / (np.log(1 + c) - c / (1 + c))) mu = self.k * (self.master_sig.mass_to_radius(m) / c ) # mu is a k vector umat[:, i] = ((3 * d200) / (200 * c ** 3)) * \ (np.cos(mu) * (sici(mu + mu * c)[1] - sici(mu)[1]) + \ np.sin(mu) * (sici(mu + mu * c)[0] - sici(mu)[0]) - \ np.sin(mu * c) / (mu + mu * c) ) return umat def bias(self, z): """ returns a 1d array of halo bias b_h(m,z) in m for each redshift (Tinker 2010 model) """ self.check_z(z) sig = np.sqrt(self.master_sig.sigma_squared_m(np.exp(self.lnM))) nu = self.delta_c(z) / sig # parameters for halo overdensity of 200 y = np.log10(200) ey4 = np.exp(-(4. / y)**4) A = 1 + 0.24 * y * ey4 a = 0.44 * y - 0.88 B = 0.183 b = 1.5 C = 0.019 + 0.107 * y + 0.19 * ey4 c = 2.4 bias = 1. - A * ((nu ** a)/(nu ** a + self.delta_c(z) ** a)) + \ B * nu ** b + C * nu ** c return bias
def reset_mastersig(self, z): self.master_sig = Sigma(self.rho_mean(z), self.delta_c(z), self.k, self.normal_pz_lin(z))
secret = f.readline().strip() f.close() cfgFileName = dirName + '/../conf.cfg' if os.path.isfile(cfgFileName): file = open(cfgFileName, 'r+') config = json.load(file) file.close() if 'db' in config: db = config['db'] if 'user' in config: user = config['user'] if 'pswd' in config: pswd = config['pswd'] sigma = Sigma(key, secret, pair, db, user, pswd) sigma.path = dirName + '/../' sigma.cascadeFileName = cascadeFileName sigma.invest = invest sigma.startIndent = startIndent sigma.totalIndent = totalIndent sigma.minProfitPercent = minProfitPercent sigma.incInvest = incInvest if isExistsCascadeFile(cascadeFileName): cascadeStruct = loadCascadeFile(cascadeFileName) sigma.setParams(cascadeStruct) else: cascadeStruct = sigma.createCascade()