MCPcopy Create free account
hub / github.com/MAC-VO/MAC-VO / plot_gaussian_conf

Function plot_gaussian_conf

Utility/Plot/PlotAxes.py:227–261  ·  view source on GitHub ↗

Given a 2x2 covariance matrix, plot the ellipse representing confidence interval of the designated confidence value. Confidence in range of [0, 1]

(ax: Axes, mean: torch.Tensor | np.ndarray, cov_matrix: torch.Tensor | np.ndarray, confidence: float)

Source from the content-addressed store, hash-verified

225@as_chain_func
226@IgnoreException
227def plot_gaussian_conf(ax: Axes, mean: torch.Tensor | np.ndarray, cov_matrix: torch.Tensor | np.ndarray, confidence: float) -> Axes:
228 """
229 Given a 2x2 covariance matrix, plot the ellipse representing confidence interval of the designated confidence
230 value. Confidence in range of [0, 1]
231 """
232 assert len(cov_matrix.shape) == 2 and cov_matrix.shape[0] == 2 and cov_matrix.shape[1] == 2
233 assert mean.shape[0] == 2 and len(mean.shape) == 1
234
235 if isinstance(mean, torch.Tensor):
236 mean = mean.cpu().numpy()
237 if isinstance(cov_matrix, torch.Tensor):
238 cov_matrix = cov_matrix.cpu().numpy()
239
240 # Calculate eigenvalues and eigenvectors
241 eigenvalues, eigenvectors = np.linalg.eig(cov_matrix)
242
243 # Sort eigenvalues and eigenvectors
244 sort_indices = np.argsort(eigenvalues)[::-1]
245 eigenvalues = eigenvalues[sort_indices]
246 eigenvectors = eigenvectors[:, sort_indices]
247
248 # Calculate scaling factor
249 chisquare_val = chi2.ppf(confidence, df=2)
250 scaling_factor = np.sqrt(chisquare_val)
251
252 # Calculate ellipse parameters
253 width, height = 2 * scaling_factor * np.sqrt(eigenvalues)
254 angle = np.arctan2(eigenvectors[1, 0], eigenvectors[0, 0])
255
256 # Create and plot ellipse
257 ellipse = Ellipse((mean[0], mean[1]), width, height, angle=np.degrees(angle),
258 facecolor='none', edgecolor='red')
259
260 ax.add_patch(ellipse)
261 return ax
262
263@as_chain_func
264@IgnoreException

Callers 1

simulate_cov_modelFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected