-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhutchinson_trace_estimator.py
48 lines (36 loc) · 1.31 KB
/
hutchinson_trace_estimator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import numpy as np
def hutchinson_trace_estimator(matrix_vector_product, n, num_samples=100):
"""
Estimates the trace of matrix A using Hutchinson's trace estimator.
Parameters:
matrix_vector_product: callable
Function that computes Ax given vector x
n: int
Dimension of the matrix
num_samples: int
Number of random vectors to use for estimation
Returns:
float: Estimated trace of the matrix
"""
trace_estimates = []
for _ in range(num_samples):
# Generate random vector with ±1 entries (Rademacher distribution)
v = np.random.choice([-1, 1], size=n)
# Compute v^T * A * v
Av = matrix_vector_product(v)
estimate = np.dot(v, Av)
trace_estimates.append(estimate)
# Return mean of estimates
return np.mean(trace_estimates)
# Example with a known matrix (just for demonstration)
def example():
# Create a test matrix
A = np.array([[1, 2], [3, 4]])
# Define the matrix-vector product function
def mv_product(x):
return A @ x
# Estimate the trace
estimated_trace = hutchinson_trace_estimator(mv_product, n=len(A), num_samples=1000)
print(f"True trace: {np.trace(A)}")
print(f"Estimated trace: {estimated_trace}")
example()