API Reference
l2
L2
A class representing the L2-space of random variables defined on a given probability space.
basis
property
basis
Get the vector space basis of the L2-space.
The basis vectors of the L2-space are the indicator functions of the atoms in the sigma algebra with nonzero probability, each scaled by the reciprocal of the square root of the atom's probability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tol
|
float
|
The tolerance below which an atom is deemed to have probability 0. |
1e-8
|
Returns:
| Name | Type | Description |
|---|---|---|
basis |
dict[str, RandomVariable]
|
A dictionary mapping the name of each basis vector to the corresponding basis vector of the L2-space. |
Examples:
>>> from sigalg.core import ProbabilityMeasure, SampleSpace, SigmaAlgebra
>>> from sigalg.l2 import L2
>>> Omega = SampleSpace().from_sequence(size=3)
>>> F = SigmaAlgebra(sample_space=Omega).from_dict({0: 0, 1: 0, 2: 1})
>>> # A probability measure assigning nonzero probability to all atoms
>>> P = ProbabilityMeasure(sample_space=Omega).from_dict({0: 0.2, 1: 0.5, 2: 0.3})
>>> H = L2(
... sample_space=Omega,
... sigma_algebra=F,
... probability_measure=P,
... )
>>> V_0, V_1 = H.basis.values()
>>> V_0
Random variable 'V_0':
V_0
sample
0 1.195229
1 1.195229
2 0.000000
>>> # A probability measure assigning zero probability to one atom
>>> Q = ProbabilityMeasure(sample_space=Omega).from_dict({0:0.2, 1:0.8, 2:0})
>>> G = L2(
... sample_space=Omega,
... sigma_algebra=F,
... probability_measure=Q,
... name="G"
... )
>>> G.basis
{'V_0': Random variable 'V_0':
V_0
sample
0 1.0
1 1.0
2 0.0}
name
property
writable
name
The name of the L2-space.
Returns:
| Name | Type | Description |
|---|---|---|
name |
Hashable
|
The name of the L2-space. |
probability_measure
property
probability_measure
The probability measure on which the L2-space is defined.
Returns:
| Name | Type | Description |
|---|---|---|
probability_measure |
ProbabilityMeasure
|
The probability measure on which the L2-space is defined. |
probability_space
property
probability_space
The probability space on which the L2-space is defined.
Returns:
| Name | Type | Description |
|---|---|---|
prob_space |
ProbabilitySpace
|
The probability space on which the L2-space is defined. |
sample_space
property
sample_space
The sample space on which the L2-space is defined.
Returns:
| Name | Type | Description |
|---|---|---|
sample_space |
SampleSpace
|
The sample space on which the L2-space is defined. |
sigma_algebra
property
sigma_algebra
The sigma-algebra on which the L2-space is defined.
Returns:
| Name | Type | Description |
|---|---|---|
sigma_algebra |
SigmaAlgebra
|
The sigma-algebra on which the L2-space is defined. |
distance
distance(first, second)
Compute the distance between two random variables in the L2-space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
first
|
RandomVariable
|
The first random variable. |
required |
second
|
RandomVariable
|
The second random variable. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If one of the two random variables is not in the L2-space. |
Returns:
| Name | Type | Description |
|---|---|---|
distance |
Real
|
The distance between the two random variables in the L2-space. |
fourier_coefficients
fourier_coefficients(rv)
Compute the Fourier coefficients of a random variable with respect to the basis of the L2-space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rv
|
RandomVariable
|
The random variable whose Fourier coefficients are to be computed. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Returns:
| Name | Type | Description |
|---|---|---|
coefficients |
dict[str, Real]
|
A dictionary mapping the name of each basis vector to the corresponding Fourier coefficient of |
Examples:
>>> from sigalg.core import ProbabilityMeasure, RandomVariable, SampleSpace, SigmaAlgebra
>>> from sigalg.l2 import L2
>>> Omega = SampleSpace().from_sequence(size=3)
>>> F = SigmaAlgebra(sample_space=Omega).from_dict({0: 0, 1: 0, 2: 1})
>>> P = ProbabilityMeasure(sample_space=Omega).from_dict({0: 0.2, 1: 0.5, 2: 0.3})
>>> H = L2(
... sample_space=Omega,
... sigma_algebra=F,
... probability_measure=P,
... )
>>> # Get the Fourier coefficients of X with respect to the basis of H
>>> X = RandomVariable(domain=Omega, name="X").from_dict({0: 2, 1: 2, 2: 3})
>>> coeffs = H.fourier_coefficients(rv=X)
>>> coeffs
{'V_0': np.float64(1.6733200530681511), 'V_1': np.float64(1.6431676725154982)}
>>> # Reconstruct X from its Fourier coefficients and the basis of H
>>> sum(coeffs[basis_name] * basis_vec for basis_name, basis_vec in H.basis.items()).with_name("X_reconstructed")
Random variable 'X_reconstructed':
X_reconstructed
sample
0 2.0
1 2.0
2 3.0
>>> # Define a new probability measure Q that assigns zero probability to an atom in the sigma algebra, and define a new L2-space
>>> Q = ProbabilityMeasure(sample_space=Omega).from_dict({0: 0.2, 1: 0.8, 2: 0.0})
>>> K = L2(
... sample_space=Omega,
... sigma_algebra=F,
... probability_measure=Q,
... )
>>> # Compute the Fourier coefficients of X with respect to the basis of K, and note that there is only one coefficient
>>> K.fourier_coefficients(rv=X)
{'V_0': np.float64(2.0)}
>>> # Reconstruct X from its Fourier coefficients and the basis of K, and note that the reconstruction differs from the original X on a set of probability zero
>>> (2.0 * K.basis["V_0"]).with_name("X_reconstructed")
Random variable 'X_reconstructed':
X_reconstructed
sample
0 2.0
1 2.0
2 0.0
inner
inner(first, second)
Compute the inner product of two random variables.
Both random variables must be in the L2-space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
first
|
RandomVariable
|
The first random variable. |
required |
second
|
RandomVariable
|
The second random variable. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If one of the random variables is not in the L2-space. |
Returns:
| Name | Type | Description |
|---|---|---|
inner_product |
Real
|
The inner product of the two random variables. |
Examples:
>>> from sigalg.core import ProbabilityMeasure, RandomVariable, SampleSpace, SigmaAlgebra
>>> from sigalg.l2 import L2
>>> Omega = SampleSpace().from_sequence(size=3)
>>> F = SigmaAlgebra(sample_space=Omega).from_dict({0: 0, 1: 0, 2: 1})
>>> P = ProbabilityMeasure(sample_space=Omega).from_dict({0: 0.2, 1: 0.5, 2: 0.3})
>>> H = L2(
... sample_space=Omega,
... sigma_algebra=F,
... probability_measure=P,
... )
>>> X = RandomVariable(domain=Omega, name="X").from_dict({0: 1, 1: 1, 2: 3})
>>> Y = RandomVariable(domain=Omega, name="Y").from_dict({0: 4, 1: 4, 2: 6})
>>> float(H.inner(X, Y))
8.2
>>> # Example of orthogonal RVs: two indicator functions of disjoint events
>>> A, B = F.to_atoms()
>>> I_A = RandomVariable.indicator_of(A)
>>> I_B = RandomVariable.indicator_of(B)
>>> float(H.inner(I_A, I_B))
0.0
integrate
integrate(rv)
Integrate a random variable with respect to the probability measure of the L2-space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rv
|
RandomVariable
|
The random variable to be integrated. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Returns:
| Name | Type | Description |
|---|---|---|
integral |
Real
|
The integral of the random variable with respect to the probability measure of the L2-space. |
Examples:
>>> from sigalg.core import ProbabilityMeasure, RandomVariable, SampleSpace, SigmaAlgebra
>>> from sigalg.l2 import L2
>>> Omega = SampleSpace().from_sequence(size=3)
>>> F = SigmaAlgebra(sample_space=Omega).from_dict({0: 0, 1: 0, 2: 1})
>>> P = ProbabilityMeasure(sample_space=Omega).from_dict({0: 0.2, 1: 0.5, 2: 0.3})
>>> H = L2(
... sample_space=Omega,
... sigma_algebra=F,
... probability_measure=P,
... )
>>> X = RandomVariable(domain=Omega, name="X").from_dict({0: 1, 1: 1, 2: 3})
>>> float(round(H.integrate(X), 2))
1.6
norm
norm(X)
Compute the norm of a random variable in the L2-space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
RandomVariable
|
The random variable whose norm is to be computed. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
The random variable must be in the L2-space. |
Returns:
| Name | Type | Description |
|---|---|---|
norm |
Real
|
The norm of the random variable in the L2-space. |
Examples:
>>> from sigalg.core import ProbabilityMeasure, RandomVariable, SampleSpace, SigmaAlgebra
>>> from sigalg.l2 import L2
>>> Omega = SampleSpace().from_sequence(size=3)
>>> F = SigmaAlgebra(sample_space=Omega).from_dict({0: 0, 1: 0, 2: 1})
>>> P = ProbabilityMeasure(sample_space=Omega).from_dict({0: 0.2, 1: 0.5, 2: 0.3})
>>> H = L2(
... sample_space=Omega,
... sigma_algebra=F,
... probability_measure=P,
... )
>>> A, _ = F.to_atoms()
>>> I_A = RandomVariable.indicator_of(A)
>>> # The squared norm of an indicator function is the probability of the corresponding event
>>> float(round(H.norm(I_A) ** 2, 1))
0.7