ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 06 Mar 2015 14:33:37 +0100Random numbers in parallel calculationshttps://ask.sagemath.org/question/26046/random-numbers-in-parallel-calculations/ Assume I have a function that requires random number (noise) like:
@parallel
def foo(i):
print np.random.random()
4 sequential runs yield desired output:
for i in range(4):
foo(i)
0.961718217227
0.909042125122
0.736138778296
0.149902522071
But the parallel run calculates only one random value:
list(foo(range(4)))
0.633760965726
0.633760965726
0.633760965726
0.633760965726
[(((0),{}),None),(((2),{}),None),(((1),{}),None),(((3),{}),None)]
How do I properly generate random values from within parallel function?EugeneFri, 06 Mar 2015 14:33:37 +0100https://ask.sagemath.org/question/26046/Speed comparison with numerical arrays: sage, R, matlabhttps://ask.sagemath.org/question/10671/speed-comparison-with-numerical-arrays-sage-r-matlab/Dear all
I am a sage newbie coming from matlab, R, and Maple, and although I really like many features, I find that my sage code for handling numerical arrays is much more complex and much slower than the corresponding code in R and Matlab.
Here's an example:
# Simuating a sample path of standard Brownian motion
I want to simulate a random path of standard Brownian motion. The input is a vector of time points, and I want returned a (random) trajectory, sampled at these time points.
To do this I need: 1) Fast random number generation 2) Fast array operations: Diff, multiplication, and cumsum. I found that in sage I need to go through NumPy, so I wrote:
sage: import numpy
sage: # General routine to sample a Brownian motion on a time grid
sage: def rB(tvec):
sage: dt = numpy.diff(numpy.array(tvec))
sage: sdt = sqrt(dt)
sage: RNG = RealDistribution('gaussian',1)
sage: dB = sdt * numpy.array([RNG.get_random_element() for j in range(dt.__len__())],dtype=float)
sage: B1 = numpy.array([normalvariate(0,sqrt(tvec[0]))],dtype=float)
sage: B = numpy.cumsum(numpy.concatenate((B1,dB)))
sage: return(B)
I then time it, testing with 1 million time points:
sage: tvec = srange(0,1e3,1e-3)
sage: time B = rB(tvec)
I get a time of 3.5 seconds on my machine, a standard Laptop running Ubuntu, running sage from the shell. Using prun, I see that half of the time, roughly, is spent generating random numbers and the other half mainly constructing numerical arrays.
For comparison, the R code would be (here I assume that tvec[1]=0)
> tvec <- seq(0,1000,1e-3)
> system.time(B <- c(0,cumsum(rnorm(length(tvec)-1,sd=sqrt(diff(tvec))))))
which executes in 0.24 seconds. The matlab code would be
>> tvec = linspace(0,1e3,1e6+1);
>> tic;B = [0,cumsum(randn(1,length(tvec)-1) .* sqrt(diff(tvec)))];toc
which runs in 0.06 seconds.
You see that my sage code is not only clumsier, it is also between 10 and 100 times slower, so I guess I am doing something wrong.
I would really appreciate if you could point me in the direction of cleaner and faster code.
Cheers,
Uffe
Uffe_H_ThygesenMon, 28 Oct 2013 18:52:08 +0100https://ask.sagemath.org/question/10671/Problem calling np.random.multinomial from notebookhttps://ask.sagemath.org/question/8972/problem-calling-nprandommultinomial-from-notebook/According to np.random.multinomial? this is what I should expect
np.random.multinomial(20, [1/6.]*6, size=2)
array([[3, 4, 3, 3, 4, 3],
[2, 4, 3, 4, 0, 7]])
However when I type the same thing in notebook I get an error. The same command works fine in my system python. In sage notebook it works when I drop the argument size=2. This seems to be problem with sage notebook. Any suggestions?
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_48.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("aW1wb3J0IG51bXB5IGFzIG5wCm5wLnJhbmRvbS5tdWx0aW5vbWlhbCgxMDAsIFsxLzIuMF0qMixzaXplPTMp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/tmp/tmpARgtW7/___code___.py", line 4, in <module>
exec compile(u'np.random.multinomial(_sage_const_100 , [_sage_const_1 /_sage_const_2p0 ]*_sage_const_2 ,size=_sage_const_3 )
File "", line 1, in <module>
File "mtrand.pyx", line 4050, in mtrand.RandomState.multinomial (numpy/random/mtrand/mtrand.c:17809)
File "element.pyx", line 1305, in sage.structure.element.RingElement.__add__ (sage/structure/element.c:11569)
File "coerce.pyx", line 797, in sage.structure.coerce.CoercionModel_cache_maps.bin_op (sage/structure/coerce.c:7467)
TypeError: unsupported operand parent(s) for '+': 'Integer Ring' and '<type 'tuple'>'
ShashankTue, 15 May 2012 19:37:13 +0200https://ask.sagemath.org/question/8972/