On 31/10/11 23:09, Chappman wrote:
to find a method in of creating a n x n array, with probabilities as
the entries of this array .
Here's the tricky part, then I have to create a coding program which
uses the values in this array.
For i=1 to 5;
For j=1 to i-1;
total_prob= (some type of simple formula which refers back
to entries in the array)
end;
end;
I recently programmed something similar in Sage to create random
matrices. What I basically did, is create NumPy arrays and transform
them to Sage matrices to perform algebraic computations. Here some examples:
sage: import numpy as np
sage: n = 5 # where n is the number of row/columns of a square matrix
sage: W = np.empty((n,n), dtype=int) # n x n matrix of intergers
# Example 1: a n x n matrix whose ij elements are all zeros, and
diagonal is one.
sage: for i in range(n):
sage: for j in range(n):
sage: if i==j:
sage: W[i,j]=1 # diagonal
sage: else:
sage: W[i,j]=0
# to see it:
sage: show( matrix(ZZ,W) )
# Example 2: a random matrix whose diagonal is zero, and the ij element
has 50% of probability of bein zero:
sage: S = np.empty((n,n), dtype=int)
sage: c = 0.5 # probability that the ij element is zone, P(S_ij=1)
sage: for i in range(n):
sage: for j in range(n):
sage: if i==j:
sage: S[i,j]=0 # diagonal
sage: else:
sage: if np.random.random()>c
sage: S[i,j]=1
sage: else:
sage: S[i,j]=0
# to see the result
sage: show( matrix(ZZ, S) )
You will see that approximately 50% of the non-diagonal elements are one
(the higher n, the most accurate this 50%)
Note that if you want to have "reproducible" random matrices (for
instance to evaluate the algorith), you should set a seed (with for
example np.random.seed(int(10))), before calling np.random
There are probably more efficient ways to create a matrix, but this is
the one I implemented for my models. Hope you find it useful.
Best
Jose
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org