What is Projection?#

Definition of Projection in R2#

The projection of a vector b onto another vector a is the component of b that lies along the direction of a. This projection represents the closest vector along the line defined by a to the vector b. The projection minimizes the difference between b and its projection, making the residual orthogonal to a. The formula for the projection of b onto a is:

Proja(b)=aTbaTaa

Geometric Implementation

In this formula:

  • aTb represents the dot product of b and a, which measures how much b aligns with a.

  • aTa represents the magnitude (length) of vector a, ensuring that the projection accounts for the size of a.

  • The result is a scaled version of a that points in the same direction as a, but has a magnitude such that the difference between b and its projection is minimized.

In summary, projection is the transformation that gives the closest vector along the direction of a to b, ensuring orthogonality between the residual and a.

import numpy as np
import matplotlib.pyplot as plt
import math

# line b
b = np.array([ 4,1 ])

# line a
a = np.array([ 2, 5 ])

# beta
beta = (a.T@b) / (a.T@a)

# draw!
plt.plot([0, b[0]],[0, b[1]],'r',label='b')
plt.plot([0, a[0]],[0, a[1]],'b',label='a')

# now plot projection line
plt.plot([b[0], beta*a[0]],[b[1], beta*a[1]],'--',label=r'b-$\beta$a')
plt.axis('square')
plt.grid()
plt.legend()
plt.axis((-6, 6, -6, 6))

plt.show()
../_images/361ae4352228b938d5630f2fcdf2b01bf0c0a72b52935b862160a13c947ba821.png

Projection Derivation#

  1. Start with the condition for projection:

aT(baβ)=0

This states that the vector baβ, which represents the error between band its projection onto a, must be orthogonal to a.

  1. Expand and rearrange:

aTbaTaβ=0
  1. Solve for β:

aTaβ=aTb
β=aTbaTa

Thus, the scalar β is the factor by which a is scaled to give the projection of b onto a.

Finally, the projection of b onto a is given by:

Proja(b)=βa=aTbaTaa

notice : βa is as close as possible to point b withuot living the line a.

Projection methods in R2 (vector to vector)#

Projection of Vector b=[4,1] onto a=[1,4]#

Method 1: Projection onto a Line#

The projection of b onto a line defined by a is calculated as:

Proja(b)=(baaa)a

Steps:

  1. Dot product of ba:

    ba=(4)(1)+(1)(4)=4+4=8

    This step finds the magnitude of the projection of b in the direction of a.

  2. Dot product of aa:

    aa=(1)(1)+(4)(4)=1+16=17

    This calculates the magnitude of vector a, which helps in scaling the projection correctly.

  3. Scalar β:

    β=817

    The ratio of the dot products gives the scalar that determines how much of a is in the direction of b.

  4. Final projection:

    Proja(b)=817×[1,4]=[817,3217][0.47,1.88]

    The vector a is scaled by β to get the projection.

Method 2: Projection Using a Unit Vector#

In this method, we first normalize a to create a unit vector, then calculate the projection.

Steps:

  1. Normalize a:

    a=(1)2+(4)2=17,u^=117[1,4]

    This step ensures that the projection is independent of the magnitude of a, focusing only on the direction.

  2. Dot product of bu^:

    bu^=4×117+1×417=817

    This calculates the component of b in the direction of the unit vector u^.

  3. Final projection:

    Proja(b)=817×[117,417]=[817,3217][0.47,1.88]

    The projection is obtained by scaling the unit vector u^.

Method 3: Projection as a Linear Transformation#

Since projections are linear transformations, they can also be expressed as a matrix-vector multiplication.

Steps:

  1. Unit vector u^: From Method 2, the unit vector is:

    u^=[117,417]
  2. Construct the projection matrix A:

    A=[u12u1u2u1u2u22]=[1174174171617]

    This matrix represents the transformation that projects any vector onto the line defined by u^.

  3. Matrix multiplication:

    Ab=[1174174171617][41]=[8173217][0.47,1.88]

    The projection is found by multiplying the matrix A by b.

Final Result#

For all three methods, the projection of b=[4,1] onto a=[1,4] is:

Proja(b)=[817,3217][0.47,1.88]

These methods illustrate different approaches to the same problem, each confirming the correctness of the projection result.

Projection methods in R2 implementation in python#

import numpy as np
import matplotlib.pyplot as plt

# Method1
def projection_onto_line(b, a):
    dot_product_b_a = np.dot(b, a)
    dot_product_a_a = np.dot(a, a)
    beta = dot_product_b_a / dot_product_a_a
    proj = beta * a
    return proj

# Merhod2
def projection_using_unit_vector(b, a):
    norm_a = np.linalg.norm(a)
    u_hat = a / norm_a
    dot_product_b_u_hat = np.dot(b, u_hat)
    proj = dot_product_b_u_hat * u_hat
    return proj

# Method3
def projection_as_matrix_product(b, a):
    norm_a = np.linalg.norm(a)
    u_hat = a / norm_a
    A = np.array([[u_hat[0]**2, u_hat[0]*u_hat[1]],
                  [u_hat[0]*u_hat[1], u_hat[1]**2]])
    proj = A @ b
    return proj

def plot_projection(b, a, proj, method_name, ax):
    # Plot original vectors and projection
    ax.quiver(0, 0, b[0], b[1], angles='xy', scale_units='xy', scale=1, color='blue', label='Vector b', linewidth=2)
    ax.quiver(0, 0, a[0], a[1], angles='xy', scale_units='xy', scale=1, color='red', label='Vector a', linewidth=2)
    ax.quiver(0, 0, proj[0], proj[1], angles='xy', scale_units='xy', scale=1, color='green', label=f'Projection ({method_name})', linewidth=2, alpha=0.7)

    
    # Plot projection line
    ax.plot([b[0], proj[0]], [b[1], proj[1]], color='orange', linestyle='--', linewidth=1.5, alpha=0.5, label='b to Projection')

    # Set limits and grid
    ax.set_xlim(-1, 5)
    ax.set_ylim(-1, 5)
    ax.grid()
    ax.axhline(0, color='black', linewidth=0.5, ls='--')
    ax.axvline(0, color='black', linewidth=0.5, ls='--')
    ax.set_aspect('equal', adjustable='box')
    ax.set_title(f'Projection of Vector b onto Vector a - {method_name}')
    ax.legend()

# Define the vectors
b = np.array([4, 1])  # Vector b
a = np.array([1, 4])  # Vector a

# Calculate projections
proj_line = projection_onto_line(b, a)
proj_unit_vector = projection_using_unit_vector(b, a)
proj_matrix_product = projection_as_matrix_product(b, a)

# Create a figure with subplots in one row
fig, axs = plt.subplots(1, 3, figsize=(18, 6))

# Plotting the projections
plot_projection(b, a, proj_line, "Line Projection", axs[0])
plot_projection(b, a, proj_unit_vector, "Unit Vector Projection", axs[1])
plot_projection(b, a, proj_matrix_product, "Matrix-Vector Product Projection", axs[2])

plt.tight_layout()
plt.show()


print(f"Method 1: {proj_line.round(2)}")
print(f"Method 2: {proj_unit_vector.round(2)}")
print(f"Method 3: {proj_matrix_product.round(2)}")
../_images/a50e34506599c57e5dae20b9e2cc0d62002371985d2f00dd6e026e150e583765.png
Method 1: [0.47 1.88]
Method 2: [0.47 1.88]
Method 3: [0.47 1.88]

Projection methods in R3 (Vector to Vector)#

Projection of Vector b=[4,1,2] onto a=[1,4,3]#

Method 1: Projection onto a Line#

The projection of b onto a is calculated as:

Proja(b)=(baaa)a

Steps:#

  1. Dot product ba:

ba=(4)(1)+(1)(4)+(2)(3)=4+4+6=14
  1. Dot product $aa$:

aa=(1)(1)+(4)(4)+(3)(3)=1+16+9=26
  1. Calculate scalar β:

β=1426=713
  1. Final projection:

Proja(b)=713[1,4,3]=[713,2813,2113][0.54,2.15,1.62]

Method 2: Projection Using a Unit Vector#

  1. Normalize a:

a=(1)2+(4)2+(3)2=1+16+9=26
u^=126[1,4,3]
  1. Dot product bu^:

bu^=4126+1426+2326=4+4+626=1426
  1. Final projection:

Proja(b)=(1426)(126,426,326)=[1426,5626,4226]=[713,2813,2113][0.54,2.15,1.62]

Method 3: Projection as a Matrix-Vector Product#

  1. Unit vector u^ (as calculated earlier):

u^=[126,426,326]
  1. Construct the projection matrix ( A ):

A=[u12u1u2u1u3u1u2u22u2u3u1u3u2u3u32]=[(126)2(126)(426)(126)(326)(426)(126)(426)2(426)(326)(326)(126)(326)(426)(326)2]
  1. Matrix multiplication:

Ab=A[412]=[4+4+62616+16+242612+12+1826]=[142656264226]=[71328132113][0.54,2.15,1.62]

Final Result#

For all three methods, the projection of b=[4,1,2] onto a=[1,4,3] is:

Proja(b)=[713,2813,2113][0.54,2.15,1.62]

These methods confirm the correctness of the projection result in R3.

Projection methods in R3 implementation in python (Vector to Vector)#

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Define the vectors
b = np.array([4, 1, 2])
a = np.array([1, 4, 3])

# Method 1: Projection onto a Line
def projection_line(b, a):
    return (np.dot(b, a) / np.dot(a, a)) * a

proj_line = projection_line(b, a)

# Method 2: Projection Using a Unit Vector
def projection_unit(b, a):
    u = a / np.linalg.norm(a)  # Normalize a
    return np.dot(b, u) * u

proj_unit = projection_unit(b, a)

# Method 3: Projection as a Matrix-Vector Product
def projection_matrix(b, a):
    u = a / np.linalg.norm(a)  # Normalize a
    projection_matrix = np.outer(u, u)  # Outer product to form the matrix
    return projection_matrix @ b  # Matrix-vector multiplication

proj_matrix = projection_matrix(b, a)

# Prepare the figure
fig = plt.figure(figsize=(15, 5))

# Plot Method 1: Direct Projection
ax1 = fig.add_subplot(131, projection='3d')
ax1.quiver(0, 0, 0, b[0], b[1], b[2], color='r', label='Vector b', arrow_length_ratio=0.1)
ax1.quiver(0, 0, 0, a[0], a[1], a[2], color='g', label='Vector a', arrow_length_ratio=0.1)
ax1.quiver(0, 0, 0, proj_line[0], proj_line[1], proj_line[2], color='b', label='Projection', arrow_length_ratio=0.1)

# Dashed line from b to the projection point
ax1.plot([b[0], proj_line[0]], [b[1], proj_line[1]], [b[2], proj_line[2]], 'k--', label='Line to Projection')

ax1.set_xlim([-1, 5])
ax1.set_ylim([-1, 5])
ax1.set_zlim([-1, 5])
ax1.set_title('Projection onto a Line')
ax1.set_xlabel('X axis')
ax1.set_ylabel('Y axis')
ax1.set_zlabel('Z axis')
ax1.legend()

# Plot Method 2: Using a Unit Vector
ax2 = fig.add_subplot(132, projection='3d')
ax2.quiver(0, 0, 0, b[0], b[1], b[2], color='r', label='Vector b', arrow_length_ratio=0.1)
ax2.quiver(0, 0, 0, a[0], a[1], a[2], color='g', label='Vector a', arrow_length_ratio=0.1)
ax2.quiver(0, 0, 0, proj_unit[0], proj_unit[1], proj_unit[2], color='b', label='Projection', arrow_length_ratio=0.1)

# Dashed line from b to the projection point
ax2.plot([b[0], proj_unit[0]], [b[1], proj_unit[1]], [b[2], proj_unit[2]], 'k--', label='Line to Projection')

ax2.set_xlim([-1, 5])
ax2.set_ylim([-1, 5])
ax2.set_zlim([-1, 5])
ax2.set_title('Projection using a Unit Vector')
ax2.set_xlabel('X axis')
ax2.set_ylabel('Y axis')
ax2.set_zlabel('Z axis')
ax2.legend()

# Plot Method 3: Matrix-Vector Product
ax3 = fig.add_subplot(133, projection='3d')
ax3.quiver(0, 0, 0, b[0], b[1], b[2], color='r', label='Vector b', arrow_length_ratio=0.1)
ax3.quiver(0, 0, 0, a[0], a[1], a[2], color='g', label='Vector a', arrow_length_ratio=0.1)
ax3.quiver(0, 0, 0, proj_matrix[0], proj_matrix[1], proj_matrix[2], color='b', label='Projection', arrow_length_ratio=0.1)

# Dashed line from b to the projection point
ax3.plot([b[0], proj_matrix[0]], [b[1], proj_matrix[1]], [b[2], proj_matrix[2]], 'k--', label='Line to Projection')

ax3.set_xlim([-1, 5])
ax3.set_ylim([-1, 5])
ax3.set_zlim([-1, 5])
ax3.set_title('Projection using Matrix-Vector Product')
ax3.set_xlabel('X axis')
ax3.set_ylabel('Y axis')
ax3.set_zlabel('Z axis')
ax3.legend()

# Show the plots
plt.tight_layout()
plt.show()
../_images/fe5618bdd8230d572a3c973122e79a4be9629ceee21a5d9f07cb464ca797a568.png

Definition of Projection in Rn#

The projection of a vector bRn onto the column space of a matrix ARn×m is the vector in the column space of A that is closest to b in terms of Euclidean distance. This projection minimizes the difference between b and the vector Ax, where xRm is the solution to the least squares problem.

Mathematical Expression:#

The projection of b onto the column space of A is given by:

ProjA(b)=A(ATA)1ATb

where:

  • ATA is the Gram matrix, representing the inner products of the columns of A,

  • (ATA)1 is the inverse of the Gram matrix (if it exists),

  • ATb is the projection of b onto the rows of A.

Geometric Interpretation:#

The resulting vector ProjA(b) lies in the column space of A, meaning it can be expressed as a linear combination of the columns of A. This projection ensures that the residual vector bProjA(b) is orthogonal to the column space of A.

Key Characteristics:#

  • The projection minimizes the distance between b and the column space of A.

  • The residual bAx is orthogonal to the column space of A.

  • This method is used in solving least squares problems, where the system Ax=b may have no exact solution due to being overdetermined (more equations than unknowns).

Applications:#

  • Least squares regression, fitting a model to data points.

  • Dimensionality reduction, projecting high-dimensional data into a lower-dimensional subspace.

References#