Function Repository Resource:

RandomSparseSPDMatrixProbNum

Source Notebook

Create a random sparse symmetric positive definite matrix using the Python package ProbNum

Contributed by: Igor Bakshee

ResourceFunction["RandomSparseSPDMatrixProbNum"][n]

creates an nn random sparse symmetric positive definite matrix.

ResourceFunction["RandomSparseSPDMatrixProbNum"][session,]

uses the specified running ExternalSessionObject.

Details and Options

ResourceFunction["RandomSparseSPDMatrixProbNum"] constructs a random matrix from its Cholesky factor in which the diagonal is set to one and all other nonzero entries of the lower triangle are sampled from a uniform distribution with bounds {min,max}.
ResourceFunction["RandomSparseSPDMatrixProbNum"] returns a SparseArray object, a Failure object or $Failed.
The following options can be given:
"CholeskyFactorDensity"0.1density of the off-diagonal entries of the Cholesky factor
“CholeskyFactorRange"{0.1,1.}the min and max values of the off-diagonal elements of the Cholesky factor
RandomSeedingAutomaticseeding of the pseudorandom generator
The value of "CholeskyFactorDensity" must be between 0 and 1 where 1 represents a dense matrix.
RandomSeeding can be an integer, None, Automatic or a numpy object generator, constructed with the resource function PythonObject.
With RandomSeedingAutomatic, ResourceFunction["RandomSparseSPDMatrixProbNum"] reseeds the random number generator every time the function is called.
ResourceFunction["RandomSparseSPDMatrixProbNum"] is essentially a shortcut to creating a random matrix in Python with the resource function ProbNumObject. It can be used when the current Wolfram Language session does not need access to the underlying Python object.

Examples

Basic Examples (3) 

Construct a random symmetric positive definite matrix:

In[1]:=
ResourceFunction["RandomSparseSPDMatrixProbNum"][7]
Out[1]=

Show the matrix:

In[2]:=
Normal[%] // MatrixForm
Out[2]=

Verify that the matrix is symmetric and positive definite:

In[3]:=
SymmetricMatrixQ[%] && PositiveDefiniteMatrixQ[%]
Out[3]=

Scope (2) 

Construct a matrix:

In[4]:=
ResourceFunction["RandomSparseSPDMatrixProbNum"][5]
Out[4]=

Use a the specified Python session:

In[5]:=
session = StartExternalSession["Python"]
Out[5]=
In[6]:=
ResourceFunction["RandomSparseSPDMatrixProbNum"][session, 5]
Out[6]=

Close the session:

In[7]:=
DeleteObject[session]

Options (4) 

CholeskyFactorDensity (1) 

Use the option "CholeskyFactorDensity" to construct a dense matrix:

In[8]:=
ResourceFunction["RandomSparseSPDMatrixProbNum"][5, "CholeskyFactorDensity" -> 1]
Out[8]=
In[9]:=
Normal[%] // MatrixForm
Out[9]=

CholeskyFactorRange (1) 

Create a matrix with larger numerical values using the "CholeskyFactorRange" option:

In[10]:=
ResourceFunction["RandomSparseSPDMatrixProbNum"][5, "CholeskyFactorRange" -> {0.1, 10}]
Out[10]=
In[11]:=
Normal[%] // MatrixForm
Out[11]=

RandomSeeding (2) 

Ensure reproducibility of the results:

In[12]:=
ResourceFunction["RandomSparseSPDMatrixProbNum"][5, RandomSeeding -> 42] // Normal
Out[12]=
In[13]:=
ResourceFunction["RandomSparseSPDMatrixProbNum"][5, RandomSeeding -> 42] // Normal
Out[13]=
In[14]:=
SameQ[%, %%]
Out[14]=

Use the specified random number generator from the Python package numpy for seeding:

In[15]:=
session = StartExternalSession["Python"]
Out[15]=
In[16]:=
ResourceFunction[
  "PythonObject"][session, "numpy.random.default_rng(seed=42)", Initialization -> "import numpy"]
Out[16]=
In[17]:=
ResourceFunction["RandomSparseSPDMatrixProbNum"][session, 5, RandomSeeding -> %] // Normal
Out[17]=
In[18]:=
DeleteObject[session]

Properties and Relations (2) 

A random sparse symmetric positive definite matrix can be constructed with the resource function ProbNumObject:

In[19]:=
p = ResourceFunction["ProbNumObject"][]
Out[19]=
In[20]:=
rng = ResourceFunction["ProbNumObject"][p, "RNG"[42]]
Out[20]=
In[21]:=
p["RandomSparseSPDMatrix"[rng, 5, .1]]
Out[21]=
In[22]:=
m = % // Normal // Normal
Out[22]=

Construct the same matrix with RandomSparseSPDMatrixProbNum:

In[23]:=
ResourceFunction["RandomSparseSPDMatrixProbNum"][5, RandomSeeding -> 42]
Out[23]=
In[24]:=
Normal[%] === m
Out[24]=

Version History

  • 1.0.0 – 26 April 2022

Source Metadata

Related Resources

License Information