Beispiel #1
0
def create_datasets():
    rs = RandomState(0)
    data_a = np.sort(rs.normal(0, 10, 500)).astype(int).reshape(100, 5)
    gene_names_a = list("ABCDE")
    cell_types_a = ["alpha", "beta", "gamma", "delta"]
    labels_a = rs.choice(np.arange(len(cell_types_a)), data_a.shape[0])
    batch_indices_a = np.random.choice(np.arange(5), size=data_a.shape[0])

    data_b = np.sort(rs.normal(100, 10, 300)).astype(int).reshape(100, 3)
    gene_names_b = list("BFA")
    cell_types_b = ["alpha", "epsilon", "rho"]
    labels_b = rs.choice(np.arange(len(cell_types_b)), data_b.shape[0])
    batch_indices_b = rs.choice(np.arange(5), size=data_b.shape[0])

    dataset_a = GeneExpressionDataset()
    dataset_b = GeneExpressionDataset()
    dataset_a.populate_from_data(X=data_a,
                                 labels=labels_a,
                                 gene_names=gene_names_a,
                                 cell_types=cell_types_a,
                                 batch_indices=batch_indices_a)
    dataset_a.name = "test_a"

    dataset_b.populate_from_data(X=data_b,
                                 labels=labels_b,
                                 gene_names=gene_names_b,
                                 cell_types=cell_types_b,
                                 batch_indices=batch_indices_b)
    dataset_b.name = "test_b"
    return dataset_a, dataset_b
Beispiel #2
0
def test_mutual_information(seed):
    num_data_points = 500

    rs = RandomState(seed)
    mi_random_uniform = compute_mutual_information(
        rs.uniform(-1, 1, size=(2, num_data_points)))

    rs = RandomState(seed)
    mi_random_gaussian = compute_mutual_information(
        rs.normal(size=(2, num_data_points)))

    rs = RandomState(seed)
    mi_random_gaussian = compute_mutual_information(
        rs.normal(size=(2, num_data_points)))

    rs = RandomState(seed)
    mi_constant = compute_mutual_information(
        np.ones((2, num_data_points)) +
        rs.normal(0, 1e-15, size=(2, num_data_points)))

    rs = RandomState(seed)
    mi_correlated = compute_mutual_information(
        generate_correlated_data(num_data_points, cov=0.9, rs=rs))
    print('MI random uniform:', mi_random_uniform)
    print('MI random gaussian:', mi_random_gaussian)
    print('MI constant:', mi_constant)
    print('MI correlated:', mi_correlated)
Beispiel #3
0
class WeightGenerationRegime:
    def __init__(self, nStates, nBivariateFeat, prng=None, seed=None, stationaryWeights=None, bivariateWeights=None):
        self.nStates = nStates
        self.nBivariateFeat = nBivariateFeat
        if prng is not None:
            self.prng = prng
        else:
            if seed is not None:
                self.prng = RandomState(seed)
            else:
                self.defaultRandomSeed = defaultSeed
                prng = RandomState(1234567890)

        if prng is not None and seed is not None:
            warnings.warn("both prng and seed are provided but we use the provided prng", DeprecationWarning)

        self.stationaryWeights = stationaryWeights
        self.bivariateWeights = bivariateWeights


    def generateStationaryWeightsFromNormal(self):
        self.stationaryWeights = self.prng.normal(0, 1, self.nStates)
        return self.stationaryWeights

    def generateBivariateWeightsFromNormal(self):
        self.bivariateWeights = self.prng.normal(0, 1, self.nBivariateFeat)
        return self.bivariateWeights

    def generateStationaryWeightsFromUniform(self):
        self.stationaryWeights = self.prng.uniform(0, 1, self.nStates)
        return self.stationaryWeights

    def generateBivariateWeightsFromUniform(self):
        self.bivariateWeights = self.prng.uniform(0, 1, self.nBivariateFeat)
        return self.bivariateWeights
Beispiel #4
0
def test_two_sample():
    prng = RandomState(42)

    x = prng.normal(1, size=20)
    y = prng.normal(4, size=20)
    res = two_sample(x, y, seed=42)
    expected = (1.0, -2.90532344604777)
    np.testing.assert_almost_equal(res, expected)

    y = prng.normal(1.4, size=20)
    res = two_sample(x, y, seed=42)
    res2 = two_sample(x, y, seed=42, keep_dist=True)
    expected = (0.96975, -0.54460818906623765)
    np.testing.assert_equal(res, expected)
    np.testing.assert_equal(res2[:2], expected)

    y = prng.normal(1, size=20)
    res = two_sample(x, y, seed=42)
    expected = (0.66505000000000003, -0.13990200413154097)
    np.testing.assert_equal(res, expected)

    res = two_sample(x, y, seed=42, interval="upper")
    expected_pv = 0.66505000000000003
    expected_ts = -0.13990200413154097
    expected_ci = (0.0, 0.6675064023707297)
    np.testing.assert_equal(res[0], expected_pv)
    np.testing.assert_equal(res[1], expected_ts)
    np.testing.assert_almost_equal(res[2], expected_ci)

    res = two_sample(x, y, seed=42, interval="lower")
    expected_ci = (0.6625865251964975, 1.0)
    np.testing.assert_almost_equal(res[2], expected_ci)
    res = two_sample(x, y, seed=42, interval="two-sided")
    expected_ci = (0.6621149803107692, 0.6679754440683887)
    np.testing.assert_almost_equal(res[2], expected_ci)

    res = two_sample(x, y, seed=42, keep_dist=True)
    exp_dist_firstfive = [0.089396492796047111,
                          0.17390295863272254,
                         -0.034211921065956274,
                          0.29103960535095719,
                         -0.76420778601368644]
    np.testing.assert_equal(res[0], expected_pv)
    np.testing.assert_equal(res[1], expected_ts)
    np.testing.assert_equal(len(res[2]), 100000)
    np.testing.assert_almost_equal(res[2][:5], exp_dist_firstfive)

    res = two_sample(x, y, seed=42, interval="two-sided", keep_dist=True)
    np.testing.assert_equal(res[0], expected_pv)
    np.testing.assert_equal(res[1], expected_ts)
    np.testing.assert_almost_equal(res[2], expected_ci)
    np.testing.assert_equal(len(res[3]), 100000)
    np.testing.assert_almost_equal(res[3][:5], exp_dist_firstfive)
Beispiel #5
0
def test_two_sample():
    prng = RandomState(42)

    x = prng.normal(1, size=20)
    y = prng.normal(4, size=20)
    res = two_sample(x, y, seed=42)
    expected = (1.0, -2.90532344604777)
    np.testing.assert_almost_equal(res, expected)

    y = prng.normal(1.4, size=20)
    res = two_sample(x, y, seed=42)
    res2 = two_sample(x, y, seed=42, keep_dist=True)
    expected = (0.96975, -0.54460818906623765)
    np.testing.assert_equal(res, expected)
    np.testing.assert_equal(res2[:2], expected)

    y = prng.normal(1, size=20)
    res = two_sample(x, y, seed=42)
    expected = (0.66505000000000003, -0.13990200413154097)
    np.testing.assert_equal(res, expected)

    res = two_sample(x, y, seed=42, interval="upper")
    expected_pv = 0.66505000000000003
    expected_ts = -0.13990200413154097
    expected_ci = (0.0, 0.6675064023707297)
    np.testing.assert_equal(res[0], expected_pv)
    np.testing.assert_equal(res[1], expected_ts)
    np.testing.assert_almost_equal(res[2], expected_ci)

    res = two_sample(x, y, seed=42, interval="lower")
    expected_ci = (0.6625865251964975, 1.0)
    np.testing.assert_almost_equal(res[2], expected_ci)
    res = two_sample(x, y, seed=42, interval="two-sided")
    expected_ci = (0.6621149803107692, 0.6679754440683887)
    np.testing.assert_almost_equal(res[2], expected_ci)

    res = two_sample(x, y, seed=42, keep_dist=True)
    exp_dist_firstfive = [
        0.089396492796047111, 0.17390295863272254, -0.034211921065956274,
        0.29103960535095719, -0.76420778601368644
    ]
    np.testing.assert_equal(res[0], expected_pv)
    np.testing.assert_equal(res[1], expected_ts)
    np.testing.assert_equal(len(res[2]), 100000)
    np.testing.assert_almost_equal(res[2][:5], exp_dist_firstfive)

    res = two_sample(x, y, seed=42, interval="two-sided", keep_dist=True)
    np.testing.assert_equal(res[0], expected_pv)
    np.testing.assert_equal(res[1], expected_ts)
    np.testing.assert_almost_equal(res[2], expected_ci)
    np.testing.assert_equal(len(res[3]), 100000)
    np.testing.assert_almost_equal(res[3][:5], exp_dist_firstfive)
Beispiel #6
0
    def setUpClass(self):
        self._N = 10
        self._D = 2

        from numpy.random import RandomState
        randomstate = RandomState(621360)

        self._X = randomstate.normal(0, 1, (self._N, self._D))
        self._G0 = randomstate.normal(0, 1, (self._N, 5))
        self._G1 = randomstate.normal(0, 1, (self._N, 6))
        y = randomstate.rand(self._N)
        y[y<=0.5] = -1.0
        y[y>0.5] = 1.0
        self._y = y
Beispiel #7
0
    def setUpClass(self):
        self._N = 10
        self._D = 2

        from numpy.random import RandomState
        randomstate = RandomState(621360)

        self._X = randomstate.normal(0, 1, (self._N, self._D))
        self._G0 = randomstate.normal(0, 1, (self._N, 5))
        self._G1 = randomstate.normal(0, 1, (self._N, 6))
        y = randomstate.rand(self._N)
        y[y <= 0.5] = -1.0
        y[y > 0.5] = 1.0
        self._y = y
Beispiel #8
0
def test_two_sample():
    prng = RandomState(42)

    # Normal-normal, different means examples
    x = prng.normal(1, size=20)
    y = prng.normal(4, size=20)
    res = two_sample(x, y, seed=42)
    expected = (1.0, -2.90532344604777)
    assert_almost_equal(res, expected, 5)
    res = two_sample(x, y, seed=42, plus1=False)
    assert_almost_equal(res, expected)

    # This one has keep_dist = True
    y = prng.normal(1.4, size=20)
    res = two_sample(x, y, seed=42)
    res2 = two_sample(x, y, seed=42, keep_dist=True)
    expected = (0.96975, -0.54460818906623765)
    assert_almost_equal(res[0], expected[0], 2)
    assert_equal(res[1], expected[1])
    assert_almost_equal(res2[0], expected[0], 2)
    assert_equal(res2[1], expected[1])

    # Normal-normal, same means
    y = prng.normal(1, size=20)
    res = two_sample(x, y, seed=42)
    expected = (0.66505000000000003, -0.13990200413154097)
    assert_almost_equal(res[0], expected[0], 2)
    assert_equal(res[1], expected[1])

    # Check the permutation distribution
    res = two_sample(x, y, seed=42, keep_dist=True)
    expected_pv = 0.66505000000000003
    expected_ts = -0.13990200413154097
    exp_dist_firstfive = [
        -0.1312181, 0.1289127, -0.3936627, -0.1439892, 0.7477683
    ]
    assert_almost_equal(res[0], expected_pv, 2)
    assert_equal(res[1], expected_ts)
    assert_equal(len(res[2]), 100000)
    assert_almost_equal(res[2][:5], exp_dist_firstfive)

    # Define a lambda function (K-S test)
    f = lambda u, v: np.max([
        abs(sum(u <= val) / len(u) - sum(v <= val) / len(v))
        for val in np.concatenate([u, v])
    ])
    res = two_sample(x, y, seed=42, stat=f, reps=100, plus1=False)
    expected = (0.62, 0.20000000000000007)
    assert_equal(res[0], expected[0])
    assert_equal(res[1], expected[1])
Beispiel #9
0
def test_two_sample():
    prng = RandomState(42)

    # Normal-normal, different means examples
    x = prng.normal(1, size=20)
    y = prng.normal(4, size=20)
    res = two_sample(x, y, seed=42)
    expected = (1.0, -2.90532344604777)
    np.testing.assert_almost_equal(res, expected)
    
    # This one has keep_dist = True
    y = prng.normal(1.4, size=20)
    res = two_sample(x, y, seed=42)
    res2 = two_sample(x, y, seed=42, keep_dist=True)
    expected = (0.96975, -0.54460818906623765)
    np.testing.assert_approx_equal(res[0], expected[0], 2)
    np.testing.assert_equal(res[1], expected[1])
    np.testing.assert_approx_equal(res2[0], expected[0], 2)
    np.testing.assert_equal(res2[1], expected[1])

    # Normal-normal, same means
    y = prng.normal(1, size=20)
    res = two_sample(x, y, seed=42)
    expected = (0.66505000000000003, -0.13990200413154097)
    np.testing.assert_approx_equal(res[0], expected[0], 2)
    np.testing.assert_equal(res[1], expected[1])

    # Check the permutation distribution
    res = two_sample(x, y, seed=42, keep_dist=True)
    expected_pv = 0.66505000000000003
    expected_ts = -0.13990200413154097
    exp_dist_firstfive = [0.08939649,
                         -0.26323896,
                         0.15428355,
                         -0.0294264,
                         0.03318078]
    np.testing.assert_approx_equal(res[0], expected_pv, 2)
    np.testing.assert_equal(res[1], expected_ts)
    np.testing.assert_equal(len(res[2]), 100000)
    np.testing.assert_almost_equal(res[2][:5], exp_dist_firstfive)

    # Define a lambda function (K-S test)
    f = lambda u,v: np.max( \
        [abs(sum(u<=val)/len(u)-sum(v<=val)/len(v)) \
        for val in np.concatenate([u,v])])
    res = two_sample(x, y, seed=42, stat=f, reps=100)
    expected = (0.68, 0.20000000000000007)
    np.testing.assert_equal(res[0], expected[0])
    np.testing.assert_equal(res[1], expected[1])
Beispiel #10
0
def test_two_sample():
    prng = RandomState(42)

    # Normal-normal, different means examples
    x = prng.normal(1, size=20)
    y = prng.normal(4, size=20)
    res = two_sample(x, y, seed=42)
    expected = (1.0, -2.90532344604777)
    np.testing.assert_almost_equal(res, expected)

    # This one has keep_dist = True
    y = prng.normal(1.4, size=20)
    res = two_sample(x, y, seed=42)
    res2 = two_sample(x, y, seed=42, keep_dist=True)
    expected = (0.96975, -0.54460818906623765)
    np.testing.assert_approx_equal(res[0], expected[0], 2)
    np.testing.assert_equal(res[1], expected[1])
    np.testing.assert_approx_equal(res2[0], expected[0], 2)
    np.testing.assert_equal(res2[1], expected[1])

    # Normal-normal, same means
    y = prng.normal(1, size=20)
    res = two_sample(x, y, seed=42)
    expected = (0.66505000000000003, -0.13990200413154097)
    np.testing.assert_approx_equal(res[0], expected[0], 2)
    np.testing.assert_equal(res[1], expected[1])

    # Check the permutation distribution
    res = two_sample(x, y, seed=42, keep_dist=True)
    expected_pv = 0.66505000000000003
    expected_ts = -0.13990200413154097
    exp_dist_firstfive = [
        0.08939649, -0.26323896, 0.15428355, -0.0294264, 0.03318078
    ]
    np.testing.assert_approx_equal(res[0], expected_pv, 2)
    np.testing.assert_equal(res[1], expected_ts)
    np.testing.assert_equal(len(res[2]), 100000)
    np.testing.assert_almost_equal(res[2][:5], exp_dist_firstfive)

    # Define a lambda function (K-S test)
    f = lambda u, v: np.max([
        abs(sum(u <= val) / len(u) - sum(v <= val) / len(v))
        for val in np.concatenate([u, v])
    ])
    res = two_sample(x, y, seed=42, stat=f, reps=100)
    expected = (0.68, 0.20000000000000007)
    np.testing.assert_equal(res[0], expected[0])
    np.testing.assert_equal(res[1], expected[1])
Beispiel #11
0
def fake_particle_ds(
        fields=("particle_position_x", "particle_position_y",
                "particle_position_z", "particle_mass", "particle_velocity_x",
                "particle_velocity_y", "particle_velocity_z"),
        units=('cm', 'cm', 'cm', 'g', 'cm/s', 'cm/s', 'cm/s'),
        negative=(False, False, False, False, True, True, True),
        npart=16**3,
        length_unit=1.0,
        data=None):
    from yt.frontends.stream.api import load_particles

    prng = RandomState(0x4d3d3d3)
    if not iterable(negative):
        negative = [negative for f in fields]
    assert (len(fields) == len(negative))
    offsets = []
    for n in negative:
        if n:
            offsets.append(0.5)
        else:
            offsets.append(0.0)
    data = {}
    for field, offset, u in zip(fields, offsets, units):
        if field in data:
            v = data[field]
            continue
        if "position" in field:
            v = prng.normal(loc=0.5, scale=0.25, size=npart)
            np.clip(v, 0.0, 1.0, v)
        v = (prng.random_sample(npart) - offset)
        data[field] = (v, u)
    bbox = np.array([[0.0, 1.0], [0.0, 1.0], [0.0, 1.0]])
    ds = load_particles(data, 1.0, bbox=bbox)
    return ds
Beispiel #12
0
def setupSeed(hoursBetweenTimestepInROMSFiles,startTime,endTime,startSpawningTime,endSpawningTime,releaseParticles):
    ##################################################
    # Create seed variation as function of day
    ##################################################

    # Make datetime array from start to end at 3 hour interval
    difference=endTime-startTime
    hoursOfSimulation=divmod(difference.total_seconds(), 3600)
    difference=endSpawningTime-startSpawningTime
    hoursOfSpawning=divmod(difference.total_seconds(), 3600)
    timeStepsSimulation=int(int(hoursOfSimulation[0])/hoursBetweenTimestepInROMSFiles)
		
    print "\nKINO TIME EVOLUTION:"
    print "=>SIMULATION: Drift simulation will run for %s simulation hours" %(timeStepsSimulation)
    print "=>SPAWNING: Simulated spawning will run for %s simulation hours\n initiated on %s and ending on %s"%(timeStepsSimulation,startSpawningTime,endSpawningTime)

    interval = timedelta(hours=24)
    hoursPerSpawning=divmod(interval.total_seconds(), 3600) #hours per spawning event
    timeStepsSpawning=int(int(hoursOfSpawning[0])/int(hoursPerSpawning[0])) #number of spawning timesteps
    spawningTimes = [startSpawningTime + interval*n for n in range(timeStepsSpawning)] #times of spawning

    # Normal distribution around 0.5
    mu, sigma = 0.5, 0.1 # mean and standard deviation
    prng = RandomState(1) # random number generator (specify number to ensure the same sequence each time)
    s = prng.normal(mu, sigma, len(spawningTimes)) # random distribution
    num=(s*releaseParticles).astype(int) # number of particles released per spawning event
    print num #timeStepsSpawning * releaseParticles * mu gives approx. number of particles released
    num=np.sort(num) #sort particles in increasing order 
    num=np.concatenate((num[len(num)%2::2],num[::-2]),axis=0) #release the highest number of particles at the midpoint of the spawning period
	
    print "SPAWNING: Simulated spawning will release %s eggs"%(np.sum(num))

    return num, spawningTimes
Beispiel #13
0
    def fit(self, X, y):
        """ Fit training data.
        Parameters
        ----------
        X : {array-like}, shape = [n_samples, n_features]
          Training vectors, where n_samples is the number of samples and
          n_features is the number of features.
        y : array-like, shape = [n_samples]
          Target values.
        Returns
        -------
        self : object
        """
        rgen = RandomState(self.random_state)
        self.w_ = rgen.normal(loc=0.0, scale=0.01, size=1 + X.shape[1])
        self.cost_ = []

        for i in range(self.n_iter):
            net_input = self.net_input(X)
            # Please note that the "activation" method has no effect
            # in the code since it is simply an identity function. We
            # could write `output = self.net_input(X)` directly instead.
            # The purpose of the activation is more conceptual, i.e.,
            # in the case of logistic regression (as we will see later),
            # we could change it to
            # a sigmoid function to implement a logistic regression classifier.
            output = self.activation(net_input)
            errors = (y - output)
            self.w_[1:] += self.eta * X.T.dot(errors)
            self.w_[0] += self.eta * errors.sum()
            cost = (errors**2).sum() / 2.0
            self.cost_.append(cost)
        return self
Beispiel #14
0
def shared_normal(shape, scale=1, rng=None, name=None):
    """Initialize a matrix shared variable with normally distributed elements."""
    if rng is None:
        rng = RandomState(seed=np.random.randint(1 << 30))
    return theano.shared(rng.normal(
        scale=scale, size=shape).astype(theano.config.floatX),
        name=name)
def generate_rand_obs(nExp, nObs, domainBounds, sigvar=None, rng=None):
    # create random sets of observations for each experiment
    if not sigvar: sigvar = 1.
    if not rng: rng = RandomState()

    domainBounds = npa(domainBounds)
    dimX = domainBounds.shape[0]
    minX = domainBounds[:, 0]
    maxX = domainBounds[:, 1]
    rangeX = maxX - minX
    xObs = rng.uniform(size=(nExp, nObs, dimX))
    xObs *= rangeX
    xObs += minX
    yObs = empty(shape=(nExp, nObs))
    for iexp in xrange(nExp):
        good = False
        while not good:
            yObs0 = rng.normal(size=(nObs))
            if yObs0.max() > 0: good = True
        yObs[iexp, :] = yObs0
    # yObs = rng.normal(size=(nExp, nObs, 1))
    yObs *= sigvar

    return {'x': xObs,
            'y': yObs}
class ParticleSource(SerializableH5):

    def __init__(self, name, shape, initial_number_of_particles, particles_to_generate_each_step, mean_momentum,
                 temperature, charge, mass):
        if initial_number_of_particles <= 0:
            raise ValueError("initial_number_of_particles <= 0")
        if particles_to_generate_each_step < 0:
            raise ValueError("particles_to_generate_each_step < 0")
        if temperature < 0:
            raise ValueError("temperature < 0")
        if mass < 0:
            raise ValueError("mass < 0")
        self.name = name
        self.shape = shape
        self.initial_number_of_particles = initial_number_of_particles
        self.particles_to_generate_each_step = particles_to_generate_each_step
        self.mean_momentum = mean_momentum
        self.temperature = temperature
        self.charge = charge
        self.mass = mass
        self._generator = RandomState()

    def generate_initial_particles(self):
        return self.generate_num_of_particles(self.initial_number_of_particles)

    def generate_each_step(self):
        return self.generate_num_of_particles(self.particles_to_generate_each_step)

    def generate_num_of_particles(self, num_of_particles):
        pos = self.shape.generate_uniform_random_posititons(self._generator, num_of_particles)
        mom = self._generator.normal(self.mean_momentum, sqrt(self.mass * self.temperature), (num_of_particles, 3))
        return ParticleArray(range(num_of_particles), self.charge, self.mass, pos, mom)
Beispiel #17
0
def test_one_sample():
    prng = RandomState(42)

    x = np.array(range(5))
    y = x - 1

    # case 1: one sample only
    res = one_sample(x, seed=42, reps=100)
    np.testing.assert_almost_equal(res[0], 0.05999999)
    np.testing.assert_equal(res[1], 2)

    # case 2: paired sample
    res = one_sample(x, y, seed=42, reps=100)
    np.testing.assert_equal(res[0], 0.02)
    np.testing.assert_equal(res[1], 1)

    # case 3: break it - supply x and y, but not paired
    y = np.append(y, 10)
    assert_raises(ValueError, one_sample, x, y)

    # case 4: say keep_dist=True
    res = one_sample(x, seed=42, reps=100, keep_dist=True)
    np.testing.assert_almost_equal(res[0], 0.05999999)
    np.testing.assert_equal(res[1], 2)
    np.testing.assert_equal(min(res[2]), -2)
    np.testing.assert_equal(max(res[2]), 2)
    np.testing.assert_equal(np.median(res[2]), 0)

    # case 5: use t as test statistic
    y = x + prng.normal(size=5)
    res = one_sample(x, y, seed=42, reps=100, stat="t", alternative="less")
    np.testing.assert_almost_equal(res[0], 0.05)
    np.testing.assert_almost_equal(res[1], -1.4491883)
Beispiel #18
0
    def _randomize_sim(self, random_state: RandomState):
        values = self._randomizer_param_values
        assert isinstance(values, np.ndarray)

        timeconst_mean, timeconst_std, dampratio_mean, dampratio_std = values
        assert timeconst_std >= 0.0
        assert dampratio_std >= 0.0

        self.sim.model.geom_solref[:, 0] = self._initial_value[:, 0] * np.exp(
            random_state.normal(timeconst_mean,
                                scale=timeconst_std,
                                size=self._initial_value.shape[0]))
        self.sim.model.geom_solref[:, 1] = self._initial_value[:, 1] * np.exp(
            random_state.normal(dampratio_mean,
                                scale=dampratio_std,
                                size=self._initial_value.shape[0]))
Beispiel #19
0
def six_node_graph():
    Path('data').mkdir(exist_ok=True)
    rng = RandomState(1)
    graph = GraphModel()
    rv_dim = 2

    # init varnode
    for _ in range(6):
        potential = np.ones([rv_dim])
        varnode = VarNode(rv_dim, potential)
        graph.add_varnode(varnode)

    # init factornode
    edges = [
        [0, 1],
        [1, 2],
        [1, 3],
        [3, 4],
        [3, 5]
    ]
    for item in edges:
        potential = rng.normal(size=[rv_dim, rv_dim])
        factorname = [f"VarNode_{data:03d}" for data in item]
        factornode = FactorNode(factorname, np.exp(potential))
        graph.add_factornode(factornode)
    graph.plot(f"data/six_node_graph.png")

    return graph
Beispiel #20
0
def test_one_sample():
    prng = RandomState(42)
    
    x = np.array(range(5))
    y = x-1
    
    # case 1: one sample only
    res = one_sample(x, seed = 42, reps = 100)
    np.testing.assert_almost_equal(res[0], 0.05999999)
    np.testing.assert_equal(res[1], 2)
    
    # case 2: paired sample
    res = one_sample(x, y, seed = 42, reps = 100)
    np.testing.assert_equal(res[0], 0.02)
    np.testing.assert_equal(res[1], 1)
    
    # case 3: break it - supply x and y, but not paired
    y = np.append(y, 10)
    assert_raises(ValueError, one_sample, x, y)
    
    # case 4: say keep_dist=True
    res = one_sample(x, seed = 42, reps = 100, keep_dist=True)
    np.testing.assert_almost_equal(res[0], 0.05999999)
    np.testing.assert_equal(res[1], 2)
    np.testing.assert_equal(min(res[2]), -2)
    np.testing.assert_equal(max(res[2]), 2)
    np.testing.assert_equal(np.median(res[2]), 0)

    # case 5: use t as test statistic
    y = x + prng.normal(size=5)
    res = one_sample(x, y, seed = 42, reps = 100, stat = "t", alternative = "less")
    np.testing.assert_equal(res[0], 0.07)
    np.testing.assert_almost_equal(res[1], 1.4491883)
Beispiel #21
0
    def fit(self, X, y):
        """Fit training data.
        Parameters
        ----------
        X : {array-like}, shape = [n_samples, n_features]
          Training vectors, where n_samples is the number of samples and
          n_features is the number of features.
        y : array-like, shape = [n_samples]
          Target values.
        Returns
        -------
        self : object
        """
        rgen = RandomState(self.random_state)
        self.w_ = rgen.normal(loc=0.0, scale=0.01, size=1 + X.shape[1])
        self.errors_ = []

        for _ in range(self.n_iter):
            errors = 0
            for xi, target in zip(X, y):
                update = self.eta * (target - self.predict(xi))
                self.w_[1:] += update * xi
                self.w_[0] += update
                errors += int(update != 0.0)
            self.errors_.append(errors)
        return self
    def test_01(self):
        n = 64
        k = 4
        random_instance = RandomState()
        challenges = [random_instance.choice((-1, +1), n) 
            for i in range(1000)]
        challenges = array(challenges, dtype=int8)
        weights = random_instance.normal(loc=0, scale=1, size=(k, n))

        transformed_challenges = ph.transform_id(challenges, k)

        ph_solution = ph.eval_sign(transformed_challenges, weights)
        
        numpy_solution = sign(
            transpose(
                array([
                    dot(
                        transformed_challenges[:,l],
                        weights[l]
                    )
                    for l in range(k)
                ])
            )).astype(int)

        numpy_solution = array(numpy_solution, dtype=int8, copy=True)

        assert_array_equal(
            ph_solution,
            numpy_solution,
            "Comparison of eval_sign with numpy fails."
        )
def rand_zern(randker):
    N = 48
    N_zern = 10
    rho_max = 0.9
    eps_rho = 1.4
    randgen = RandomState(randker)
    extents = [-1, 1, -1, 1]

    # Construct the coordinates
    x = np.linspace(-rho_max, rho_max, N)
    rho_spacing = x[1] - x[0]
    xx, yy = np.meshgrid(x, x)
    rho = np.sqrt(xx ** 2 + yy ** 2)
    theta = np.arctan2(xx, yy)
    aperture_mask = rho <= eps_rho * rho_max
    rho, theta = rho[aperture_mask], theta[aperture_mask]
    rho_max = np.max(rho)
    extends = [-rho_max, rho_max, -rho_max, rho_max]

    # Compute the Zernike series
    coef = randgen.normal(size=N_zern)
    z = zern.ZernikeNaive(mask=aperture_mask)
    phase_map = z(coef=coef, rho=rho, theta=theta, normalize_noll=False, mode='Jacobi', print_option='Silent')
    phase_map = zern.rescale_phase_map(phase_map, peak=1)
    phase_2d = zern.invert_mask(phase_map, aperture_mask)

    return phase_2d
Beispiel #24
0
    def get_weight(self, date_time, how, error_magnitude=0., error_type=None):
        if self.tv_type == 'day:hour':
            if how == 'linear':
                day = date_time.date()
                m = (self.data[day][date_time.hour + 1] -
                     self.data[day][date_time.hour])
                w = self.data[day][date_time.hour] + m * (date_time.minutes /
                                                          60)
            elif how == 'last':
                w = self.data[date_time.day()][date_time.hour]
            elif how == 'next':
                w = self.data[date_time.day()][date_time.hour + 1]
            else:
                raise AttributeError("Invalid input for argument 'how'.")
        elif self.tv_type == 'day:schedule':
            w = min([
                t - date_time.time() for t in self.data[date_time.isoweekday()]
                if t > date_time.time()
            ])
        elif self.tv_type is None:
            w = self.data
        else:
            raise TypeError("Time variance type corrupted.")

        if error_type.lower() in ['normal', 'gaussian']:
            w += RandomState.normal(loc=error_magnitude[0],
                                    scale=error_magnitude[1])
        elif error_type.lower() == 'uniform':
            w += RandomState.uniform(low=error_magnitude[0],
                                     high=error_magnitude[1])
        else:
            pass

        return w
Beispiel #25
0
def transform(X_input):
    """Calculate the random fourier features for an Gaussian kernel

    Keyword arguments:
    X_input - the input matrix X_input which will be transformed
    """

    # Parameters
    m = 3000
    gamma = 60

    # Copy
    X = X_input

    # Get the dimensions
    d = 0
    if len(X.shape)<=1:
        d = len(X)
    else:
        d = X.shape[1]

    # Draw iid m samples omega from p and b from [0,2pi]
    random_state = RandomState(124)
    omega = np.sqrt(2.0 * gamma) * random_state.normal(size=(d, m))
    b = random_state.uniform(0, 2 * np.pi, size=m)

    # Transform the input
    projection = np.dot(X, omega) + b
    Z = np.sqrt(2.0/m) * np.cos(projection)

    return Z
Beispiel #26
0
def fun_rand_problems(N=10, S=100, K=10, d=6, noise_level=1, seed=None):
    rng = RandomState(seed)
    t = np.arange(S)/S
    D = [[10*rng.rand()*np.sin(2*np.pi*K*rng.rand()*t +
                               (0.5-rng.rand())*np.pi)
          for _ in range(d)]
         for _ in range(K)]
    D = np.array(D)
    nD = np.sqrt((D*D).sum(axis=-1))[:, :, np.newaxis]
    D /= nD + (nD == 0)

    rho = .1*K/(d*S)
    pbs = []
    for n in range(N):
        out.write("\rProblem construction: {:7.2%}".format(n/N))
        out.flush()
        T = rng.randint(50, 70)
        Z = (rng.rand(K, (T-1)*S+1) < rho)*rng.rand(K, (T-1)*S+1)*10
        X = np.array([[np.convolve(zk, dk, 'full') for dk in Dk]
                      for Dk, zk in zip(D, Z)]).sum(axis=0)
        X += noise_level*rng.normal(size=X.shape)

        pbs += [MultivariateConvolutionalCodingProblem(
            D, X, lmbd=.1)]
    return pbs, D
Beispiel #27
0
def test_two_sample():
    prng = RandomState(42)

    # Normal-normal, different means examples
    x = prng.normal(1, size=20)
    y = prng.normal(4, size=20)
    res = two_sample(x, y, seed=42)
    expected = (1.0, -2.90532344604777)
    assert_almost_equal(res, expected, 5)
    res = two_sample(x, y, seed=42, plus1=False)
    assert_almost_equal(res, expected)

    # This one has keep_dist = True
    y = prng.normal(1.4, size=20)
    res = two_sample(x, y, seed=42)
    res2 = two_sample(x, y, seed=42, keep_dist=True)
    expected = (0.96975, -0.54460818906623765)
    assert_almost_equal(res[0], expected[0], 2)
    assert_equal(res[1], expected[1])
    assert_almost_equal(res2[0], expected[0], 2)
    assert_equal(res2[1], expected[1])

    # Normal-normal, same means
    y = prng.normal(1, size=20)
    res = two_sample(x, y, seed=42)
    expected = (0.66505000000000003, -0.13990200413154097)
    assert_almost_equal(res[0], expected[0], 2)
    assert_equal(res[1], expected[1])

    # Check the permutation distribution
    res = two_sample(x, y, seed=42, keep_dist=True)
    expected_pv = 0.66505000000000003
    expected_ts = -0.13990200413154097
    exp_dist_firstfive = [-0.1312181,  0.1289127, -0.3936627, -0.1439892,  0.7477683]
    assert_almost_equal(res[0], expected_pv, 2)
    assert_equal(res[1], expected_ts)
    assert_equal(len(res[2]), 100000)
    assert_almost_equal(res[2][:5], exp_dist_firstfive)

    # Define a lambda function (K-S test)
    f = lambda u, v: np.max(
        [abs(sum(u <= val) / len(u) - sum(v <= val) / len(v))
         for val in np.concatenate([u, v])])
    res = two_sample(x, y, seed=42, stat=f, reps=100, plus1=False)
    expected = (0.62, 0.20000000000000007)
    assert_equal(res[0], expected[0])
    assert_equal(res[1], expected[1])
Beispiel #28
0
def test_two_sample_shift():
    prng = RandomState(42)

    # Normal-normal, different means examples
    x = prng.normal(1, size=20)
    y = prng.normal(4, size=20)
    f = lambda u: u - 3
    finv = lambda u: u + 3
    f_err = lambda u: 2 * u
    f_err_inv = lambda u: u / 2
    expected_ts = -2.9053234460477784

    # Test null with shift other than zero
    res = two_sample_shift(x, y, seed=42, shift=2, plus1=False)
    assert res[0] == 1
    assert res[1] == expected_ts
    print("finished test 1 in test_two_sample_shift()")

    res2 = two_sample_shift(x, y, seed=42, shift=2, keep_dist=True)
    np.testing.assert_almost_equal(res2[0], 1, 4)
    assert res2[1] == expected_ts
    np.testing.assert_almost_equal(res2[2][:3],
                                   np.array([1.140174, 2.1491466, 2.6169429]))
    print("finished test 2 in test_two_sample_shift()")

    res = two_sample_shift(x, y, seed=42, shift=2, alternative="less")
    np.testing.assert_almost_equal(res[0], 0, 3)
    assert res[1] == expected_ts
    print("finished test 3 in test_two_sample_shift()")

    # Test null with shift -3
    res = two_sample_shift(x, y, seed=42, shift=(f, finv))
    np.testing.assert_almost_equal(res[0], 0.377, 2)
    assert res[1] == expected_ts
    print("finished test 4 in test_two_sample_shift()")

    res = two_sample_shift(x, y, seed=42, shift=(f, finv), alternative="less")
    np.testing.assert_almost_equal(res[0], 0.622, 2)
    assert res[1] == expected_ts
    print("finished test 5 in test_two_sample_shift()")

    # Test null with multiplicative shift
    res = two_sample_shift(x,
                           y,
                           seed=42,
                           shift=(f_err, f_err_inv),
                           alternative="two-sided")
    np.testing.assert_almost_equal(res[0], 0, 3)
    assert res[1] == expected_ts
    print("finished test 6 in test_two_sample_shift()")

    # Define a lambda function
    f = lambda u, v: np.max(u) - np.max(v)
    res = two_sample(x, y, seed=42, stat=f, reps=100)
    expected = (1, -3.2730653690015465)
    assert res[0] == expected[0]
    assert res[1] == expected[1]
    print("finished test 7 in test_two_sample_shift()")
Beispiel #29
0
def fun_rand_problem(T, S, K, d, lmbd, noise_level, seed=None):
    rng = RandomState(seed)
    rho = K / (d * S)
    D = rng.normal(scale=10.0, size=(K, d, S))
    D = np.array(D)
    nD = np.sqrt((D * D).sum(axis=-1, keepdims=True))
    D /= nD + (nD == 0)

    Z = (rng.rand(K, (T - 1) * S + 1) < rho).astype(np.float64)
    Z *= rng.normal(scale=10, size=(K, (T - 1) * S + 1))

    X = np.array([[np.convolve(zk, dk, 'full') for dk in Dk]
                  for Dk, zk in zip(D, Z)]).sum(axis=0)
    X += noise_level * rng.normal(size=X.shape)

    z0 = np.zeros((K, (T - 1) * S + 1))
    pb = MultivariateConvolutionalCodingProblem(D, X, z0=z0, lmbd=lmbd)
    return pb
Beispiel #30
0
def synthetic(n, phi=0.9, k=3, var=1, seed=1):
    """Auto-regressive test data generator."""
    prng = RandomState(seed)
    P = zeros((n,))
    X = zeros((n,))

    # Initial values for P, X.
    P[0] = prng.normal(0,var)
    X[0] = prng.normal(0, var)

    for i in xrange(1, n):
        P[i] = P[i-1] + X[i-1] + k*prng.normal(0, var)
        X[i] = phi*X[i-1] + prng.normal(0, var)

    R = max(P) - min(P)
    Z = exp(P/R)

    return Z
Beispiel #31
0
 def setup_class(self):
     self.initial_values = [100, 5, 1]
     func = lambda p, x: p[0] * np.exp(-0.5 / p[2] ** 2 * (x - p[1]) ** 2)
     errf = lambda p, x, y: (func(p, x) - y)
     self.xdata = np.arange(0, 10, 0.1)
     sigma = 8. * np.ones_like(self.xdata)
     rsn = RandomState(1234567890)
     yerror = rsn.normal(0, sigma)
     self.ydata = func(self.initial_values, self.xdata) + yerror
    def solve(self, optimization_function):
        rand = RandomState(self.seed)

        X = optimization_function.X
        n_dim = X.shape[1]

        for i in range(self.n_mc):
            hyperplane_normal = rand.normal(0, 1, n_dim)
            optimization_function.compute(hyperplane_normal)
Beispiel #33
0
class ParticleSource(SerializableH5):
    @inject.params(shape=Box)
    def __init__(self,
                 name="particle_source",
                 shape=None,
                 initial_number_of_particles: int = 0,
                 particles_to_generate_each_step: int = 0,
                 mean_momentum: VectorInput = 0,
                 temperature: float = 0.,
                 charge: float = 0.,
                 mass: float = 1.):
        self.name = name
        self.shape = shape
        self.initial_number_of_particles = initial_number_of_particles
        self.particles_to_generate_each_step = particles_to_generate_each_step
        self.mean_momentum = vector(mean_momentum)
        self.temperature = temperature
        self.charge = charge
        self.mass = mass
        self._generator = RandomState()

    def generate_initial_particles(self) -> ParticleArray:
        return self.generate_num_of_particles(self.initial_number_of_particles)

    def generate_each_step(self):
        return self.generate_num_of_particles(
            self.particles_to_generate_each_step)

    def generate_num_of_particles(self, num_of_particles):
        pos = self.shape.generate_uniform_random_posititons(
            self._generator, num_of_particles)
        mom = self._generator.normal(self.mean_momentum,
                                     sqrt(self.mass * self.temperature),
                                     (num_of_particles, 3))
        return ParticleArray(range(num_of_particles), self.charge, self.mass,
                             pos, mom)

    @staticmethod
    def import_h5(g):
        from ef.config.components import Shape
        ga = g.attrs
        shape = Shape.import_h5(g, region=False)
        name = g.name.split('/')[-1]
        momentum = np.array([ga['mean_momentum_{}'.format(c)]
                             for c in 'xyz']).reshape(3)
        return ParticleSource(name, shape,
                              int(ga['initial_number_of_particles']),
                              int(ga['particles_to_generate_each_step']),
                              momentum, float(ga['temperature']),
                              float(ga['charge']), float(ga['mass']))

    def export_h5(self, g):
        for k in 'temperature', 'charge', 'mass', 'initial_number_of_particles', 'particles_to_generate_each_step':
            g.attrs[k] = [getattr(self, k)]
        for i, c in enumerate('xyz'):
            g.attrs['mean_momentum_{}'.format(c)] = [self.mean_momentum[i]]
        self.shape.export_h5(g, region=False)
def obtainSufficientStatisticsForChainGraphRateMtx(nStates, nRep=5000, bt=5.0, nSeq=100, SDOfBiWeights=0.5, bivariateDictionary=None):

    ## we generate the sufficient statistics for a large number of replications first
    ## and then we summarize the sufficient statistics for the forward sampler and
    ## then we use this data to run HMC and local BPS algorithms separately to see
    ## if we can obtain reasonable estimates of the exchangeable parameters

    seedNum = np.arange(0, nRep)

    if bivariateDictionary is None:
        bivariateDictionary = getHardCodedDictChainGraph(nStates)

    prng = RandomState(1234567890)
    stationaryWeights = prng.uniform(0, 1, nStates)
    bivariateWeights = prng.normal(0, SDOfBiWeights, int((nStates) * (nStates - 1) / 2))
    testRateMtx = ReversibleRateMtxPiAndBinaryWeightsWithGraphicalStructure(nStates, stationaryWeights,
                                                                            bivariateWeights, bivariateDictionary)
    stationaryDist = testRateMtx.getStationaryDist()
    rateMatrix = testRateMtx.getRateMtx()

    nInit = np.zeros(nStates)
    holdTimes = np.zeros(nStates)
    nTrans = np.zeros((nStates, nStates))

    for i in seedNum:

        seqList = generateFullPathUsingRateMtxAndStationaryDist(RandomState(i), nSeq, nStates,  rateMatrix,
                                                                stationaryDist, bt)
        ## summarize the sufficient statistics
        ## extract first state from sequences
        firstStates = getFirstAndLastStateOfListOfSeq(seqList)['firstLastState'][:, 0]
        unique, counts = np.unique(firstStates, return_counts=True)
        nInitCount = np.asarray((unique, counts)).T
        nInit = nInit + nInitCount[:, 1]

        for j in range(nSeq):
            sequences = seqList[j]
            holdTimes = holdTimes + sequences['sojourn']
            nTrans = nTrans + sequences['transitCount']
        print(i)

    avgNTrans = nTrans / nRep
    avgHoldTimes = holdTimes / nRep
    avgNInit = nInit / nRep

    result = {}
    result['stationaryWeights'] = stationaryWeights
    result['bivariateDictionary'] = bivariateDictionary
    result['bivariateWeights'] = bivariateWeights
    result['rateMatrix'] = rateMatrix
    result['stationaryDist'] = stationaryDist
    result['exchangeableCoef'] = testRateMtx.getExchangeCoef()
    result['transitCount'] = avgNTrans
    result['sojourn'] = avgHoldTimes
    result['nInit'] = avgNInit
    return result
def test_pooling_for_parameters(inputs_shape, pool_shape, pool_stride,
        testname):
    sys.stdout.write("{:40s} ...".format(testname))
    rng = RandomState(hash('tobipuma') % 4294967295) 
    inputs = rng.normal(size=inputs_shape).astype(np.float32)
    dnn_pool3d2d_func = create_dnnpool3d2d_func(pool_shape, pool_stride,
        inputs_shape[2:])
    reference_result = max_pool_3d_numpy(inputs, pool_shape, pool_stride)
    test_function(dnn_pool3d2d_func, testname, reference_result, inputs)
    sys.stdout.write(" Ok.\n")
Beispiel #36
0
    def _randomize_sim(self, random_state: RandomState):
        values = self._randomizer_param_values
        assert isinstance(values, np.ndarray)

        dmax_mean, dmax_std, delta_mean, delta_std, width_mean, width_std = values
        assert dmax_std >= 0.0
        assert delta_std >= 0.0
        assert width_std >= 0.0

        # We randomize (1-dmax) since dmax typically very close to 1 and we'd like to cover the
        # range [0, 1] well. We then sample delta that is subtracted from dmax to produce dmin,
        # thus ensuring that dmin <= dmax holds.
        dmax = 1.0 - (1.0 - self._initial_value[:, 1]) * np.exp(
            random_state.normal(
                dmax_mean, scale=dmax_std, size=self._initial_value.shape[0]))
        dmax = np.clip(dmax, *self._drange)
        delta = (self._initial_value[:, 1] -
                 self._initial_value[:, 0]) * np.exp(
                     random_state.normal(delta_mean,
                                         scale=delta_std,
                                         size=self._initial_value.shape[0]))
        dmin = np.clip(dmax - delta, *self._drange)

        # Sample width.
        width = self._initial_value[:, 2] * np.exp(
            random_state.normal(width_mean,
                                scale=width_std,
                                size=self._initial_value.shape[0]))

        # Validate constraints. Mujoco internally already ensures that dmin and dmax are clipped,
        # if necessary (http://mujoco.org/book/modeling.html#CSolver), but we enforce slightly
        # stronger constraints for additional stability.
        assert dmin.shape == dmax.shape == width.shape
        assert (dmin <= dmax).all()
        assert (self._drange[0] <= dmin).all()
        assert (dmin <= self._drange[1]).all()
        assert (self._drange[0] <= dmax).all()
        assert (dmax <= self._drange[1]).all()

        self.sim.model.geom_solimp[:, 0] = dmin
        self.sim.model.geom_solimp[:, 1] = dmax
        self.sim.model.geom_solimp[:, 2] = width
Beispiel #37
0
    def test_01(self):
        N = 1000
        k = 2
        random_instance = RandomState()
        responses = array(random_instance.normal(loc=0, scale=1, size=(N, k)))

        ph_xor = ph.combiner_xor(responses)
        numpy_xor = prod(responses, 1)

        assert_array_equal(ph_xor, numpy_xor,
                           "Comparison of combiner_xor with numpy fails.")
Beispiel #38
0
def bimodal_demand(
    number_of_flows: int, random_state: RandomState = RandomState()) -> Demand:
    """
    Generates bimodal demand (probabilistic) for one time step
    Args:
        number_of_flows: the number of flows to create
        random_state: numpy RandomState (for seeding)
    Returns:
        A demand array
    """
    demand = np.zeros(number_of_flows, dtype=float)
    for i in range(number_of_flows):
        # Coin flip
        if random_state.uniform() < 0.8:
            # Standard flow
            demand[i] = random_state.normal(150, 20)
        else:
            # Elephant flow
            demand[i] = random_state.normal(800, 20)
    return demand
Beispiel #39
0
    def _randomize_sim(self, random_state: RandomState):
        values = self._randomizer_param_values
        assert isinstance(values, np.ndarray)

        self.sim.model.actuator_gainprm[:, self.
                                        _idx] = self._initial_value * np.exp(
                                            random_state.normal(
                                                values[0],
                                                scale=abs(values[1]),
                                                size=self._initial_value.shape)
                                        )
Beispiel #40
0
class LSTDRP(td.LSTDLambdaJP):
    """
        LSTD-l1
        regularized LSTD approach adding an l1 penalty on the A * theta - b residuals.

        A Danzuig Selector Approach to Temporal Difference Learning
        Geist M., Scherrer B., ... (ICML 2012)
    """

    def __init__(self, dim_lower, seed=None, **kwargs):
        self.dim_lower = dim_lower
        self.seed = seed
        td.LSTDLambdaJP.__init__(self, **kwargs)

    def reset(self):
        td.LSTDLambdaJP.reset(self)
        if self.seed is not None:
            self.prng = RandomState(self.seed)
        else:
            self.prng = np.random

    @property
    def theta(self):
        try:
            self._tic()
            D = self.C1.shape[0]
            n = self.t
            if self.dim_lower < 1:
                dim_lower = np.sqrt(n)
                dim_lower = np.maximum(1, dim_lower)
                dim_lower = int(dim_lower) + 1
            else:
                dim_lower = self.dim_lower
            proj = self.prng.normal(scale=1.0 / np.sqrt(dim_lower), size=(dim_lower, D))
            # for debugging, sanity check!
            # dim_lower = 6
            # proj = np.eye(D)[:dim_lower,:]
            A = np.dot(proj, np.dot(-self.C1 - self.C2, proj.T))
            b = np.dot(proj, self.b)
            # Phi = np.dot(self.Phi.finalized, proj)
            # Psi = np.dot(self.Psi.finalized, proj)
            # b = np.dot(Phi.T, self.R.finalized.flatten()).flatten()
            # A = np.dot(Phi.T, Psi)

            theta_t = np.dot(np.linalg.pinv(A), b)
            return np.dot(theta_t, proj).flatten()
        except np.linalg.LinAlgError, e:
            print e
            return np.zeros_like(self.b)
        finally:
Beispiel #41
0
def block(seed):
    """ Return block of normal random numbers

    Parameters
    ----------
    seed : {None, int}
        The seed to generate the noise.sd

    Returns
    --------
    noise : numpy.ndarray
        Array of random numbers
    """
    num = SAMPLE_RATE * BLOCK_SIZE
    rng = RandomState(seed % 2**32)
    variance = SAMPLE_RATE / 2
    return rng.normal(size=num, scale=variance**0.5)
Beispiel #42
0
def test_two_sample_shift():
    prng = RandomState(42)

    # Normal-normal, different means examples
    x = prng.normal(1, size=20)
    y = prng.normal(4, size=20)
    f = lambda u: u - 3
    finv = lambda u: u + 3
    f_err = lambda u: 2 * u
    f_err_inv = lambda u: u / 2
    expected_ts = -2.9053234460477784

    # Test null with shift other than zero
    res = two_sample_shift(x, y, seed=42, shift=2, plus1=False)
    assert_equal(res[0], 1)
    assert_equal(res[1], expected_ts)
    res2 = two_sample_shift(x, y, seed=42, shift=2, keep_dist=True)
    assert_almost_equal(res2[0], 1, 4)
    assert_equal(res2[1], expected_ts)
    assert_almost_equal(res2[2][:3], np.array(
        [1.140174 , 2.1491466, 2.6169429]))
    res = two_sample_shift(x, y, seed=42, shift=2, alternative="less")
    assert_almost_equal(res[0], 0, 3)
    assert_equal(res[1], expected_ts)

    # Test null with shift -3
    res = two_sample_shift(x, y, seed=42, shift=(f, finv))
    assert_almost_equal(res[0], 0.377, 2)
    assert_equal(res[1], expected_ts)
    res = two_sample_shift(x, y, seed=42, shift=(f, finv), alternative="less")
    assert_almost_equal(res[0], 0.622, 2)
    assert_equal(res[1], expected_ts)

    # Test null with multiplicative shift
    res = two_sample_shift(x, y, seed=42,
        shift=(f_err, f_err_inv), alternative="two-sided")
    assert_almost_equal(res[0], 0, 3)
    assert_equal(res[1], expected_ts)

    # Define a lambda function
    f = lambda u, v: np.max(u) - np.max(v)
    res = two_sample(x, y, seed=42, stat=f, reps=100)
    expected = (1, -3.2730653690015465)
    assert_equal(res[0], expected[0])
    assert_equal(res[1], expected[1])
Beispiel #43
0
def test_two_sample_shift():
    prng = RandomState(42)

    # Normal-normal, different means examples
    x = prng.normal(1, size=20)
    y = prng.normal(4, size=20)
    f = lambda u: u - 3
    finv = lambda u: u + 3
    f_err = lambda u: 2 * u
    f_err_inv = lambda u: u / 2
    expected_ts = -2.9053234460477784

    # Test null with shift other than zero
    res = two_sample_shift(x, y, seed=42, shift=2)
    np.testing.assert_equal(res[0], 1)
    np.testing.assert_equal(res[1], expected_ts)
    res2 = two_sample_shift(x, y, seed=42, shift=2, keep_dist=True)
    np.testing.assert_equal(res2[0], 1)
    np.testing.assert_equal(res2[1], expected_ts)
    np.testing.assert_almost_equal(res2[2][:3], np.array(
        [1.55886506,  0.87281296,  1.13611123]))
    res = two_sample_shift(x, y, seed=42, shift=2, alternative="less")
    np.testing.assert_equal(res[0], 0)
    np.testing.assert_equal(res[1], expected_ts)

    # Test null with shift -3
    res = two_sample_shift(x, y, seed=42, shift=(f, finv))
    np.testing.assert_equal(res[0], 0.38074999999999998)
    np.testing.assert_equal(res[1], expected_ts)
    res = two_sample_shift(x, y, seed=42, shift=(f, finv), alternative="less")
    np.testing.assert_almost_equal(res[0], 0.61925)
    np.testing.assert_equal(res[1], expected_ts)

    # Test null with multiplicative shift
    res = two_sample_shift(x, y, seed=42,
        shift=(f_err, f_err_inv), alternative="two-sided")
    np.testing.assert_equal(res[0], 0)
    np.testing.assert_equal(res[1], expected_ts)

    # Define a lambda function
    f = lambda u, v: np.max(u) - np.max(v)
    res = two_sample(x, y, seed=42, stat=f, reps=100)
    expected = (1, -3.2730653690015465)
    np.testing.assert_equal(res[0], expected[0])
    np.testing.assert_equal(res[1], expected[1])
Beispiel #44
0
class BetaModelSource(object):
    def __init__(self):

        self.prng = RandomState(32)
        self.kT = kT
        self.Z = Z
        self.O = O
        self.Ca = Ca

        nx = 128
        ddims = (nx,nx,nx)

        x, y, z = np.mgrid[-R:R:nx*1j,
                           -R:R:nx*1j,
                           -R:R:nx*1j]

        r = np.sqrt(x**2+y**2+z**2)

        dens = np.zeros(ddims)
        dens[r <= R] = rho_c*(1.+(r[r <= R]/r_c)**2)**(-1.5*beta)
        dens[r > R] = 0.0
        pden = np.zeros(ddims)
        x = r[r <= R]/r_s
        pden[r <= R] = rho_s/(x*(1.+x)**2)
        pden[r > R] = 0.0
        temp = self.kT*K_per_keV*np.ones(ddims)
        bbox = np.array([[-0.5,0.5],[-0.5,0.5],[-0.5,0.5]])
        velz = self.prng.normal(loc=v_shift,scale=v_width,size=ddims)
        dm_disp = 1000.*np.ones(ddims) # km/s

        data = {}
        data["density"] = (dens, "g/cm**3")
        data["dark_matter_density"] = (pden, "g/cm**3")
        data["dark_matter_dispersion"] = (dm_disp, "km/s")
        data["temperature"] = (temp, "K")
        data["velocity_x"] = (np.zeros(ddims), "cm/s")
        data["velocity_y"] = (np.zeros(ddims), "cm/s")
        data["velocity_z"] = (velz, "cm/s")
        data["oxygen"] = (self.O*np.ones(ddims), "Zsun")
        data["calcium"] = (self.Ca*np.ones(ddims), "Zsun")
        data["metallicity"] = (self.Z*np.ones(ddims), "Zsun")
        self.ds = load_uniform_grid(data, ddims, length_unit=(2*R, "Mpc"),
                                    nprocs=64, bbox=bbox)
Beispiel #45
0
class ParticleBetaModelSource(object):
    def __init__(self):

        self.prng = RandomState(35)
        self.kT = kT
        self.Z = Z

        num_particles = 1000000

        rr = np.linspace(0.0, R, 10000)
        # This formula assumes beta = 2/3
        M_r = 4*np.pi*rho_c*r_c*r_c*(rr-r_c*np.arctan(rr/r_c))
        M_r *= cm_per_mpc**3

        pmass = M_r[-1]*np.ones(num_particles)/num_particles
        M_r /= M_r[-1]
        u = self.prng.uniform(size=num_particles)

        radius = np.interp(u, M_r, rr, left=0.0, right=1.0)
        dens = rho_c*(1.+(radius/r_c)**2)**(-1.5*beta)
        radius /= (2.*R)
        theta = np.arccos(self.prng.uniform(low=-1.,high=1.,size=num_particles))
        phi = 2.*np.pi*self.prng.uniform(size=num_particles)

        temp = self.kT*K_per_keV*np.ones(num_particles)
        velz = self.prng.normal(loc=v_shift,scale=v_width,size=num_particles)

        bbox = np.array([[-0.5,0.5],[-0.5,0.5],[-0.5,0.5]])

        data = {}
        data["io", "density"] = (dens, "g/cm**3")
        data["io", "temperature"] = (temp, "K")
        data["io", "particle_position_x"] = (radius*np.sin(theta)*np.cos(phi), "code_length")
        data["io", "particle_position_y"] = (radius*np.sin(theta)*np.sin(phi), "code_length")
        data["io", "particle_position_z"] = (radius*np.cos(theta), "code_length")
        data["io", "particle_velocity_x"] = (np.zeros(num_particles), "cm/s")
        data["io", "particle_velocity_y"] = (np.zeros(num_particles), "cm/s")
        data["io", "particle_velocity_z"] = (velz, "cm/s")
        data["io", "particle_mass"] = (pmass, "g")

        self.ds = load_particles(data, length_unit=(2*R, "Mpc"), bbox=bbox)
Beispiel #46
0
def dsim(g, xdists, u_to_x, T, seed, maxitr, tol, ftol, eps):
    """
    Directional simulation. 
    """

    # Seed the random number generator if required
    if seed == -1:
        prng = RandomState()
    else:
        prng = RandomState(seed)

    ndims = len(xdists)
    rays = prng.normal(0, 1, size=(maxitr, ndims))
    unit_rays = rays/la.norm(rays)
    pfs = zeros(maxitr)

    for i, ray in enumerate(unit_rays):
        d_ray = 1
        ray0 = ray 
        while abs(g(u_to_x(ray, xdists, T))) > ftol and abs(d_ray) > ftol:
            # Calculate gradient and directional derivative
            grad = dgdu(g, ray, u_to_x, xdists, T, eps)
            dderiv = dot(grad, ray/la.norm(ray))
            # delta length of ray is g function / directional derivative
            d_ray = g(u_to_x(ray, xdists, T))/dderiv
            ray = ray - d_ray*ray/la.norm(ray)
        # Only store a non-zero pf if the ray hasn't changed direction 
        pfs[i] = 1 - chi2.cdf(la.norm(ray)**2, ndims) if dot(ray0, ray) > 0 else 0
    pfs[isnan(pfs)] = 0
    mu_pf, std_pf = pfs.mean(), pfs.std()
    se_pf = std_pf/sqrt(maxitr)
    cv_pf = se_pf/mu_pf
    beta = -norm.ppf(mu_pf) if mu_pf < 0.5 else norm.ppf(mu_pf)
    msgs = []
    return {'vars': xdists, 'beta': beta, 'Pf': mu_pf, 'stderr': se_pf, 
            'stdcv': cv_pf, 'nitr': maxitr, 'g_beta': -1, 'msgs': msgs}
Beispiel #47
0
rng = RandomState(int(sys.argv[1]))
params_file = open(sys.argv[2], "r")
output_file = open(sys.argv[3], "w")

params = { line.split("=")[0].strip() : eval(line.split("=")[1]) 
           for line in open("params/default_params")   }
for line in params_file :
    params[line.split("=")[0].strip()] = eval(line.split("=")[1])
params_file.close()

target_features = [BinaryFeature.create_random(rng, params["NUM_INPUT_VARS"], 
                                               params["BETA"]) 
                   for _ in range(params["NUM_HIDDEN_TARGETS"])]

#make random weights, but don't use bias (this is what Mahmood paper does)
target_weights = np.concatenate((rng.normal(0,1,params["NUM_HIDDEN_TARGETS"]),
                                 [0])) 

target_functions = [Learner(target_features, target_weights)]

if params["NON_STATIONARY"] :
    features_to_replace = choice_without_replacement(rng, 
                                params["NUM_HIDDEN_TARGETS"],
                                params["PERCENT_TARGET_FEATURES_TO_REPLACE"] * 
                                params["NUM_HIDDEN_TARGETS"])
    
    target_features = [BinaryFeature(feature.weights, 
                                     feature.beta) 
                       for feature in target_features]
    target_weights = target_weights.copy()
    for f in features_to_replace :
Beispiel #48
0
import pandas as pd

from nose.tools import (assert_almost_equal, assert_equal)
from numpy.random import RandomState

from rsmtool.analysis import (correlation_helper,
                              metrics_helper)

prng = RandomState(133)
df_features = pd.DataFrame({'sc1': [1, 2, 3, 4, 1, 2, 3, 4, 1, 2],
                            'f1': prng.normal(0, 1, 10),
                            'f2': prng.normal(1, 0.1, 10),
                            'f3': prng.normal(2, 0.1, 10),
                            'group': ['group1']*10},
                             index  = range(0, 10))

df_features_same_score = df_features.copy()
df_features_same_score[['sc1']] = [3]*10

human_scores = pd.Series(prng.randint(1, 5, size=10))
system_scores = pd.Series(prng.random_sample(10)*5)
same_human_scores = pd.Series([3]*10)

def test_correlation_helper():
    # test that there are no nans for data frame with 10 values
    retval = correlation_helper(df_features, 'sc1', 'group')
    assert_equal(retval[0].isnull().values.sum(), 0)
    assert_equal(retval[1].isnull().values.sum(), 0)


def test_that_correlation_helper_works_for_data_with_one_row():
Beispiel #49
0
     elements = list(elements)
     rnd.shuffle(elements)
     for i in range(len(elements)):
         main = elements[i]
         other = elements[i-1]
         state = reference_states[atomic_numbers[main]]
         if state['symmetry'] in known_states:
             break
 if state['symmetry'] not in known_states:
     print "Cannot simulate %s, reference state '%s' not supported" % (main, state['symmetry'])
     print "SKIPPING MODEL!"
     continue
 
 init_atoms = bulk(main, orthorhombic=True).repeat((7,7,7))
 r = init_atoms.get_positions()
 r += rnd.normal(0.0, 0.1, r.shape)
 init_atoms.set_positions(r)
 z = init_atoms.get_atomic_numbers()
 if other:
     some_atoms = rnd.randint(0, 20, len(init_atoms)) == 0
     z[some_atoms] = atomic_numbers[other]
     init_atoms.set_atomic_numbers(z)
     z_other = atomic_numbers[other]
 else:
     z_other = 0
 print ("Generated a %s system with %i %s-atoms and %i %s-atoms"
        % (state['symmetry'], 
           np.equal(z, atomic_numbers[main]).sum(),
           main,
           np.equal(z, z_other).sum(),
           other))
class TestAnalyzer:

    def setUp(self):

        self.prng = RandomState(133)

        self.df_features = pd.DataFrame({'sc1': [1, 2, 3, 4, 1, 2, 3, 4, 1, 2],
                                         'f1': self.prng.normal(0, 1, 10),
                                         'f2': self.prng.normal(1, 0.1, 10),
                                         'f3': self.prng.normal(2, 0.1, 10),
                                         'group': ['group1'] * 10},
                                        index=range(0, 10))

        self.df_features_same_score = self.df_features.copy()
        self.df_features_same_score[['sc1']] = [3] * 10

        self.human_scores = pd.Series(self.prng.randint(1, 5, size=10))
        self.system_scores = pd.Series(self.prng.random_sample(10) * 5)
        self.same_human_scores = pd.Series([3] * 10)

        # get the directory containing the tests
        self.test_dir = dirname(__file__)

    def test_correlation_helper(self):

        # test that there are no nans for data frame with 10 values
        retval = Analyzer.correlation_helper(self.df_features, 'sc1', 'group')
        assert_equal(retval[0].isnull().values.sum(), 0)
        assert_equal(retval[1].isnull().values.sum(), 0)

    def test_that_correlation_helper_works_for_data_with_one_row(self):
        # this should return two data frames with nans
        # we expect a runtime warning here so let's suppress it
        with warnings.catch_warnings():
            warnings.filterwarnings('ignore', category=RuntimeWarning)
            retval = Analyzer.correlation_helper(self.df_features[:1], 'sc1', 'group')
            assert_equal(retval[0].isnull().values.sum(), 3)
            assert_equal(retval[1].isnull().values.sum(), 3)

    def test_that_correlation_helper_works_for_data_with_two_rows(self):
        # this should return 1/-1 for marginal correlations and nans for
        # partial correlations
        retval = Analyzer.correlation_helper(self.df_features[:2], 'sc1', 'group')
        assert_equal(abs(retval[0].values).sum(), 3)
        assert_equal(retval[1].isnull().values.sum(), 3)

    def test_that_correlation_helper_works_for_data_with_three_rows(self):
        # this should compute marginal correlations but return Nans for
        # partial correlations
        retval = Analyzer.correlation_helper(self.df_features[:3], 'sc1', 'group')
        assert_equal(retval[0].isnull().values.sum(), 0)
        assert_equal(retval[1].isnull().values.sum(), 3)

    def test_that_correlation_helper_works_for_data_with_four_rows(self):
        # this should compute marginal correlations and return a unity
        # matrix for partial correlations
        retval = Analyzer.correlation_helper(self.df_features[:4], 'sc1', 'group')
        assert_equal(retval[0].isnull().values.sum(), 0)
        assert_almost_equal(abs(retval[1].values).sum(), 3)

    def test_that_correlation_helper_works_for_data_with_the_same_label(self):

        # this should return two data frames with nans
        with warnings.catch_warnings():
            warnings.filterwarnings('ignore', category=RuntimeWarning)
            retval = Analyzer.correlation_helper(self.df_features_same_score, 'sc1', 'group')
            assert_equal(retval[0].isnull().values.sum(), 3)
            assert_equal(retval[1].isnull().values.sum(), 3)

    def test_that_metrics_helper_works_for_data_with_one_row(self):
        # There should be NaNs for SMD, correlations and both sds
        with warnings.catch_warnings():
            warnings.filterwarnings('ignore', category=RuntimeWarning)
            evals = Analyzer.metrics_helper(self.human_scores[0:1],
                                            self.system_scores[0:1])
            assert_equal(evals.isnull().values.sum(), 4)

    def test_that_metrics_helper_works_for_data_with_the_same_label(self):
        # There should be NaNs for correlation.
        # Note that for a dataset with a single response
        # kappas will be 0 or 1
        with warnings.catch_warnings():
            warnings.filterwarnings('ignore', category=RuntimeWarning)
            evals = Analyzer.metrics_helper(self.same_human_scores,
                                            self.system_scores)
            assert_equal(evals.isnull().values.sum(), 1)

    def test_metrics_helper_population_sds(self):
        df_new_features = pd.read_csv(join(self.test_dir, 'data', 'files', 'train.csv'))
        # compute the metrics when not specifying the population SDs
        computed_metrics1 = Analyzer.metrics_helper(df_new_features['score'],
                                                    df_new_features['score2'])
        expected_metrics1 = pd.Series({'N': 500.0,
                                       'R2': 0.65340566606389394,
                                       'RMSE': 0.47958315233127197,
                                       'SMD': 0.036736365006090885,
                                       'adj_agr': 100.0,
                                       'corr': 0.82789026370069529,
                                       'exact_agr': 77.0,
                                       'h_max': 6.0,
                                       'h_mean': 3.4199999999999999,
                                       'h_min': 1.0,
                                       'h_sd': 0.81543231461565147,
                                       'kappa': 0.6273493195074531,
                                       'sys_max': 6.0,
                                       'sys_mean': 3.4500000000000002,
                                       'sys_min': 1.0,
                                       'sys_sd': 0.81782496620652367,
                                       'wtkappa': 0.82732732732732728})

        # and now compute them specifying the population SDs
        computed_metrics2 = Analyzer.metrics_helper(df_new_features['score'],
                                                    df_new_features['score2'],
                                                    population_human_score_sd=0.5,
                                                    population_system_score_sd=0.4)
        # the only number that should change is the SMD
        expected_metrics2 = expected_metrics1.copy()
        expected_metrics2['SMD'] = 0.066259

        assert_series_equal(computed_metrics1.sort_index(), expected_metrics1.sort_index())
        assert_series_equal(computed_metrics2.sort_index(), expected_metrics2.sort_index())

    def test_compute_pca_less_components_than_features(self):
        # test pca when we have less components than features
        df = pd.DataFrame({'a': range(100)})
        for i in range(100):
            df[i] = df['a'] * i
        (components, variance) = Analyzer.compute_pca(df, df.columns)
        assert_equal(len(components.columns), 100)
        assert_equal(len(variance.columns), 100)

    def test_compute_disattenuated_correlations_single_human(self):
        hm_corr = pd.Series([0.9, 0.8, 0.6],
                            index=['raw', 'raw_trim', 'raw_trim_round'])
        hh_corr = pd.Series([0.81], index=[''])
        df_dis_corr = Analyzer.compute_disattenuated_correlations(hm_corr,
                                                                  hh_corr)
        assert_equal(len(df_dis_corr), 3)
        assert_equal(df_dis_corr.loc['raw', 'corr_disattenuated'], 1.0)

    def test_compute_disattenuated_correlations_matching_human(self):
        hm_corr = pd.Series([0.9, 0.4, 0.6],
                            index=['All data', 'GROUP1', 'GROUP2'])
        hh_corr = pd.Series([0.81, 0.64, 0.36],
                            index=['All data', 'GROUP1', 'GROUP2'])
        df_dis_corr = Analyzer.compute_disattenuated_correlations(hm_corr,
                                                                  hh_corr)
        assert_equal(len(df_dis_corr), 3)
        assert_array_equal(df_dis_corr['corr_disattenuated'], [1.0, 0.5, 1.0])

    def test_compute_disattenuated_correlations_single_matching_human(self):
        hm_corr = pd.Series([0.9, 0.4, 0.6],
                            index=['All data', 'GROUP1', 'GROUP2'])
        hh_corr = pd.Series([0.81],
                            index=['All data'])
        df_dis_corr = Analyzer.compute_disattenuated_correlations(hm_corr,
                                                                  hh_corr)
        assert_equal(len(df_dis_corr), 3)
        assert_array_equal(df_dis_corr['corr_disattenuated'], [1.0, np.nan, np.nan])

    def test_compute_disattenuated_correlations_mismatched_indices(self):
        hm_corr = pd.Series([0.9, 0.6],
                            index=['All data', 'GROUP2'])
        hh_corr = pd.Series([0.81, 0.64],
                            index=['All data', 'GROUP1'])
        df_dis_corr = Analyzer.compute_disattenuated_correlations(hm_corr,
                                                                  hh_corr)
        assert_equal(len(df_dis_corr), 3)
        assert_array_equal(df_dis_corr['corr_disattenuated'], [1.0, np.nan, np.nan])

    def test_compute_disattenuated_correlations_negative_human(self):
        hm_corr = pd.Series([0.9, 0.8],
                            index=['All data', 'GROUP1'])
        hh_corr = pd.Series([-0.03, 0.64],
                            index=['All data', 'GROUP1'])
        df_dis_corr = Analyzer.compute_disattenuated_correlations(hm_corr,
                                                                  hh_corr)
        assert_equal(len(df_dis_corr), 2)
        assert_array_equal(df_dis_corr['corr_disattenuated'], [np.nan, 1.0])
Beispiel #51
0
def randomu(seed, di=None, binomial=None, double=False, gamma=False,
            normal=False, poisson=False):
    """
    Replicates the randomu function avaiable within IDL
    (Interactive Data Language, EXELISvis).
    Returns an array of uniformly distributed random numbers of the
    specified dimensions.
    The randomu function returns one or more pseudo-random numbers
    with one or more of the following distributions:
    Uniform (default)
    Gaussian
    binomial
    gamma
    poisson

    :param seed:
        If seed is not of type mtrand.RandomState, then a new state is
        initialised. Othersise seed will be used to generate the random
        values.

    :param di:
        A list specifying the dimensions of the resulting array. If di
        is a scalar then randomu returns a scalar.
        Dimensions are D1, D2, D3...D8 (x,y,z,lambda...).
        The list will be inverted to suit Python's inverted dimensions
        i.e. (D3,D2,D1).

    :param binomial:
        Set this keyword to a list of length 2, [n,p], to generate
        random deviates from a binomial distribution. If an event
        occurs with probablility p, with n trials, then the number of
        times it occurs has a binomial distribution.

    :param double:
        If set to True, then randomu will return a double precision
        random numbers.

    :param gamma:
        Set this keyword to an integer order i > 0 to generate random
        deviates from a gamm distribution.

    :param Long:
        If set to True, then randomu will return integer uniform
        random deviates in the range [0...2^31-1], using the Mersenne
        Twister algorithm. All other keywords will be ignored.

    :param normal:
        If set to True, then random deviates will be generated from a
        normal distribution.

    :param poisson:
        Set this keyword to the mean number of events occurring during
        a unit of time. The poisson keword returns a random deviate
        drawn from a poisson distribution with that mean.

    :param ULong:
        If set to True, then randomu will return unsigned integer
        uniform deviates in the range [0..2^32-1], using the Mersenne
        Twister algorithm. All other keywords will be ignored.

    :return:
        A NumPy array of uniformly distributed random numbers of the
        specified dimensions.

    Example:
        >>> seed = None
        >>> x, sd = randomu(seed, [10,10])
        >>> x, sd = randomu(seed, [100,100], binomial=[10,0.5])
        >>> x, sd = randomu(seed, [100,100], gamma=2)
        >>> # 200x by 100y array of normally distributed values
        >>> x, sd = randomu(seed, [200,100], normal=True)
        >>> # 1000 deviates from a poisson distribution with a mean of 1.5
        >>> x, sd = randomu(seed, [1000], poisson=1.5)
        >>> # Return a scalar from a uniform distribution
        >>> x, sd = randomu(seed)

    :author:
        Josh Sixsmith, [email protected], [email protected]

    :copyright:
        Copyright (c) 2014, Josh Sixsmith
        All rights reserved.

        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:

        1. Redistributions of source code must retain the above copyright notice, this
           list of conditions and the following disclaimer.
        2. Redistributions in binary form must reproduce the above copyright notice,
           this list of conditions and the following disclaimer in the documentation
           and/or other materials provided with the distribution.

        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
        ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
        WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
        ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
        (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
        LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
        ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
        (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
        SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

        The views and conclusions contained in the software and documentation are those
        of the authors and should not be interpreted as representing official policies,
        either expressed or implied, of the FreeBSD Project.
    """

    # Initialise the data type
    if double:
        dtype = 'float64'
    else:
        dtype = 'float32'

    # Check the seed
    # http://stackoverflow.com/questions/5836335/consistenly-create-same-random-numpy-array
    if type(seed) != mtrand.RandomState:
        seed = RandomState()

    if di is not None:
        if type(di) is not list:
            raise TypeError("Dimensions must be a list or None.")
        if len(di) > 8:
            raise ValueError("Error. More than 8 dimensions specified.")
        # Invert the dimensions list
        dims = di[::-1]
    else:
        dims = 1

    # Python has issues with overflow:
    # OverflowError: Python int too large to convert to C long
    # Occurs with Long and ULong
    #if Long:
    #    res = seed.random_integers(0, 2**31-1, dims)
    #    if di is None:
    #        res = res[0]
    #    return res, seed

    #if ULong:
    #    res = seed.random_integers(0, 2**32-1, dims)
    #    if di is None:
    #        res = res[0]
    #    return res, seed

    # Check for other keywords
    distributions = 0
    kwds = [binomial, gamma, normal, poisson]
    for kwd in kwds:
        if kwd:
            distributions += 1

    if distributions > 1:
        print("Conflicting keywords.")
        return

    if binomial:
        if len(binomial) != 2:
            msg = "Error. binomial must contain [n,p] trials & probability."
            raise ValueError(msg)

        n = binomial[0]
        p = binomial[1]

        res = seed.binomial(n, p, dims)

    elif gamma:
        res = seed.gamma(gamma, size=dims)

    elif normal:
        res = seed.normal(0, 1, dims)

    elif poisson:
        res = seed.poisson(poisson, dims)

    else:
        res = seed.uniform(size=dims)

    res = res.astype(dtype)

    if di is None:
        res = res[0]

    return res, seed
Beispiel #52
0
			mu = self.get_mu(self.observed[i], w)
			mus = np.hstack((mus, mu))
		mus = mus.reshape((self.n,2))
		sig = np.dot(self.W.transpose(), self.W)
		sig = sig/self.sigx
		sig = np.linalg.inv(sig)

		return mus, sig
		
		
		


if __name__ == "__main__":
	fa = FA(10)
	w = RS.normal(0,1, (3, 2))
	mus, sig = fa.MLE_EP()
	#print fa.W
	#plotter.plot_approx_posterior(sig, mus, 0)
	#print "Truth: ", fa.W
	#print ""
	#print "init: ", w
	#fa.marginal_likelihood(w)
	#print ""
	#print "init: ", fa.W
	#print fa.W
	#fa.marginal_likelihood(fa.W)
	#fa.marginal_likelihood(w)
		

Beispiel #53
0
class Generator():
    seed = None
    random = None
    def __init__(self, seed=1):
        super(Generator, self).__init__()
        self.random = RandomState(seed)
        self.seed = seed
        
    def reseed(self):
        self.random = RandomState(self.seed)
        
    def randSyllable(self):
        c1_dice = ( self.random.random_sample() < 0.91 ) #Chance that a regular consonant will start the syllable
        s1_dice = ( self.random.random_sample() < 0.05 ) #Chance that a special conjunction consonant is used
        v1_dice = ( self.random.random_sample() < 0.85 ) #Chance that a regular vowel will be used
        c2_add_dice = ( self.random.random_sample() < 0.28 ) #Chance that it has an ending consonant
        c2_dice = ( self.random.random_sample() < 0.91 ) #Chance that a regular consonant will end the syllable
        s2_dice = ( self.random.random_sample() < 0.03 ) #Chance that the ending has an addon consonant
        
        c1 = self.random.choice(REGULAR_CONSONANTS) if c1_dice else self.random.choice(COMPOSITE_CONSONANTS)
        s1 = self.random.choice(SPECIAL_CONSONANTS) if s1_dice else ''
        v1 = self.random.choice(REGULAR_VOWELS) if v1_dice else self.random.choice(COMPOSITE_VOWELS)
        c2 = ( self.random.choice(REGULAR_CONSONANTS) if c2_dice else self.random.choice(ENDING_CONSONANTS) ) if c2_add_dice else ''
        s2 = self.random.choice(ADDON_ENDING_CONSONANTS) if s2_dice else ''
        syllable = c1+s1+v1+c2+s2
#         print(syllable)
        return syllable
    
    def randWord(self, s=2):
        """ s = number of syllables in int """
        word = ''
        for syllable in range(0, s):
            word += self.randSyllable()
        return word
    
    def randSentence(self, meter=[2, 2, 1, 2, 3, 2, 1, 2, 2]):
        sentence = []
        for syllable in meter:
            sentence.append(self.randWord(syllable))
        return ' '.join(sentence)
    
    def randParagraph(self):
        paragraph = []
        rand_wordcount = [ self.random.randint(3, 6) for i in range(0, self.random.randint( 4, 5 )) ]
        for words in rand_wordcount:
            rand_meter = [ self.random.randint(1, 4) for i in range(0, words) ]
            sentence = self.randSentence(rand_meter)
            paragraph.append(sentence)
        return '. '.join(paragraph)
    
    def randDictionary(self, word_list=['apple', 'banana', 'cake', 'dog', 'elephant', 'fruit', 'guava', 'human', 'island', 'joke', 'king', 'love', 'mother', 'nature', 'ocean', 'pie', 'queen', 'random', 'start', 'tree', 'up', 'vine', 'wisdom', 'yellow', 'zoo' ]):
        rand_dict_e2r = { word: self.randWord() for word in word_list }
        rand_dict_r2e = { v: k for k, v in rand_dict_e2r.items() }
        ordered_e2r = OrderedDict()
        print("English to Random Language")
        for key in sorted(rand_dict_e2r.keys()):
            print(key+ ' : '+rand_dict_e2r[key])
            ordered_e2r[key] = rand_dict_e2r[key]
        ordered_r2e = OrderedDict()
        print("\n\nRandom Language to English")
        for key in sorted(rand_dict_r2e.keys()):
            print(key+ ' : '+rand_dict_r2e[key])
            ordered_r2e[key] = rand_dict_r2e[key]
        return ( ordered_e2r, ordered_r2e )
    
    def convertWord(self, word):
        word = word.lower()
        saved_state = self.random.get_state()
        
        # Word mapping method : md5
        # To make it more natural, this mapping should be updated
        # to reflect natural language patterns
        md5 = hashlib.md5(bytes(word, encoding='utf-8'))
        wordseed = ( self.seed + int.from_bytes(md5.digest(), 'little') ) % (2**31)
#         print(wordseed)
        self.random.seed( wordseed )
        randword = self.randWord( math.ceil( abs( self.random.normal(2, 1) ) ) )
        self.random.set_state(saved_state)
        return randword
     
    def convertSentence(self, sentence):
        words = sentence.split()
        converted = [self.convertWord(word) for word in words]
        return ' '.join(converted)