Strict Standards: Redefining already defined constructor for class Object in /www/htdocs/w00b2db0/cake/libs/object.php on line 63

Strict Standards: Non-static method Configure::getInstance() should not be called statically in /www/htdocs/w00b2db0/cake/bootstrap.php on line 47
Example for Python Experiment Suite

Example for Python Experiment Suite

This little example demonstrates how to use the Python Experiment Suite (source code available here).

This example verifies a simple stochastic law, namely that the sample mean of n random numbers drawn from a normal distribution converges to the actual mean of the distribution for large n.

The first part is the actual Python script called
from expsuite import PyExperimentSuite
from numpy import *
import os

class MySuite(PyExperimentSuite):
    restore_supported = True
    def reset(self, params, rep):
        # initialize array
        self.numbers = zeros(params['iterations'])
        # seed random number generator
    def iterate(self, params, rep, n):
        # draw normally distributed random number
        self.numbers[n] = random.normal(params['mean'], params['std'])
        # calculate sample mean and offset
        samplemean = mean(self.numbers[:n])
        offset = abs(params['mean']-samplemean)
        # return dictionary
        ret = {'n':n, 'number':self.numbers[n], 
            'samplemean':samplemean, 'offset':offset}
        return ret
    def save_state(self, params, rep, n):
        # save array as binary file
        save(os.path.join(params['path'], params['name'], 
            'array_%i.npy'%rep), self.numbers)

    def restore_state(self, params, rep, n):
        # load array from file
        self.numbers = load(os.path.join(params['path'], 
            params['name'], 'array_%i.npy'%rep))

if __name__ == '__main__':
    mysuite = MySuite()

The part below is the configuration file experiments.cfg. The second part is the configuration file, called experiments.cfg. Copy both in the same folder, then run the script from the command line: python

repetitions = 1
iterations = 4000
path = results
seed = None

mean = 0
std = 1

mean = 0
std = 5
After running the script, the logfiles are stored in a sub-directory ./results/ and can be accessed with the built-in Python API, for example with these commands:
from suite import MySuite
from matplotlib import pyplot as plt
from numpy import *

mysuite = MySuite()
exps = mysuite.get_exps()

print 'lowest offset for experiment normal:', mysuite.get_value(exps[0], 0, 'offset', 'min')
print 'lowest offset for experiment highstd:', mysuite.get_value(exps[1], 0, 'offset', 'min')

plt.plot(mysuite.get_history(exps[0], 0, 'offset'), linewidth=2, color='blue', label='std=1')
plt.plot(mysuite.get_history(exps[1], 0, 'offset'), linewidth=2, color='red', label='std=5')

This page has been viewed 12814 times.