dimanche 30 octobre 2022

Why does the scatter plot of calculated determinants and traces using uniformly distributed inputs not look as expected?

I'm trying to find the probability for a linear system to be various types of stability, where the system is given by the following matrix:

[[a,b],
[c,d]]

And a,b,c,d are chosen from a random uniform distribution from a domain of [-1,1]. The primary graph of interest is trace vs. determinant, ie. (a+d) vs. (a*d-c*b)

Plot of stability classification

In my code I generate random matrices using numpy.random.uniform(-1,1,size(2,2)). The trace and determinant then have ranges of [-2,2]. Here is the code I used:

import numpy as np
import matplotlib.pyplot as plt

det = [] # determinant
tr = [] # trace
i=0

while i < 100000:
    linsys = np.random.uniform(-1, 1, size=(2,2))
    tr.append(linsys[0][0]+linsys[1][1])
    det.append(linsys[0][0]*linsys[1][1]-linsys[1][0]*linsys[0][1])
    i += 1
    
print(np.max(tr),np.max(det),np.min(tr),np.min(det))
    
plt.scatter(det,tr)
plt.title("Plot of Trace v Determinant")
plt.xlabel("a*d-c*b")
plt.ylabel("a+d")
#plt.scatter(np.abs(det),2*np.sqrt(np.abs(det)))
#plt.scatter(np.abs(det),-2*np.sqrt(np.abs(det)))

fig1, ax1 = plt.subplots(figsize =(6, 4))
ax1.hist(det, bins = [-3,-2,-1,0,1,2,3])
plt.title('Histogram of trace')
fig2, ax2 = plt.subplots(figsize =(6, 4))
ax2.hist(trace, bins = [-3,-2,-1,0,1,2,3])
plt.title('Histogram of determinant')

I'd expect the graph of trace vs. determinant to be completely uniform with no weird shapes but instead I find this very strange shape when plotting them:

Plot of trace v determinant

And these are the distributions of the trace and determinant:

![Distributions of trace and determinant

Again, since all the variables a,b,c,d have the same domain [-1,1] I'd expect the sum of them or products of them to still have a uniform distribution. I'm wondering then why there's this strange shape in the scatter plot of trace v determinant then? Maybe I am defining the uniformly random matrix incorrectly?




Aucun commentaire:

Enregistrer un commentaire