Esempio n. 1
0
def experiment_2(n_trials, N, n_samples_range):
    distance_array = np.empty((n_samples_range + 1, ))
    TRUE_VAR = 1

    for n in range(2, n_samples_range + 1):
        print('Count epoch #: ', n)
        total_var, total_var_bag = np.empty(n_trials, ), np.empty(n_trials, )
        for i in range(n_trials):
            if i % 1000 == 0:
                print('Count trial #: ', i)
            # x = np.random.normal(0, 1, size=n)
            # x = np.random.uniform(-1, 1, size=n)
            x = semicircular.rvs(size=n)
            # rademacher
            # x = 2 * bernouilli - 1

            # # Estimator MSE var
            var_x = (len(x) / (len(x) - 1)) * np.var(x)
            total_var[i] = var_x

            # Estimator MSE var avec bagging
            mean_bagg = np.empty(N, )
            for j in range(N):
                bag = bagging_np(x)
                var_x_bag = (len(bag) / (len(bag) - 1)) * np.var(bag)
                mean_bagg[j] = var_x_bag
            var_bag = np.mean(mean_bagg)
            total_var_bag[i] = var_bag

        EXP_var = np.mean(total_var)
        EXP_var_bag = np.mean(total_var_bag)

        BIAS_sq_var = (EXP_var - TRUE_VAR)**2
        BIAS_sq_var_bag = (EXP_var_bag - TRUE_VAR)**2

        VAR_VAR_bag = (len(total_var_bag) /
                       (len(total_var_bag) - 1)) * np.var(total_var_bag)
        VAR_VAR = (len(total_var) / (len(total_var) - 1)) * np.var(total_var)

        MSE_VAR = VAR_VAR + BIAS_sq_var
        MSE_VAR_BAG = VAR_VAR_bag + BIAS_sq_var_bag

        DIST = (MSE_VAR_BAG - MSE_VAR)

        distance_array[n] = DIST
    return distance_array
Esempio n. 2
0
def mutation(pop, U, n, mut):
    """
	This function creates mutations
	"""
    ###############
    # max mutations a poisson number (U is expected number) - MUCH SLOWER!
    ###############
    # nnewmuts = np.random.poisson(U, len(pop)) #poisson number of new mutations for each individual
    # oldmuts = np.sum(pop, axis=1) - 1 #number of mutations (not counting origin mutation) already in each individual (from ancestors)
    # totmuts = nnewmuts + oldmuts #total number of mutations in each individual
    # rtotmuts = np.clip(totmuts, 0, mutmax) #realized total number of mutations: allow each individual to have some max number of mutations
    # rnnewmuts = np.clip(rtotmuts - oldmuts, 0, None) #realized number of new mutations
    # newmutpop = np.repeat(np.transpose(np.identity(len(pop), dtype=int)), rnnewmuts, axis=1) #columns for new mutations in pop
    # pop = np.append(pop, newmutpop, axis=1) #append new columns to population matrix
    # newmuts = np.random.multivariate_normal(mean_mut, cov_mut, sum(rnnewmuts)) #phenotypic effect of new mutations
    # mut = np.append(mut, newmuts, axis=0) #append effect of new mutations to mutation matrix
    ###############
    # max one new mutation per individual (U is a probability) - MUCH FASTER!
    ###############
    rand3 = np.random.uniform(size=len(
        pop))  #random uniform number in [0,1] for each potential mutant
    nmuts = sum(
        rand3 < U
    )  # mutate if random number is below mutation probability; returns number of new mutations
    whomuts = np.where(rand3 < U)  #indices of mutants
    newmuts = semicircular.rvs(loc=0,
                               scale=(4 * l)**0.5, size=n * nmuts).reshape(
                                   nmuts,
                                   n)  #phenotypic effect of new mutations
    pop = np.append(pop,
                    np.transpose(np.identity(len(pop), dtype=int)[whomuts[0]]),
                    axis=1)  #add new loci and identify mutants
    mut = np.append(mut, newmuts,
                    axis=0)  #append effect of new mutations to mutation list
    ###############
    return [pop, mut]
        semicircular.pdf(x),
        'r-',
        lw=5,
        alpha=0.6,
        label='semicircular 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 = semicircular()
ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')

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

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

# Generate random numbers:

r = semicircular.rvs(size=1000)

# And compare the histogram:

ax.hist(r, density=True, histtype='stepfilled', alpha=0.2)
ax.legend(loc='best', frameon=False)
plt.show()
Esempio n. 4
0
plt.title("Experiment 4: Histogram for sums of generated random variables")
plt.hist(samples4, 100, density=True)
# initializations for theoretical normal distribution curve
mu = np.mean(samples4)
sigma = np.std(samples4)
x = np.linspace(mu - 6*sigma, mu + 6*sigma, 200)
plt.plot(x, stats.norm.pdf(x, mu, sigma))
plt.show()


# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

# Experiment 5:

s = semicircular.rvs(2, 1, size=50000)  # 50000 randomly generated values from a distribution of semi-circle of radius 1 centered at 2.

samples5 = [np.sum(random.choices(s, k=2)) for i in range(50000)]  # Number of values is 2.

print("Experiment 5: ")
print("Sample mean is ", np.mean(samples5))
print("Sample standard deviation is ", np.std(samples5))
plt.figure()
plt.title("Experiment 5: Histogram for generated random variables")
plt.hist(s, 100, density=True)
plt.show()
plt.title("Experiment 5: Histogram for sums of generated random variables")
plt.hist(samples5, 100, density=True)
# initializations for theoretical normal distribution curve
mu = np.mean(samples5)
sigma = np.std(samples5)
plt.title("Histogram for generated random variables")
plt.hist(bigs, 70, cumulative=0, density=True)
plt.show()
plt.figure()
plt.title("Histogram for sums of generated random variables")
plt.hist(total, 70, cumulative=0, density=True)
mu = np.mean(total)
sigma = np.std(total)
x = np.linspace(mu - 8 * sigma, mu + 8 * sigma, 4000)
plt.plot(x, stats.norm.pdf(x, mu, sigma))
plt.show()
print("\n--------------------------\n")

#Experiment 5-6-7
for values in LOV:
    s = semicircular.rvs(2, 1, 10000)
    total = []
    for i in range(4000):
        total.append(sum(random.choices(s, k=values)))
    print("Sample mean is:", np.mean(total))
    print("Sample standart deviation is:", np.std(total))
    plt.figure()
    plt.title("Histogram for generated random variables")
    plt.hist(s, 70, cumulative=0, density=True)
    plt.show()
    plt.figure()
    plt.title("Histogram for sums of generated random variables")
    plt.hist(total, 50, cumulative=0, density=True)
    mu = np.mean(total)
    sigma = np.std(total)
    x = np.linspace(mu - 8 * sigma, mu + 8 * sigma, 4000)
Esempio n. 6
0
import math
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import bernoulli
from scipy.stats import norm
from scipy.stats import binom
from scipy.stats import semicircular

#number of test cases for each variable
k = 10000

#counter for counting the number of times favourable situation occurs
count = 0
#creating an array of x coordinate using semicircular function
data_x = semicircular.rvs(loc=0, scale=1, size=k, random_state=None)

#creating an array of x coordinate using semicircular function
data_y = semicircular.rvs(loc=0, scale=1, size=k, random_state=None)

#comparing each term of both arrays and counting number of times the favourable situation occurs
for i in range(k):
    for j in range(k):
        #print(" " + str(data_y[j]) + ":" + str(data_x[i]))
        if (data_y[j] > abs(data_x[i])):
            count = count + 1

#probability of the event is number of times the situation occured / total test cases
# total test cases is k^2
probab = count / pow(k, 2)
print("The probability is: ", probab)