n/qx : Probability to die in n years being alive at age x.
    Probability that x survives n year, and then dies in th subsequent t years
509895
def tqxn(mt, x, n, t):
    
    return tpx(mt, x, t) * qx(mt, x + n)
40423
function invIncBeta(p, a, b) {
  if(x < 0 || x > 1) {
    throw new RangeError('First argument must be between 0 and 1.');
	}

  // Special cases, there can make trouble otherwise
  else if (a === 1 && b === 1) return p;
  else if (p === 1) return 1;
  else if (p === 0) return 0;
  else if (a === 0) return 0;
  else if (b === 0) return 1;

  else {
    var EPS = 1e-8,
        a1 = a - 1,
        b1 = b - 1,
        j = 0,
        lna, lnb, pp, t, u, err, x, al, h, w, afac;

	if(a >= 1 && b >= 1) {
    pp = (p < 0.5) ? p : 1 - p;
    t = Math.sqrt(-2 * Math.log(pp));

		x = (2.30753 + t * 0.27061) / (1 + t* (0.99229 + t * 0.04481)) - t;
		if(p < 0.5) x = -x;
		al = (x * x - 3) / 6;
		h = 2 / (1 / (2 * a - 1)  + 1 / (2 * b - 1));
		w = (x * Math.sqrt(al + h) / h) - (1 / (2 * b - 1) - 1 / (2 * a - 1)) * (al + 5 / 6 - 2 / (3 * h));
		x = a / (a + b * Math.exp(2 * w));
	} else {
		lna = Math.log(a / (a + b));
		lnb = Math.log(b / (a + b));
		t = Math.exp(a * lna) / a;
		u = Math.exp(b * lnb) / b;
		w = t + u;
		if (p < t / w) x = Math.pow(a * w * p, 1 / a);
		else x = 1 - Math.pow(b * w * (1 - p), 1 / b);
	}

	afac = -gammaCollection.logGamma(a) - gammaCollection.logGamma(b) + gammaCollection.logGamma(a + b);

  for(; j < 10; j++) {
		if(x === 0 || x === 1) return x;
		err = regualizedBeta(x, a, b) - p;

    t = Math.exp(a1 * Math.log(x) + b1 * log1p(-x) + afac);
		u = err / t;
		x -= (t = u / (1 - 0.5 * Math.min(1, u * (a1 / x - b1 / (1 - x)))));

    if (x <= 0) x = 0.5 * (x + t);
		if (x >= 1) x = 0.5 * (x + t + 1);

		if (Math.abs(t) < EPS * x && j > 0) break;
	}

	return x;
  }
}
956609
def competence(stochastic):
        
        if stochastic.dtype in bool_dtypes:
            return 2

        elif isinstance(stochastic, distributions.Bernoulli):
            return 2
            
        elif (isinstance(stochastic, distributions.Categorical) and 
            (len(stochastic.parents['p'])==2)):
            return 2

        else:
            return 0
457364
def somenan(self, x, fun, p=0.1):
        """"""
        if np.random.rand(1) < p:
            return np.NaN
        else:
            return fun(x)
551194
def cumulative_distribution(self, X):
        
        self.check_fit()

        # Wrapper for pdf to accept vector as args
        def func(*args):
            return self.probability_density(list(args))

        # Lower bound for integral, to split significant part from tail
        lower_bound = self.get_lower_bound()

        ranges = [[lower_bound, val] for val in X]
        return integrate.nquad(func, ranges)[0]
791962
public void doMutation(double probability, DoubleSolution solution)  {
    for (int i = 0; i < solution.getNumberOfVariables(); i++) {
      if (randomGenenerator.getRandomValue() < probability) {
        double rand = randomGenenerator.getRandomValue();
        double tmp = (rand - 0.5) * perturbation;

        tmp += solution.getVariableValue(i);

        if (tmp < solution.getLowerBound(i)) {
          tmp = solution.getLowerBound(i);
        } else if (tmp > solution.getUpperBound(i)) {
          tmp = solution.getUpperBound(i);
        }

        solution.setVariableValue(i, tmp);
      }
    }
  }
347475
def survival(self,parents,offspring,elite=None,elite_index=None,X=None,X_O=None,F=None,F_O=None):
        """"""
        if self.sel == 'tournament':
            survivors, survivor_index = self.tournament(parents + offspring, self.tourn_size, num_selections = len(parents))
        elif self.sel == 'lexicase':
            survivor_index = self.lexicase(np.vstack((F,F_O)), num_selections = len(parents), survival = True)
            survivors = [(parents+ offspring)[s] for s in survivor_index]
        elif self.sel == 'epsilon_lexicase':
            # survivors, survivor_index = self.epsilon_lexicase(parents + offspring, num_selections = len(parents), survival = True)
            if self.lex_size:
                sizes = [len(i.stack) for i in (parents + offspring)]
                survivor_index = self.epsilon_lexicase(np.vstack((F,F_O)), sizes, num_selections = F.shape[0], survival = True)
                survivors = [(parents+ offspring)[s] for s in survivor_index]
            else:
                survivor_index = self.epsilon_lexicase(np.vstack((F,F_O)), [], num_selections = F.shape[0], survival = True)
                survivors = [(parents+ offspring)[s] for s in survivor_index]
        elif self.sel == 'deterministic_crowding':
            survivors, survivor_index = self.deterministic_crowding(parents,offspring,X,X_O)
        elif self.sel == 'random':
            # pdb.set_trace()
            survivor_index = self.random_state.permutation(np.arange(2*len(parents)))[:len(parents)]
            survivors = [(parents + offspring)[s] for s in survivor_index]
        # elitism
        if self.elitism:
            if min([x.fitness for x in survivors]) > elite.fitness:
                # if the elite individual did not survive and elitism is on, replace worst individual with elite
                rep_index = np.argmax([x.fitness for x in survivors])
                survivors[rep_index] = elite
                survivor_index[rep_index] = elite_index
        # return survivors

        return survivors,survivor_index
594996
def evolve(self, new_date):
        
        if self.state.date == new_date and not self.initial_state.date == new_date:
            return self.state
        if self._len:
            q = [self.random.gauss(0., 1.) for _ in range(int(self._len))]
        else:
            q = self.random.gauss(0., 1.)
        self.state.value = self.func(self.state.value, self.state.date, new_date, q)
        self.state.date = new_date
        return self.state
1081289
def life_range
      if birth
        if death
          birth..death # exact
        else
          birth..(birth + max_le) # estimation based on birth
        end
      elsif death
        (death - max_le)..death # estimation based on death
      end
    end
29314
def year(self, minimum: int = 1990, maximum: int = 2050) -> int:
        
        return self.random.randint(minimum, maximum)
1012040
def choose(n, k):
    
    if 0 <= k <= n:
        ntok = 1
        ktok = 1
        for t in xrange(1, min(k, n - k) + 1):
            ntok *= n
            ktok *= t
            n -= 1
        return ntok // ktok
    else:
        return 0
89142
def qx(mt, x):
    
    if x < len(mt.qx):
        return mt.qx[x]
    else:
        return 0
309413
def Likelihood(self, data):
        
        m = len(data)
        if self.n < m:
            return 0

        x = data
        p = self.Random()
        q = p[:m] ** x
        return q.prod()
854337
def probability_lt(self, x):
        
        if self.mean is None:
            return
        return normdist(x=x, mu=self.mean, sigma=self.standard_deviation)
1117284
def qstd(x,quant=0.05,top=False,bottom=False):
    
    s = np.sort(x)
    n = np.size(x)
    lo = s[int(n*quant)]
    hi = s[int(n*(1-quant))]
    if top:
        w = np.where(x>=lo)
    elif bottom:
        w = np.where(x<=hi)
    else:
        w = np.where((x>=lo)&(x<=hi))
    return np.std(x[w])
400742
def formula(person, period, parameters):
        '''
        
        '''
        age_condition = person('age', period) >= parameters(period).general.age_of_retirement
        return age_condition
1219105
def roulette_selection(population, fitnesses):
    """"""
    probabilities = _fitnesses_to_probabilities(fitnesses)

    intermediate_population = []
    for _ in range(len(population)):
        # Choose a random individual
        selection = random.uniform(0.0, 1.0)
        # Iterate over probabilities list
        for i, probability in enumerate(probabilities):
            if probability >= selection:  # First probability that is greater
                intermediate_population.append(population[i])
                break

    return intermediate_population