MatCarlo

From FarmShare

Revision as of 17:37, 19 February 2013 by Bishopj (Talk | contribs)
Jump to: navigation, search

Matlab and Monte Carlo simulations

This example shows the use of python to generate many matlab jobs. Each of these matlab jobs is a monte carlo simulation with different parameters as written about here http://en.literateprograms.org/Monte_Carlo_simulation_%28Matlab%29#chunk%20def:simplest_monovariate_montecarlo.

setup

import os

n = 0
for mymu in [ 0.05, 0.06, 0.07, 0.08, 0.09 ]:
    for myT in range(10,50,10):
        n += 1

        matlabstartup = '''
S0      = 1000
mu      = %f
sigma   = .12
T       =   %d
nb_traj = 100
step    = 1/255

[S, t]  = simplest_montecarlo0( sigma, T, nb_traj, S0, mu, step);

listOfVariables = {'S', 't'};
save('run%d/mydata.mat', listOfVariables{:});

mcplot = figure('Color',[0.9412 0.9412 0.9412 ]);
plot(t,S, 'linewidth',2);
axis([t(1) t(end) min(S(:)) max(S(:))]);
xlabel('time');ylabel('value');
title(sprintf('%%d trajectories', size(S,2)));
print(mcplot,'-dpng','run%d/figure.png')
''' % (mymu, myT, n, n)

        qsubscript = '''
#$ -N run%d
#$ -o run%d/job.out
#$ -e run%d/job.error
#$ -cwd
#$ -S /bin/bash
#$ -l testq=1

/mnt/glusterfs/software/non-free/MATLAB-R2012b/bin/matlab -nodesktop -singleCompThread < run%d/run.m
''' % (n,n,n,n)

        os.mkdir('run%d' % n)

        runfile = open('run%d/run.m' % n, 'w')
        runfile.write(matlabstartup)
        runfile.close()

        qsubfile = open('run%d/run.submit' % n, 'w')
        qsubfile.write(qsubscript)
        qsubfile.close()

        os.system('qsub run%d/run.submit' % n)


function [S, t] = simplest_montecarlo0( sigma, T, nb_traj, S0, mu, step)
% SIMPLEST_MONTECARLO - the simplest Monte Carlo simulation with Matlab
  % use:
  %  S = simplest_montecarlo( sigma, T, nb_traj, S0, mu, step)
  % example:
  %  [S, t] = simplest_montecarlo( .12, 5, 50, 100, .05, 1/255);

nT = ceil(T/step);
W  = sigma * sqrt(step) * cumsum(randn(nT, nb_traj));
c  = repmat((mu - sigma^2/2) *step * (1:nT)',1,nb_traj);
S  = [repmat(S0,1,nb_traj); S0 * exp( c + W)];
if nargout > 1
   t = [0;step * (1:nT)'];
end

running

bishopj@corn08:/mnt/glusterfs/bishopj/mattest$ python ~/farmshare/simplemonte.py 
Your job 669323 ("run1") has been submitted
Your job 669324 ("run2") has been submitted
Your job 669325 ("run3") has been submitted
Your job 669326 ("run4") has been submitted
Your job 669327 ("run5") has been submitted
Your job 669328 ("run6") has been submitted
Your job 669329 ("run7") has been submitted
Your job 669330 ("run8") has been submitted
Your job 669331 ("run9") has been submitted
Your job 669332 ("run10") has been submitted
Your job 669333 ("run11") has been submitted
Your job 669334 ("run12") has been submitted
Your job 669335 ("run13") has been submitted
Your job 669336 ("run14") has been submitted
Your job 669337 ("run15") has been submitted
Your job 669338 ("run16") has been submitted
Your job 669339 ("run17") has been submitted
Your job 669340 ("run18") has been submitted
Your job 669341 ("run19") has been submitted
Your job 669342 ("run20") has been submitted




bishopj@corn08:/mnt/glusterfs/bishopj/mattest$ qstat
job-ID  prior   name       user         state submit/start at     queue                          slots ja-task-ID 
-----------------------------------------------------------------------------------------------------------------
 669323 0.25000 run1       bishopj      r     02/19/2013 14:08:10 test.q@barley-testq.stanford.e     1        
 669324 0.25000 run2       bishopj      r     02/19/2013 14:08:10 test.q@barley-testq.stanford.e     1        
 669325 0.25000 run3       bishopj      r     02/19/2013 14:08:10 test.q@barley-testq.stanford.e     1        
 669326 0.25000 run4       bishopj      r     02/19/2013 14:08:10 test.q@barley-testq.stanford.e     1        
 669327 0.25000 run5       bishopj      qw    02/19/2013 14:08:07                                    1        
 669328 0.25000 run6       bishopj      qw    02/19/2013 14:08:08                                    1        
 669329 0.25000 run7       bishopj      qw    02/19/2013 14:08:10                                    1        
 669330 0.25000 run8       bishopj      qw    02/19/2013 14:08:11                                    1        
 669331 0.25000 run9       bishopj      qw    02/19/2013 14:08:12                                    1        
 669332 0.25000 run10      bishopj      qw    02/19/2013 14:08:13                                    1        
 669333 0.25000 run11      bishopj      qw    02/19/2013 14:08:14                                    1        
 669334 0.25000 run12      bishopj      qw    02/19/2013 14:08:16                                    1        
 669335 0.25000 run13      bishopj      qw    02/19/2013 14:08:17                                    1        
 669336 0.25000 run14      bishopj      qw    02/19/2013 14:08:18                                    1        
 669337 0.25000 run15      bishopj      qw    02/19/2013 14:08:19                                    1        
 669338 0.25000 run16      bishopj      qw    02/19/2013 14:08:20                                    1        
 669339 0.25000 run17      bishopj      qw    02/19/2013 14:08:22                                    1        
 669340 0.25000 run18      bishopj      qw    02/19/2013 14:08:23                                    1        
 669341 0.25000 run19      bishopj      qw    02/19/2013 14:08:24                                    1        
 669342 0.00000 run20      bishopj      qw    02/19/2013 14:08:25                                    1

bishopj@corn08:/mnt/glusterfs/bishopj/mattest/run1$ cat job.out 
Warning: No display specified.  You will not be able to display graphics on the screen.
Warning: No window system found.  Java option 'MWT' ignored.

                            < M A T L A B (R) >
                  Copyright 1984-2012 The MathWorks, Inc.
                    R2012b (8.0.0.783) 64-bit (glnxa64)
                              August 22, 2012

 
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.
 
>> >> 
S0 =

        1000

>> 
mu =

    0.0500

>> 
sigma =

    0.1200

>> 
T =

    10

>> 
nb_traj =

   100

>> 
step =

    0.0039

>> >> >> >> >> >> >>




/mnt/glusterfs/bishopj/mattest/run1$ ls -tlr total 1864 -rw-r--r-- 1 bishopj root 191 Feb 19 14:08 run.submit -rw-r--r-- 1 bishopj root 473 Feb 19 14:08 run.m -rw-r--r-- 1 bishopj root 52 Feb 19 14:08 job.error -rw-r--r-- 1 bishopj root 1889160 Feb 19 14:08 mydata.mat -rw-r--r-- 1 bishopj root 635 Feb 19 14:08 job.out </source>

bishopj@corn08:/mnt/glusterfs/bishopj/mattest/run18$ matlab -nodesktop   
Warning: No display specified.  You will not be able to display graphics on the screen.
Warning: No window system found.  Java option 'MWT' ignored.

                                                                  < M A T L A B (R) >
                                                        Copyright 1984-2012 The MathWorks, Inc.
                                                          R2012b (8.0.0.783) 64-bit (glnxa64)
                                                                    August 22, 2012

No window system found.  Java option 'MWT' ignored.
 
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.
 
>> disp mydata
mydata
>> load mydata
>> disp mydata
mydata
>> show mydata
Undefined function 'show' for input arguments of type 'char'.
 
>> whos
  Name         Size               Bytes  Class     Attributes

  S         5101x100            4080800  double              
  t         5101x1                40808  double              

>> whos -file mydata
  Name         Size               Bytes  Class     Attributes

  S         5101x100            4080800  double              
  t         5101x1                40808  double              

>>



bishopj@corn15:/mnt/glusterfs/bishopj/mattest/run18$ cat run.m

S0      = 1000
mu      = 0.090000
sigma   = .12
T       =   20
nb_traj = 100
step    = 1/255

[S, t]  = simplest_montecarlo0( sigma, T, nb_traj, S0, mu, step);

listOfVariables = {'S', 't'};
save('run18/mydata.mat', listOfVariables{:});

mcplot = figure('Color',[0.9412 0.9412 0.9412 ]);
plot(t,S, 'linewidth',2);
axis([t(1) t(end) min(S(:)) max(S(:))]);
xlabel('time');ylabel('value');
title(sprintf('%d trajectories', size(S,2)));
print(mcplot,'-dpng','run18/figure.png')


bishopj@corn15:/mnt/glusterfs/bishopj/mattest/run18$ cat run.submit 

#$ -N run18
#$ -o run18/job.out
#$ -e run18/job.error
#$ -cwd
#$ -S /bin/bash
#$ -l testq=1

/mnt/glusterfs/software/non-free/MATLAB-R2012b/bin/matlab -nodesktop -singleCompThread < run18/run.m
Personal tools
Toolbox
LANGUAGES