ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 28 Aug 2012 17:53:10 -0500Generating random normal vectors and matriceshttp://ask.sagemath.org/question/9274/generating-random-normal-vectors-and-matrices/I'm new to sage, and am looking for the best ways to generate matrices and vectors with the following properties:
- Matrices with independent normal entries. The default distribution of entries in matrices generated by random_matrix seems to be uniform over [-1,1]. Can that be changed?
- Random (normal) vectors with fixed sparsity, in the sense that only a given number of the entries are non-zero.
Thanks for any assistance.Tue, 28 Aug 2012 15:29:21 -0500http://ask.sagemath.org/question/9274/generating-random-normal-vectors-and-matrices/Answer by DSM for <p>I'm new to sage, and am looking for the best ways to generate matrices and vectors with the following properties:</p>
<ul>
<li><p>Matrices with independent normal entries. The default distribution of entries in matrices generated by random_matrix seems to be uniform over [-1,1]. Can that be changed?</p></li>
<li><p>Random (normal) vectors with fixed sparsity, in the sense that only a given number of the entries are non-zero.</p></li>
</ul>
<p>Thanks for any assistance.</p>
http://ask.sagemath.org/question/9274/generating-random-normal-vectors-and-matrices/?answer=13970#post-id-13970From within Sage, I'd probably do something like
sage: m = Matrix(RR, 3, lambda i,j: normalvariate(0, 1))
sage: m
[ -2.62449138547238 0.892440016295533 0.370843911194638]
[-0.556296861517236 0.298136996038704 0.244309907008598]
[ -1.48878357104797 0.424470381797098 -1.02406746759107]
for the first. (See also `numpy.random.normal`, although you'd have to feed it into a `Matrix` to get the full Sage functionality anyway.)
For the second, the best way would depend upon whether you want to specify an exact number of non-zero terms or only an approximate fraction. If exact, then maybe just use `sample` to decide which terms you want to be nonzero and use the `dict` constructor:
sage: sample(xrange(10), 3)
[8, 1, 6]
sage: {index: normalvariate(0, 1) for index in sample(xrange(10), 3)}
{0: 0.6312570531236461, 1: -0.3442153313732221, 7: 0.12271841036659839}
sage: vector(RR, 10, {index: normalvariate(0, 1) for index in sample(xrange(10), 3)})
(0.000000000000000, 0.000000000000000, 0.000000000000000, -2.16336854587694, -0.123030170562214, 0.000000000000000, 0.000000000000000, -1.99437183544239, 0.000000000000000, 0.000000000000000)
[Note that since I'm computing new random numbers each time, the three lines above aren't consistent with each other. But you should get the idea.]
Tue, 28 Aug 2012 17:20:40 -0500http://ask.sagemath.org/question/9274/generating-random-normal-vectors-and-matrices/?answer=13970#post-id-13970Comment by stafford for <p>From within Sage, I'd probably do something like</p>
<pre><code>sage: m = Matrix(RR, 3, lambda i,j: normalvariate(0, 1))
sage: m
[ -2.62449138547238 0.892440016295533 0.370843911194638]
[-0.556296861517236 0.298136996038704 0.244309907008598]
[ -1.48878357104797 0.424470381797098 -1.02406746759107]
</code></pre>
<p>for the first. (See also <code>numpy.random.normal</code>, although you'd have to feed it into a <code>Matrix</code> to get the full Sage functionality anyway.)</p>
<p>For the second, the best way would depend upon whether you want to specify an exact number of non-zero terms or only an approximate fraction. If exact, then maybe just use <code>sample</code> to decide which terms you want to be nonzero and use the <code>dict</code> constructor:</p>
<pre><code>sage: sample(xrange(10), 3)
[8, 1, 6]
sage: {index: normalvariate(0, 1) for index in sample(xrange(10), 3)}
{0: 0.6312570531236461, 1: -0.3442153313732221, 7: 0.12271841036659839}
sage: vector(RR, 10, {index: normalvariate(0, 1) for index in sample(xrange(10), 3)})
(0.000000000000000, 0.000000000000000, 0.000000000000000, -2.16336854587694, -0.123030170562214, 0.000000000000000, 0.000000000000000, -1.99437183544239, 0.000000000000000, 0.000000000000000)
</code></pre>
<p>[Note that since I'm computing new random numbers each time, the three lines above aren't consistent with each other. But you should get the idea.]</p>
http://ask.sagemath.org/question/9274/generating-random-normal-vectors-and-matrices/?comment=19148#post-id-19148Thanks for these suggestions.Tue, 28 Aug 2012 17:53:10 -0500http://ask.sagemath.org/question/9274/generating-random-normal-vectors-and-matrices/?comment=19148#post-id-19148