예제 #1
0
def getTstep(index,start,end,size):
    times_answer=[]
    if(index<0):
        index=abs(index)
        times=powerlaw.rvs(index,loc=start,scale=1,size=size)
        for time in times:
            times_answer.append(1/time)
    else:
        times_answer= powerlaw.rvs(index, loc=start, scale=end-start, size=size)
    return times_answer
예제 #2
0
def generateToy():

  np.random.seed(12345)

  fig,ax = plt.subplots(4,sharex=True)
  #fig,ax = plt.subplots(2)

  powerlaw_arg = 2
  triang_arg=0.7
  n_samples = 500
  #generate simple line with slope 1, from 0 to 1
  frozen_powerlaw = powerlaw(powerlaw_arg) #powerlaw.pdf(x, a) = a * x**(a-1)
  #generate triangle with peak at 0.7
  frozen_triangle = triang(triang_arg) #up-sloping line from loc to (loc + c*scale) and then downsloping for (loc + c*scale) to (loc+scale).
  frozen_uniform = uniform(0.2,0.5)
  frozen_uniform2 = uniform(0.3,0.2)

  x = np.linspace(0,1)

  signal = np.random.normal(0.5, 0.1, n_samples/2)

  data_frame = pd.DataFrame({'powerlaw':powerlaw.rvs(powerlaw_arg,size=n_samples),
    'triangle':triang.rvs(triang_arg,size=n_samples),
    'uniform':np.concatenate((uniform.rvs(0.2,0.5,size=n_samples/2),uniform.rvs(0.3,0.2,size=n_samples/2))),
    'powerlaw_signal':np.concatenate((powerlaw.rvs(powerlaw_arg,size=n_samples/2),signal))})

  ax[0].plot(x, frozen_powerlaw.pdf(x), 'k-', lw=2, label='powerlaw pdf')
  hist(data_frame['powerlaw'],bins=100,normed=True,histtype='stepfilled',alpha=0.2,label='100 bins',ax=ax[0])
  #hist(data_frame['powerlaw'],bins='blocks',fitness='poly_events',normed=True,histtype='stepfilled',alpha=0.2,label='b blocks',ax=ax[0])
  ax[0].legend(loc = 'best')

  ax[1].plot(x, frozen_triangle.pdf(x), 'k-', lw=2, label='triangle pdf')
  hist(data_frame['triangle'],bins=100,normed=True,histtype='stepfilled',alpha=0.2,label='100 bins',ax=ax[1])
  hist(data_frame['triangle'],bins='blocks',fitness='poly_events',normed=True,histtype='stepfilled',alpha=0.2,label='b blocks',ax=ax[1])
  ax[1].legend(loc = 'best')

  #ax[0].plot(x, frozen_powerlaw.pdf(x), 'k-', lw=2, label='powerlaw pdf')
  hist(data_frame['powerlaw_signal'],bins=100,normed=True,histtype='stepfilled',alpha=0.2,label='100 bins',ax=ax[2])
  #hist(data_frame['powerlaw_signal'],bins='blocks',normed=True,histtype='stepfilled',alpha=0.2,label='b blocks',ax=ax[2])
  ax[2].legend(loc = 'best')

  ax[3].plot(x, frozen_uniform.pdf(x)+frozen_uniform2.pdf(x), 'k-', lw=2, label='uniform pdf')
  hist(data_frame['uniform'],bins=100,normed=True,histtype='stepfilled',alpha=0.2,label='100 bins',ax=ax[3])
  #hist(data_frame['uniform'],bins='blocks',fitness = 'poly_events',p0=0.05,normed=True,histtype='stepfilled',alpha=0.2,label='b blocks',ax=ax[3])
  ax[3].legend(loc = 'best')

  plt.show()
  fig.savefig('plots/toy_plots.png')
예제 #3
0
    def __get_community_sizes(self):
        rvs = powerlaw.rvs(
            self.comm_exp,
            size=self.num_comm
        )

        comm_sizes = [int(size) for size in (rvs * self.max_comm)]
        return comm_sizes
예제 #4
0
    def __get_community_degrees(self, comm_size):
        rvs = powerlaw.rvs(
            self.degree_exp,
            size=comm_size
        )

        comm_degrees = [int(degree) for degree in (rvs * self.max_degree)]
        return comm_degrees
예제 #5
0
	def __get_community_sizes(self):
		rvs = powerlaw.rvs(
			self.comm_exp,
			size=self.num_comm
		)

		comm_sizes = [int(size) for size in (rvs * self.max_comm)]
		return comm_sizes
예제 #6
0
	def __get_community_degrees(self, comm_size):
		rvs = powerlaw.rvs(
			self.degree_exp,
			size=comm_size
		)

		comm_degrees = [int(degree) for degree in (rvs * self.max_degree)]
		return comm_degrees
예제 #7
0
    def test_rvs(self):
        # a negative value in `negpowerlaw` should be the same as the `powerlaw` from `scipy.stats` when low=0 and b=1
        arr1 = negpowerlaw.rvs_alt(-2.1, 0, 1, size=100000)
        arr2 = powerlaw.rvs(2.1, 0, 1, size=100000)

        # test same distribution by mean and variance (within 2 decimal places)
        self.assertAlmostEqual(np.mean(arr1), np.mean(arr2), 2)
        self.assertAlmostEqual(np.var(arr1), np.var(arr2), 2)
def gen_random(db, cur, pedge, min_cycle, max_cycle, min_bytes, max_bytes):
    pedge = float(pedge)
    min_cycle = int(min_cycle)
    max_cycle = int(max_cycle)
    min_bytes = int(min_bytes)
    max_bytes = int(max_bytes)
    peers = [peer_number for peer_number, in cur.execute(u"SELECT peer_number FROM predefined_identities ORDER BY peer_number")]
    numargs = powerlaw.numargs
    [a] = [0.9,] * numargs

    for first_peer_number, second_peer_number in itertools.combinations(peers, 2):
        assert first_peer_number < second_peer_number, [first_peer_number, second_peer_number]
        if random.random() < pedge:
            cycle = random.randint(min_cycle, max_cycle)
            upload_first_to_second =  powerlaw.rvs(a)*max_bytes #random.randint(min_bytes, max_bytes)
            upload_second_to_first = powerlaw.rvs(a)*max_bytes #random.randint(min_bytes, max_bytes)
            cur.execute(u"INSERT INTO predefined_books (first_peer_number, second_peer_number, cycle, upload_first_to_second, upload_second_to_first) VALUES (?, ?, ?, ?, ?)",
                        (first_peer_number, second_peer_number, cycle, upload_first_to_second, upload_second_to_first))
예제 #9
0
def get_one2many_mm(bs,
                    h,
                    w,
                    mean_match_query_term=None,
                    mean_match_doc_term=None,
                    dist='binomial'):
    '''
    Get match matrix with one query term matching many doc terms.
    @param bs: batch size
    @param h: height of the matrix (number of query terms)
    @param w: width of the matrix (number of document terms)
    @return: a numpy array of size (bs, h, w)
    '''
    if dist not in {'binomial', 'power_law', 'custom'}:
        raise Exception('not supported distribution.')
    mms = []
    for i in range(bs):
        data = [1] * w
        col = range(w)
        row = np.random.choice(h, w)
        mms.append(csr_matrix((data, (row, col)), shape=(h, w)).toarray())
    q_match_prob = min(1, mean_match_query_term /
                       h) if mean_match_query_term != None else 0.5
    d_match_prob = min(1, mean_match_doc_term /
                       w) if mean_match_doc_term != None else 0.5
    if dist == 'custom':
        # select rows (query terms) and columns (doc terms) independently
        mask = np.random.choice(2, size=(bs, h, 1), p=[1-q_match_prob, q_match_prob]) * \
               np.random.choice(2, size=(bs, 1, w), p=[1-d_match_prob, d_match_prob])
    elif dist == 'binomial':
        # the number of columns (doc terms) with match obeys binomial distribution
        # the probability of a column to be reserved (have match) is q_match_prob*d_match_prob
        mask = np.random.choice(
            2,
            size=(bs, h, w),
            p=[1 - q_match_prob * d_match_prob, q_match_prob * d_match_prob])
    elif dist == 'power_law':
        # the number of columns (doc terms) with match obeys power law distribution
        d_match_nums = []
        while len(d_match_nums) < bs:
            n = math.floor(powerlaw.rvs(POWER_LOW_A) * (POWER_LOW_MAX + 1))
            if n <= w:
                d_match_nums.append(n)
        #print(np.histogram(d_match_nums, bins=range(w + 1)))
        #plt.hist(d_match_nums, bins=range(w + 1))
        #plt.show()
        def ind_to_arr(inds, len):
            arr = np.zeros((len), dtype=np.float32)
            arr[inds] = 1
            return arr

        mask = np.stack([
            ind_to_arr(np.random.choice(w, size=(n), replace=False), w)
            for n in d_match_nums
        ])
        mask = np.expand_dims(mask, axis=1)
    return get_eval_mm(mask * np.stack(mms))
예제 #10
0
 def __init__(self, num_of_worker, a, alpha, beta, seed=2020):
     self.alpha = alpha
     self.beta = beta
     self.num_of_workers = num_of_worker
     np.random.seed(seed)
     self.num_of_samples = [
         math.ceil(i)
         for i in powerlaw.rvs(a, size=self.num_of_workers) * 2000
     ]
     self.X = []
     self.Y = []
예제 #11
0
def get_powerlaw_random_role_assignment(num_nodes, num_roles, alpha=3.0, seed=1000):
    random_role_assignment = np.zeros((num_nodes, num_roles))
    np.random.seed(seed=seed)
    simulated_data = pl.rvs(alpha, size=num_nodes)
    hist, bins = np.histogram(simulated_data, bins=num_roles-1)
    default_value = 1.0
    test = []
    roles = np.digitize(simulated_data, bins)
    for node, role in zip(xrange(num_nodes), roles):
        test.append(role)
        random_role_assignment[node][role - 1] = default_value
    return random_role_assignment
예제 #12
0
def generate_weights(nr_signals,
                     sumTo=1,
                     distribution="uniform",
                     powerlaw_param=0.5):
    if distribution == "normal":
        r = [normal(0, 3) for i in range(0, nr_signals)]
    elif distribution == "uniform":
        r = np.zeros(nr_signals) + 1 / nr_signals
    elif distribution == "powerlaw":
        r = powerlaw.rvs(powerlaw_param, size=nr_signals)
        r = [round(i / sum(r), 3) * sumTo for i in r]
    s = sum(r)
    return [i / s * sumTo for i in r]
예제 #13
0
def distribute_files(file_list, dht, m, n_f, node_number):
    ''' distribute the files at the nodes of the ring based on theirs key (id) after hasing their names
    popularity= the value that calculated from powerlaw.rvs, take the first 5 digits. As popularity at that point
    is a float number, it willl be multiplied by 100.
    At the end we roundup the value. Popularity=round(power_law*(100))'''
    files = {}
    r = powerlaw.rvs(0.75, size=n_f)
    popularity = [float(str(x)[:5]) for x in r]
    for ind, i in enumerate(file_list):
        pop = popularity[ind] * (100)
        pop = round(pop)
        files[generate_hash_name(i)] = [i, int(pop)]
    return files
예제 #14
0
def sample_ecc(x, planet, P_orb):
    """
    Samples eccentricities using a Beta distribution
    for planets (see Kipping 2013) and a power law
    distirbution for binaries (see Moe & Di Stefano 2017).
    Args:
        x (numpy array): Random numbers between 0 and 1.
        planet (bool): True if planet and False if binary.
        P_orb (float): Orbital period.
    Returns:
        x (numpy array): Sampled eccentricities.
    """
    size = len(x)
    if planet == True:
        x = beta.rvs(0.867, 3.030, size=size)
    else:
        if P_orb <= 10:
            # note the variable is nu+1, not nu
            x = powerlaw.rvs(0.2, size=size)
        else:
            x = powerlaw.rvs(0.6, size=size)
    return x
예제 #15
0
def main(a):

    #arg = raw_input("Zipf & Pareto belongs to power law. Please select: 1: Pareto\'s Law, 2: Zipf\'s law") )

    fig, ax = plt.subplots(1, 1)
    mean, var, skew, kurt = powerlaw.stats(a, moments='mvsk')

    x = power_law_dist(a)

    ax.plot(x, powerlaw.pdf(x, a), 'r-', lw=5, alpha=0.6, label='powerlaw pdf')
    rv = powerlaw(a)
    ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')
    r = powerlaw.rvs(a, size=1000)
    ax.legend(loc='best', frameon=False)

    #plt.plt(x,y)
    plt.show()
예제 #16
0
def hashing(requests, nodes):
    """Function that read the given file line by line and hash each string using SHA-1.
	The function returns a list of tuples. Each tuple has the requested item, the file's name and a random start"""

    hash_list = []
    with open('filenames.txt') as f:
        count = 0
        lines = random.sample(f.readlines(), requests)
        popularity = powerlaw.rvs(1.65,
                                  size=len(lines),
                                  discrete=True,
                                  scale=10)
        for line in lines:
            hash_object = hashlib.sha1(line)
            hash_key = int(hash_object.hexdigest(), 16) % (2**15)
            hash_tuple = (hash_key, line.rstrip('\n'), popularity[count])
            hash_list.append(hash_tuple)
            count += 1
    return hash_list
예제 #17
0
def generate_time_stamps(hyper_params={}):
    """
    Generates time stamps for using power law to sample the average number of points per day,
    and using a poisson distribution to sample the the number of points per day.
    """
    params = {
        'period': 30,
        'a': 0.2,
        'max_average': 15,
        # 'start_date': np.datetime64('2020-07-18'),
        'start_date': datetime.datetime(2020, 7, 18, 0, 0, 0, 0),
        'random_state': 1234
    }
    params.update(hyper_params)

    np.random.seed(params['random_state'])
    max_avg = params['max_average']
    period = params['period']
    start_date = params['start_date']

    avg_num_per_day = int(
        np.ceil(powerlaw.rvs(params['a'], size=1)[0] * max_avg))
    num_per_day_lst = np.random.poisson(avg_num_per_day, period)

    # time_stamps = []
    # for idx, num in enumerate(num_per_day_lst):
    #     time_stamps += num*[start_date + idx]

    time_stamps = []
    for idx, num in enumerate(num_per_day_lst):
        new_times = []
        extra_seconds = (np.random.random(num) * 3600 * 24).astype(int)
        extra_microseconds = (np.random.random(num) * 1000000).astype(int)
        for i in range(num):
            diff_time = datetime.timedelta(idx, int(extra_seconds[i]),
                                           int(extra_microseconds[i]))
            new_time = start_date + diff_time
            unix_time = new_time.timestamp()
            new_times.append(unix_time)
        time_stamps += sorted(new_times)

    return time_stamps
def generate_distribution(p, min_holdings, num_of_banks, num_of_assets, fit=False, fixed_mean=False, average=16+2.0/3):
    if fixed_mean:
        min_holdings = calc_min_holdings(average,p)
#    np.random.seed(1)
    while True:
        rvss = 1/powerlaw.rvs(p, scale=1/min_holdings, size=num_of_banks)
        if max(rvss) < num_of_assets:
            break
    fixed_rvs = rvss.copy()
    round_rvs = map(round, fixed_rvs)
    if fit:
        hist, bins = np.histogram(rvss, bins=np.logspace(np.log10(min(rvss)), np.log10(max(rvss))))
        fitfunc = lambda p, x: p[0]*x**p[1]
        errfunc = lambda p, x, y: fitfunc(p, x) - y
        p0theo = (p-1)*(min_holdings**(p-1))
        p0 = [0.5, -3]
        p1, success = optimize.leastsq(errfunc,p0[:], args=(bins[:-1], hist))
#        plt.plot(bins[:-1],hist,'.',hold=True)
#        plt.hold()
#        plt.plot(bins[:-1], fitfunc(p1,bins[:-1]), hold=True)
        print p1, p0theo, np.mean(rvss)
    return round_rvs
# Display the probability density function (``pdf``):

x = np.linspace(powerlaw.ppf(0.01, a), powerlaw.ppf(0.99, a), 100)
ax.plot(x, powerlaw.pdf(x, a), 'r-', lw=5, alpha=0.6, label='powerlaw pdf')

# Alternatively, the distribution object can be called (as a function)
# to fix the shape, location and scale parameters. This returns a "frozen"
# RV object holding the given parameters fixed.

# Freeze the distribution and display the frozen ``pdf``:

rv = powerlaw(a)
ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')

# Check accuracy of ``cdf`` and ``ppf``:

vals = powerlaw.ppf([0.001, 0.5, 0.999], a)
np.allclose([0.001, 0.5, 0.999], powerlaw.cdf(vals, a))
# True

# Generate random numbers:

r = powerlaw.rvs(a, size=1000)

# And compare the histogram:

ax.hist(r, normed=True, histtype='stepfilled', alpha=0.2)
ax.legend(loc='best', frameon=False)
plt.show()
예제 #20
0
parser.add_argument("--R",
                    "--r",
                    type=int,
                    help="Number of requests. (default 1000)",
                    default=1000)
args = parser.parse_args()
#print "Given N: ", args.N
#print "Number of requests: ",args.R

chord = Chord(args.N - 1, args.R)
chord.assignFilesToNodes()

chord.updateTables()

requestList = powerlaw.rvs(1.65,
                           size=args.R,
                           discrete=True,
                           scale=chord.getMaxNodes())

lst = [choice(chord.getNodeList()) for i in range(0, args.R)]

#print chord.getAliveNodes()
for (node, request) in zip(lst, requestList):
    node.writeToQueue([request, node.getNodeId()])

while True:
    c = 0
    for node in chord.getNodeList():
        request = node.readFromQueue()
        if request == None:  #no pending Requests in the i-nodes Queue
            c += 1
        else:
def generate_snapshot(lit_venue_count = 3, dark_venue_count = 3):
    orderbook = {}
    #lit venues first
    for lv in range(lit_venue_count):
        orderbook[lit_venues[lv]] = {}
        # buy first
        side = 1
        limit = 0
        price = 100
        while (limit < 5):         
            
            nb_orders = int(round(10*powerlaw.rvs(0.6)))
            
            if nb_orders > 0:
                orderbook[lit_venues[lv]][price] = []
                for i in range(nb_orders):
                    size = int(round(normal(100, 30))) # total qty
                    iceberg = powerlaw.rvs(0.8) # ratio of visible qty
                    decay = random()
                    
                    o = Order()
                    o.side = side
                    o.price = price
                    o.size = size
                    o.shown = int(iceberg * size)
                    o.decay = decay
                    o.id = "Order_%f_%d" %(price, i)
                    o.symbol = lit_venues[lv]
                    
                    orderbook[lit_venues[lv]][price].append(o)
                limit += 1                
            price -= 1
        
        side = -1
        limit = 0
        price = 101    
        while (limit < 5):         
            
            nb_orders = int(round(10*powerlaw.rvs(0.6)))
            
            if nb_orders > 0:
                orderbook[lit_venues[lv]][price] = []
                for i in range(nb_orders):
                    size = int(round(normal(100, 30))) # total qty
                    iceberg = powerlaw.rvs(0.8) # ratio of visible qty
                    decay = random()
                    
                    o = Order()
                    o.side = side
                    o.price = price
                    o.size = size
                    o.shown = int(iceberg * size)
                    o.decay = decay
                    o.id = "Order_%f_%d" %(price, i)
                    o.symbol = lit_venues[lv]
                    
                    orderbook[lit_venues[lv]][price].append(o)
                limit += 1                
            price += 1
            
    
    return orderbook
예제 #22
0
import time
#import pdb

###########################################################
random.seed()
output = open("syndata.txt", "w+")
S = range(4)  # list of stages
S_label = ['breeding', 'fattening', 'trader',
           'slaughter']  # or whatever 'piglet production',
#ns = [45426, 34518, 15631, 2333]                        # number of barns of every stage
ns = [4500, 3400, 1500, 600]
theta = [
    expon.rvs(scale=1 / 0.013),  #lambda=0.013
    lognorm.rvs(s=1.27, scale=math.exp(3.62)),
    lognorm.rvs(s=1.06, scale=math.exp(4.3079)),
    100 * powerlaw.rvs(a=1.42)
]  #alpha=1.42
birth_rate = [1.81, 0.09, 0.0, 0.0]
mortal_rate = [0.00042, 0.08, 0.01, 1]

# T is a set of possible transactions
T = {(0, 1, 60, 1, 0.6), (1, 2, 120, 1, 0.6), (2, 3, 1, 1, 0.7)}
barnlist = []
barn_index = {
}  # dict of pairs (start_index, end_index) giving the index ranges of barns of each type
breeding_no = 47894
fattening_no = 31628
trader_no = 15631
slaughterh_no = 2755

예제 #23
0
import numpy as np, pandas as pd, matplotlib.pyplot as plt
from scipy.stats import beta, powerlaw

n_draws = int(1e4)
a, b = 1.12, 3.09
ecc_pl = beta.rvs(a, b, size=n_draws)

eta = 0.5  # roughly

# f(x,a) = ax^{a-1} for scipy's built-in powerlaw distribution.
ecc_stellar = powerlaw.rvs(eta + 1, size=n_draws)

plt.hist(ecc_pl, color='C0', label='beta (1.12,3.09)', alpha=0.5)

plt.hist(ecc_stellar, color='C1', label='powerlaw (e^{0.5})', alpha=0.5)
plt.xlabel('ecc')
plt.legend()
outpath = '../results/beta_vs_powerlaw.png'
plt.savefig(outpath, dpi=350)
print('made {}'.format(outpath))
예제 #24
0
def _isotropic_spinmag(samples):
    a1 = powerlaw.rvs(3, size=len(samples))
    a2 = powerlaw.rvs(3, size=len(samples))
    return a1, a2
def derive_mass_semimaj_constraints(setupfn,
                                    rvfitdir,
                                    chainpath,
                                    verbose=False,
                                    multiprocess=True):

    np.random.seed(42)

    chaindf = pd.read_csv(chainpath)

    (rvtimes, rvs, rverrs, resid, telvec, dvdt, curv, dvdt_merr, dvdt_perr,
     time_base) = _get_fit_results(setupfn, rvfitdir)

    # #NOTE: debugging
    # n_mass_grid_edges = 31 # a 4x4 grid has 5 edges. want: 64+1, 128+1...
    # n_sma_grid_edges = 31 # a 4x4 grid has 5 edges.
    # n_injections_per_cell = 16 # 500 # want: 500
    # NOTE: production
    n_mass_grid_edges = 129  # a 4x4 grid has 5 edges. want: 64+1, 128+1...
    n_sma_grid_edges = 129  # a 4x4 grid has 5 edges.
    n_injections_per_cell = 512  # 500 # want: 500

    mass_grid = (
        np.logspace(np.log10(1), np.log10(900), num=n_mass_grid_edges) *
        u.Mjup)
    sma_grid = (np.logspace(np.log10(3), np.log10(500), num=n_sma_grid_edges) *
                u.AU)

    log_like_arr = np.zeros(
        (n_mass_grid_edges - 1, n_sma_grid_edges - 1, n_injections_per_cell))
    ao_detected_arr = np.zeros_like(log_like_arr)

    sizestr = '{}x{}x{}'.format(n_mass_grid_edges - 1, n_sma_grid_edges - 1,
                                n_injections_per_cell)
    outpath = ('../data/rv_simulations/mass_semimaj_loglikearr_{}.pickle'.
               format(sizestr))

    if not os.path.exists(outpath):

        for mass_upper, sma_upper in product(mass_grid[1:], sma_grid[1:]):

            mass_left_ind = np.argwhere(mass_grid == mass_upper) - 1
            sma_left_ind = np.argwhere(sma_grid == sma_upper) - 1

            mass_lower = mass_grid[mass_left_ind].squeeze()
            sma_lower = sma_grid[sma_left_ind].squeeze()

            pstr = ('{:s} {:.2f}, {:.2f}, {:.2f}, {:.2f}'.format(
                datetime.now().isoformat(), mass_lower, mass_upper, sma_lower,
                sma_upper))
            print(pstr)

            # sample the models from the chain
            rv_model_single_planet_and_linear_trend, chain_sample_params = (
                draw_models(setupfn,
                            rvfitdir,
                            chaindf,
                            rvtimes,
                            n_samples=n_injections_per_cell))

            # n_sample x n_rvs array of the "RV - 1 planet" model (leaving in the
            # linear trend).
            # note: if there were a curvature term, it wuld go in here too.
            full_resid = (rvs - rv_model_single_planet_and_linear_trend +
                          (nparr(chain_sample_params.dvdt)[:, None] *
                           (rvtimes[None, :] - time_base)))

            # draw (a_c, M_c, e_c) for each simulated companion
            mass_c = loguniform(low=np.log10(mass_lower.value),
                                high=np.log10(mass_upper.value),
                                size=n_injections_per_cell)
            sma_c = loguniform(low=np.log10(sma_lower.value),
                               high=np.log10(sma_upper.value),
                               size=n_injections_per_cell)
            cos_incl_c = np.random.uniform(0, 1, size=n_injections_per_cell)
            incl_c = np.rad2deg(np.arccos(cos_incl_c))

            ecc_c = np.zeros(n_injections_per_cell)

            # case: companion is a planet. use Kipping (2013).
            a, b = 1.12, 3.09
            _ecc_c_planetary = beta.rvs(a, b, size=n_injections_per_cell)

            # case: companion is a star. go for Moe & Di Stefano (2017), Eq 17.

            period_c = (((sma_c * u.AU)**(3) * 4 * np.pi**2 /
                         (const.G * (MSTAR + mass_c * u.Mjup)))**(1 / 2)).to(
                             u.day)

            # Moe & Di Stefano (2017), Eq 17.
            eta = 0.6 - 0.7 / (np.log10(period_c.to(u.day).value) - 0.5)

            # f(x,a) = ax^{a-1} for scipy's built-in powerlaw distribution.
            _ecc_c_stellar = powerlaw.rvs(eta + 1, size=n_injections_per_cell)

            # assign the eccentricties piece-wise at 10 Mjup.
            cutoff = 10  # Mjup
            ecc_c[mass_c <= cutoff] = _ecc_c_planetary[mass_c <= cutoff]
            ecc_c[mass_c > cutoff] = _ecc_c_stellar[mass_c > cutoff]

            #
            # do a max-likelihood fit for time and argument of periastron.
            #

            if multiprocess:
                tasks = [(pd.DataFrame({
                    'time': rvtimes,
                    'tel': telvec,
                    'mnvel': full_resid[ix, :],
                    'errvel': rverrs
                }), {
                    k: chain_sample_params.iloc[ix][k]
                    for k in chain_sample_params.columns
                    if 'gamma' in k or 'jit' in k
                }, mass_c[ix], sma_c[ix], incl_c[ix], ecc_c[ix])
                         for ix in range(n_injections_per_cell)]

                nworkers = mp.cpu_count()
                maxworkertasks = 1000
                pool = mp.Pool(nworkers, maxtasksperchild=maxworkertasks)

                result = pool.map(maxlike_worker, tasks)

                pool.close()
                pool.join()

                loglike_vals, ao_detected_vals = (nparr(result)[:, 0],
                                                  nparr(result)[:, 1])

            if not multiprocess:
                # serial approach
                loglike_vals, ao_detected_vals = [], []
                for ix in range(n_injections_per_cell):

                    data = pd.DataFrame({
                        'time': rvtimes,
                        'tel': telvec,
                        'mnvel': full_resid[ix, :],
                        'errvel': rverrs
                    })

                    gammajit_dict = {
                        k: chain_sample_params.iloc[ix][k]
                        for k in chain_sample_params.columns
                        if 'gamma' in k or 'jit' in k
                    }

                    post = initialize_sim_posterior(data, mass_c[ix] * u.Mjup,
                                                    sma_c[ix] * u.AU,
                                                    incl_c[ix], ecc_c[ix],
                                                    gammajit_dict)

                    post = radvel.fitting.maxlike_fitting(post, verbose=False)

                    ao_detected = get_AO_detectability(mass_c[ix], sma_c[ix],
                                                       incl_c[ix], ecc_c[ix],
                                                       post)

                    if verbose:
                        print(post.logprob())

                    loglike_vals.append(post.logprob())
                    ao_detected_vals.append(ao_detected)

            log_like_arr[mass_left_ind,
                         sma_left_ind, :] = (nparr(loglike_vals))

            ao_detected_arr[mass_left_ind,
                            sma_left_ind, :] = (nparr(ao_detected_vals))

            ostr = ('\t loglike: {:.1f}. ao_detected_frac: {:.2f}'.format(
                np.nanmean(nparr(loglike_vals)),
                np.nanmean(nparr(ao_detected_vals))))
            print(ostr)

        with open(outpath, 'wb') as outf:
            pickle.dump(log_like_arr, outf, protocol=pickle.HIGHEST_PROTOCOL)
        print('saved {}'.format(outpath))

        outpath = outpath.replace('loglikearr', 'aodetectedarr')
        with open(outpath, 'wb') as outf:
            pickle.dump(ao_detected_arr,
                        outf,
                        protocol=pickle.HIGHEST_PROTOCOL)
        print('saved {}'.format(outpath))

    else:

        log_like_arr = pickle.load(open(outpath, 'rb'))
        ao_detected_arr = pickle.load(
            open(outpath.replace('loglikearr', 'aodetectedarr'), 'rb'))

    #
    # Convert log-likelihood values to relative probability by taking the exp.
    # Then average out the "sample" dimension (mass, sma, eccentricity, etc).
    #
    rv_log_like = np.log(np.exp(log_like_arr).mean(axis=2))

    rv_and_ao_log_like = np.log(
        nparr(np.exp(log_like_arr) * (1 - ao_detected_arr)).mean(axis=2))

    # -2*logprob == chi^2
    # Convert likelihood values to a normalized probability via
    #   P ~ -exp(-chi^2/2)
    rv_prob_arr = np.exp(rv_log_like) / np.exp(rv_log_like).sum().sum()

    rv_and_ao_prob_arr = np.exp(rv_and_ao_log_like) / np.exp(
        rv_and_ao_log_like).sum().sum()

    return rv_prob_arr, rv_and_ao_prob_arr, mass_grid, sma_grid
예제 #26
0
def quad(size=1, lb=0., ub=1.):

    return powerlaw.rvs(3., loc=lb, scale=ub - lb, size=int(size))
예제 #27
0
 def _power_law_dist(self):
     return powerlaw.rvs(1.66, random_state=2)
def qdist(nsamples):
    #From Allen 2007
    gamma = 1.8
    randomq = powerlaw.rvs(gamma+1,size=nsamples)

    return randomq
예제 #29
0
def generateToy():

    np.random.seed(12345)

    fig, ax = plt.subplots(4, sharex=True)
    #fig,ax = plt.subplots(2)

    powerlaw_arg = 2
    triang_arg = 0.7
    n_samples = 500
    #generate simple line with slope 1, from 0 to 1
    frozen_powerlaw = powerlaw(
        powerlaw_arg)  #powerlaw.pdf(x, a) = a * x**(a-1)
    #generate triangle with peak at 0.7
    frozen_triangle = triang(
        triang_arg
    )  #up-sloping line from loc to (loc + c*scale) and then downsloping for (loc + c*scale) to (loc+scale).
    frozen_uniform = uniform(0.2, 0.5)
    frozen_uniform2 = uniform(0.3, 0.2)

    x = np.linspace(0, 1)

    signal = np.random.normal(0.5, 0.1, n_samples / 2)

    data_frame = pd.DataFrame({
        'powerlaw':
        powerlaw.rvs(powerlaw_arg, size=n_samples),
        'triangle':
        triang.rvs(triang_arg, size=n_samples),
        'uniform':
        np.concatenate((uniform.rvs(0.2, 0.5, size=n_samples / 2),
                        uniform.rvs(0.3, 0.2, size=n_samples / 2))),
        'powerlaw_signal':
        np.concatenate((powerlaw.rvs(powerlaw_arg,
                                     size=n_samples / 2), signal))
    })

    ax[0].plot(x, frozen_powerlaw.pdf(x), 'k-', lw=2, label='powerlaw pdf')
    hist(data_frame['powerlaw'],
         bins=100,
         normed=True,
         histtype='stepfilled',
         alpha=0.2,
         label='100 bins',
         ax=ax[0])
    #hist(data_frame['powerlaw'],bins='blocks',fitness='poly_events',normed=True,histtype='stepfilled',alpha=0.2,label='b blocks',ax=ax[0])
    ax[0].legend(loc='best')

    ax[1].plot(x, frozen_triangle.pdf(x), 'k-', lw=2, label='triangle pdf')
    hist(data_frame['triangle'],
         bins=100,
         normed=True,
         histtype='stepfilled',
         alpha=0.2,
         label='100 bins',
         ax=ax[1])
    hist(data_frame['triangle'],
         bins='blocks',
         fitness='poly_events',
         normed=True,
         histtype='stepfilled',
         alpha=0.2,
         label='b blocks',
         ax=ax[1])
    ax[1].legend(loc='best')

    #ax[0].plot(x, frozen_powerlaw.pdf(x), 'k-', lw=2, label='powerlaw pdf')
    hist(data_frame['powerlaw_signal'],
         bins=100,
         normed=True,
         histtype='stepfilled',
         alpha=0.2,
         label='100 bins',
         ax=ax[2])
    #hist(data_frame['powerlaw_signal'],bins='blocks',normed=True,histtype='stepfilled',alpha=0.2,label='b blocks',ax=ax[2])
    ax[2].legend(loc='best')

    ax[3].plot(x,
               frozen_uniform.pdf(x) + frozen_uniform2.pdf(x),
               'k-',
               lw=2,
               label='uniform pdf')
    hist(data_frame['uniform'],
         bins=100,
         normed=True,
         histtype='stepfilled',
         alpha=0.2,
         label='100 bins',
         ax=ax[3])
    #hist(data_frame['uniform'],bins='blocks',fitness = 'poly_events',p0=0.05,normed=True,histtype='stepfilled',alpha=0.2,label='b blocks',ax=ax[3])
    ax[3].legend(loc='best')

    plt.show()
    fig.savefig('plots/toy_plots.png')
예제 #30
0
#100*powerlaw.rvs(a = 1.42)
 #lognorm.rvs( s = 1.06 , scale  = math.exp(4.3079 )
theta = [lambda: expon.rvs(scale = 1 / 0.013),            #lambda=0.013
         lambda: 2 * lognorm.rvs(s = 1.27, scale  = math.exp(3.62)), 
         lambda: 3.1 * lognorm.rvs(scale = 23.97,s = 1.83,loc = 1), 
         lambda: 10 * lognorm.rvs(s = 1.32, scale = math.exp(4.54))]              #alpha=1.42
birth_rate =  [1.81,0.2,0.0,0.0]
mortal_rate = [0.0042,0.0008,0.0001,1]  

min_bch_size = [lambda: lognorm.rvs(s=1.44,scale=math.exp(3.23)), 
                lambda: lognorm.rvs(s=0.89,scale=math.exp(4.07)),
                lambda: expon.rvs(scale =1/0.0078 ) ]  
  
loyalty = [lambda: beta.rvs(0.83, 0.7),
           lambda: beta.rvs(1.54, 0.67),
           lambda: powerlaw.rvs(0.54)]       

# T is a set of possible transactions
T = {(0,1,60),(1,2,120),(2,3,0)}
barnlist = []
barn_index = {}  # dict of pairs (start_index, end_index) giving the index ranges of barns of each type
breeding_no=47894
fattening_no=31628
trader_no=15631
slaughterh_no=2755
class Barn:
    """
    describe the attributes and methods for each barn
    """

    def __init__(self, Barn_id, stage_type, capacity, destination, gis, Dlist): 
예제 #31
0
def graph_gen(self_con, other_con, nodes=100, groups=10):
    corr = lambda x, y: self_con if x == y else other_con
    g, bm = random_graph(nodes, lambda: (powerlaw.rvs(2.4, size=1) * -1 + 1) * 20, directed=False, model="blockmodel-traditional", block_membership=lambda: np.random.randint(int(groups)), vertex_corr=corr)
    # poisson(10).rvs(1)
    return g, bm