Randomized Algorithms


 Randomized Algorithms: An algorithm that uses random numbers to decide what to do next anywhere in its logic is called a Randomized Algorithm

For example, in Randomized Quick Sort, we use a random number to pick the next pivot (or we randomly shuffle the array)

 Type of Randomized Algorithms

1.  Las Vegas: A Las Vegas algorithm is an algorithm which uses randomness, but gives guarantees that the solution obtained for given problem is correct.

Advantage:  Always produce a correct answer. Randomness affects the running time, not correctness. Expected running time is finite and analyzed probabilistically. 

 A randomized quick-sort algorithm where we randomly choose pivot is an example of Las-Vegas algorithm. The algorithm always sorts the array. The advantage we get with randomization is, there is no pattern for which the quick sort causes worst case (unlike choosing fixed pivot like a corner element causes worst case for sorted input)

Disadvantage: Time complexity of these algorithms is based on a random value and time complexity is evaluated as expected value. 

Quick Short: This is a method to partition the array in place such that all elements to the left of the pivot element are smaller, while all elements to the right of the pivot are greater than the pivot. Then we recursively call the same procedure for left and right subarrays. 

There are two types of algorithm for random pivoting.  Lomuto Partitioning  and Hoare Partitioning.

Algorithm for random pivoting using Lomuto Partitioning 

partition(arr[], lo, hi) 
pivot = arr[hi]
i = lo // place for swapping
for j := lo to hi – 1 do
if arr[j] <= pivot then
swap arr[i] with arr[j]
i = i + 1
swap arr[i] with arr[hi]
return i
partition_r(arr[], lo, hi)
r = Random Number from lo to hi
Swap arr[r] and arr[hi]
return partition(arr, lo, hi)
quicksort(arr[], lo, hi)
if lo < hi
p = partition_r(arr, lo, hi)
quicksort(arr, lo , p-1)
quicksort(arr, p+1, hi)
 
========================================================================================
# Python implementation QuickSort using 
# Lomuto's partition Scheme.
 
import random

'''
The function which implements QuickSort.
arr :- array to be sorted.
start :- starting index of the array.
stop :- ending index of the array.

'''
def quicksort(arr, start , stop):
if(start < stop):

# pivotindex is the index where the pivot lies in the array
pivotindex = partitionrand(arr,\
start, stop)

# At this stage the array is partially sorted around the pivot. 
        # Separately sorting the left half of the array and the right 
        # half of the array.
quicksort(arr , start , pivotindex-1)
quicksort(arr, pivotindex + 1, stop)

# This function generates random pivot swaps the first element with the pivot
# and calls the partition function.

def partitionrand(arr , start, stop):

# Generating a random number between the starting index of the array 
# and the ending index of the array.
randpivot = random.randrange(start, stop)

# Swapping the starting element of the array and the pivot
arr[start], arr[randpivot] = \
arr[randpivot], arr[start]
return partition(arr, start, stop)

'''
This function takes the first element as pivot,
places the pivot element at the correct position
in the sorted array. All the elements are re-arranged
according to the pivot, the elements smaller than the
pivot is places on the left and the elements
greater than the pivot is placed to the right of pivot.
'''

def partition(arr,start,stop):
pivot = start # pivot

# a variable to memorize where the
i = start + 1

# partition in the array starts from.
for j in range(start + 1, stop + 1):

# if the current element is smaller
# or equal to pivot, shift it to the
# left side of the partition.

if arr[j] <= arr[pivot]:
arr[i] , arr[j] = arr[j] , arr[i]
i = i + 1
arr[pivot] , arr[i - 1] =\
arr[i - 1] , arr[pivot]
pivot = i - 1
return (pivot)

# Driver Code
if __name__ == "__main__":
array = [11, 8, 9, 6, 2, 7]
quicksort(array, 0, len(array) - 1)
print(array) 

======================================================================= 

Algorithm for random pivoting using using Hoare's partition Scheme. 
partition(arr[], lo, hi)
pivot = arr[lo]
i = lo - 1 // Initialize left index
j = hi + 1 // Initialize right index

while(True)
// Find a value in left side greater than pivot
do
i = i + 1
while arr[i] < pivot
// Find a value in right side smaller than pivot
do
j = j - 1
while arr[j] > pivot

if i >= j then
return j
else
swap arr[i] with arr[j]
end while

partition_r(arr[], lo, hi)
r = Random number from lo to hi
Swap arr[r] and arr[lo]
return partition(arr, lo, hi)
quicksort(arr[], lo, hi)
if lo < hi
p = partition_r(arr, lo, hi)
quicksort(arr, lo, p)
quicksort(arr, p+1, hi)
 ========================================================================================

 # Python implementation QuickSort using Hoare's partition Scheme.

import random

'''
The function which implements randomised
QuickSort, using Haore's partition scheme.
arr :- array to be sorted.
start :- starting index of the array.
stop :- ending index of the array.

'''
def quicksort(arr, start, stop):
    if(start < stop):
        
        # pivotindex is the index where
        # the pivot lies in the array

        pivotindex = partitionrand(arr,\
                              start, stop)
        
        # At this stage the array is 
        # partially sorted around the pivot. 
        # separately sorting the left half of 
        # the array and the right
        # half of the array.

        quicksort(arr , start , pivotindex)
        quicksort(arr, pivotindex + 1, stop)

# This function generates random pivot,
# swaps the first element with the pivot
# and calls the partition function.

def partitionrand(arr , start, stop):

    # Generating a random number between 
    # the starting index of the array and 
    # the ending index of the array.

    randpivot = random.randrange(start, stop)

    # Swapping the starting element of 
    # the array and the pivot

    arr[start], arr[randpivot] =\
        arr[randpivot], arr[start]
    return partition(arr, start, stop)

'''
This function takes the first element
as pivot, places the pivot element at
the correct position in the sorted array.
All the elements are re-arranged according
to the pivot, the elements smaller than 
the pivot is places on the left and 
the elements greater than the pivot is
placed to the right of pivot.

'''
def partition(arr,start,stop):
    pivot = start # pivot
    i = start - 1
    j = stop + 1
    while True:
        while True:
            i = i + 1
            if arr[i] >= arr[pivot]:
                break
        while True:
            j = j - 1
            if arr[j] <= arr[pivot]:
                break
        if i >= j:
            return j
        arr[i] , arr[j] = arr[j] , arr[i]

# Driver Code
if __name__ == "__main__":
    array = [

[11, 8, 9, 6, 2, 7]

    quicksort(array, 0, len(array) - 1)
    print(array)

 ================================================================

*************************************************************************

2. Monte Carlo Algorithms:  A random algorithm is Monte-Carlo algorithms if it can give the wrong answer sometimes. 

  • Running time is typically fixed or bounded.
  • Output may be incorrect with a small probability.
  • Often used when approximate answers are acceptable or when error probability can be made tiny. These algorithms are used for solving physical simulation system and mathematical system.

The Monte-Carlo methods are used in places where deterministic algorithms take a lot time. Monte-Carlo integration is the most common application of Monte-Carlo algorithm. There are various methods used for integration by using Monte-Carlo methods such as,

i) Direct sampling methods which includes the stratified sampling, recursive stratified sampling, importance sampling.
ii) Random walk Monte-Carlo algorithm which is used to find out the integration for given problem.
iii) Gibbs sampling.

 

Applications of Randomized Algorithms

  1. Randomized algorithms have huge applications in Cryptography. 
  2. Load Balancing.
  3. Number-Theoretic Applications: Primality Testing.
  4. Data Structures: Hashing, Sorting, Searching, Order Statistics and Computational Geometry.
  5. Algebraic identities: Polynomial and Matrix identity verification. Interactive proof systems.
  6. Mathematical programming: Faster algorithms for linear programming, Rounding linear program solutions to integer program solutions
  7. Graph algorithms: Minimum spanning trees, shortest paths, minimum cuts.
  8. Counting and Enumeration: Matrix permanent Counting combinatorial structures.
  9. Parallel and distributed computing: Deadlock avoidance distributed consensus.
  10. Probabilistic existence proofs: Show that a combinatorial object arises with non-zero probability among objects drawn from a suitable probability space.
  11. Derandomization: First devise a randomized algorithm then argue that it can be derandomized to yield a deterministic algorithm.

 

 

 

 

 

 

 

 

Total Derivatives part 1

Geometric Interpretation of Total Derivative

Consider a function of two variables $$ z = f(x, y) $$ This function represents a surface in three-dimensional space. Each point on the surface corresponds to a particular value of $x$ and $y$.

x z y (x, y, z)

The red point represents a point on the surface corresponding to the values $(x, y)$.


Small Changes in Variables

Suppose the independent variables $x$ and $y$ change by small amounts $dx$ and $dy$. These small changes cause a change in $z$, denoted by $dz$.

dx dy

The movement along the $x$-direction by $dx$ and along the $y$-direction by $dy$ together produce a total change in the value of $z$.


=============================

Total Change in z

The total change in $z$ is the combined effect of the change due to $x$ and the change due to $y$. Mathematically, it is given by:

$$ dz = \frac{\partial z}{\partial x}\,dx + \frac{\partial z}{\partial y}\,dy $$

dz

Thus, the total derivative represents the net rate of change of the function due to simultaneous changes in all independent variables.


==========================================================

Introduction to Total Derivatives

When a dependent variable depends on more than one independent variable, its change depends on the changes in all those variables. In such cases, we use the concept of Total Derivative.

If a function is given by $$ z = f(x, y) $$ where $x$ and $y$ are independent variables, then a small change in $z$ due to small changes in $x$ and $y$ is called the total differential.

The total differential of $z$ is denoted by $dz$ and is defined as: $$ dz = \frac{\partial z}{\partial x}\,dx + \frac{\partial z}{\partial y}\,dy $$

Total derivatives are widely used in error analysis, approximation, thermodynamics, and engineering applications.


Examples on Total Derivatives


Example 1

Find the total differential of $z = x^2y$.

Solution:

$$ \frac{\partial z}{\partial x} = 2xy, \quad \frac{\partial z}{\partial y} = x^2 $$ $$ dz = 2xy\,dx + x^2\,dy $$


Example 2

Find $dz$ if $z = x^2 + y^2$.

Solution:

$$ \frac{\partial z}{\partial x} = 2x, \quad \frac{\partial z}{\partial y} = 2y $$ $$ dz = 2x\,dx + 2y\,dy $$


Example 3

Find the total differential of $z = xy + y^2$.

Solution:

$$ \frac{\partial z}{\partial x} = y, \quad \frac{\partial z}{\partial y} = x + 2y $$ $$ dz = y\,dx + (x + 2y)\,dy $$


Example 4

Find $dz$ if $z = x^3y^2$.

Solution:

$$ \frac{\partial z}{\partial x} = 3x^2y^2, \quad \frac{\partial z}{\partial y} = 2x^3y $$ $$ dz = 3x^2y^2\,dx + 2x^3y\,dy $$


Example 5

Find the total derivative of $z = \ln(x^2 + y^2)$.

Solution:

$$ \frac{\partial z}{\partial x} = \frac{2x}{x^2 + y^2}, \quad \frac{\partial z}{\partial y} = \frac{2y}{x^2 + y^2} $$ $$ dz = \frac{2x}{x^2 + y^2}\,dx + \frac{2y}{x^2 + y^2}\,dy $$


Example 6

Find $dz$ if $z = e^{xy}$.

Solution:

$$ \frac{\partial z}{\partial x} = ye^{xy}, \quad \frac{\partial z}{\partial y} = xe^{xy} $$ $$ dz = ye^{xy}\,dx + xe^{xy}\,dy $$


Example 7

Find the total differential of $z = \sin(xy)$.

Solution:

$$ \frac{\partial z}{\partial x} = y\cos(xy), \quad \frac{\partial z}{\partial y} = x\cos(xy) $$ $$ dz = y\cos(xy)\,dx + x\cos(xy)\,dy $$


Example 8

Find $dz$ if $z = x^2y + y^3$.

Solution:

$$ \frac{\partial z}{\partial x} = 2xy, \quad \frac{\partial z}{\partial y} = x^2 + 3y^2 $$ $$ dz = 2xy\,dx + (x^2 + 3y^2)\,dy $$


Example 9

Find the total differential of $z = \sqrt{x^2 + y^2}$.

Solution:

$$ \frac{\partial z}{\partial x} = \frac{x}{\sqrt{x^2 + y^2}}, \quad \frac{\partial z}{\partial y} = \frac{y}{\sqrt{x^2 + y^2}} $$ $$ dz = \frac{x}{\sqrt{x^2 + y^2}}\,dx + \frac{y}{\sqrt{x^2 + y^2}}\,dy $$


Example 10

Find $dz$ if $z = x^2y^2$.

Solution:

$$ \frac{\partial z}{\partial x} = 2xy^2, \quad \frac{\partial z}{\partial y} = 2x^2y $$ $$ dz = 2xy^2\,dx + 2x^2y\,dy $$


==================================================

Question 1

If $z = x^2y + y^2x$, find the total differential $dz$ and hence find the approximate change in $z$ when $x$ changes from 1 to 1.02 and $y$ changes from 2 to 2.01.

Solution:

$$ \frac{\partial z}{\partial x} = 2xy + y^2, \quad \frac{\partial z}{\partial y} = x^2 + 2xy $$ At $x=1$, $y=2$: $$ \frac{\partial z}{\partial x} = 8,\quad \frac{\partial z}{\partial y} = 5 $$

$$ dx = 0.02,\quad dy = 0.01 $$

$$ dz = 8(0.02) + 5(0.01) = 0.21 $$


Question 2

If $z = \ln(x^2 + y^2)$, find the total differential and evaluate $dz$ at $x=2$, $y=1$.

Solution:

$$ \frac{\partial z}{\partial x} = \frac{2x}{x^2 + y^2}, \quad \frac{\partial z}{\partial y} = \frac{2y}{x^2 + y^2} $$

At $x=2$, $y=1$: $$ dz = \frac{4}{5}dx + \frac{2}{5}dy $$


Question 3

Find the total derivative of $z = e^{xy}$ and hence find the approximate change in $z$ when $x=1$, $y=2$, $dx=0.01$, $dy=0.02$.

Solution:

$$ \frac{\partial z}{\partial x} = ye^{xy}, \quad \frac{\partial z}{\partial y} = xe^{xy} $$

At $x=1$, $y=2$: $$ dz = 2e^2(0.01) + e^2(0.02) = 0.04e^2 $$


Question 4

If $z = \sqrt{x^2 + y^2}$, find the total differential $dz$.

Solution:

$$ \frac{\partial z}{\partial x} = \frac{x}{\sqrt{x^2 + y^2}}, \quad \frac{\partial z}{\partial y} = \frac{y}{\sqrt{x^2 + y^2}} $$

$$ dz = \frac{x}{\sqrt{x^2 + y^2}}dx + \frac{y}{\sqrt{x^2 + y^2}}dy $$


Question 5

If $z = x^3y^2$, find the total differential and hence find the rate of change of $z$ with respect to time $t$, given $x = t^2$ and $y = t$.

Solution:

$$ \frac{\partial z}{\partial x} = 3x^2y^2, \quad \frac{\partial z}{\partial y} = 2x^3y $$

$$ \frac{dz}{dt} = \frac{\partial z}{\partial x}\frac{dx}{dt} + \frac{\partial z}{\partial y}\frac{dy}{dt} $$

Since $x=t^2$, $y=t$: $$ \frac{dx}{dt}=2t,\quad \frac{dy}{dt}=1 $$

$$ \frac{dz}{dt} = 3(t^4)(t^2)(2t) + 2(t^6)(t) $$


Question 6

Find the total differential of $z = \sin(xy)$.

Solution:

$$ \frac{\partial z}{\partial x} = y\cos(xy), \quad \frac{\partial z}{\partial y} = x\cos(xy) $$

$$ dz = y\cos(xy)\,dx + x\cos(xy)\,dy $$


Question 7

If $z = x^2y + y^3$, find $dz$ and hence find the approximate change in $z$ when $x=2$, $y=1$, $dx=0.05$, $dy=0.02$.

Solution:

$$ \frac{\partial z}{\partial x} = 2xy, \quad \frac{\partial z}{\partial y} = x^2 + 3y^2 $$

At $x=2$, $y=1$: $$ dz = 4(0.05) + 7(0.02) = 0.34 $$


Question 8

Show that the total differential of $z = \ln(xy)$ is $$ dz = \frac{dx}{x} + \frac{dy}{y} $$

Solution:

$$ \frac{\partial z}{\partial x} = \frac{1}{x}, \quad \frac{\partial z}{\partial y} = \frac{1}{y} $$

$$ dz = \frac{dx}{x} + \frac{dy}{y} $$


Question 9

If $z = x^2 + y^2$, find the total differential and hence obtain the approximate error in $z$ when the errors in $x$ and $y$ are $\delta x$ and $\delta y$ respectively.

Solution:

$$ dz = 2x\,dx + 2y\,dy $$

Approximate error: $$ \delta z = 2x\,\delta x + 2y\,\delta y $$


Question 10

Find the total derivative of $z = x^2y^2$ and hence find the rate of change of $z$ with respect to $t$, given $x = \sin t$ and $y = \cos t$.

Solution:

$$ \frac{\partial z}{\partial x} = 2xy^2, \quad \frac{\partial z}{\partial y} = 2x^2y $$

$$ \frac{dz}{dt} = \frac{\partial z}{\partial x}\frac{dx}{dt} + \frac{\partial z}{\partial y}\frac{dy}{dt} $$


📘 End of Ten-Marks Questions on Total Derivatives

📘 End of Total Derivative Examples

Partial Differentiation : An Introduction.

=============================================================================================

Introduction to Partial Differentiation

In many practical situations, a quantity depends on more than one independent variable. For example, the volume of a gas may depend on both pressure and temperature, or the area of a surface may depend on two spatial coordinates. Such functions are called functions of several variables.

If a function depends on two independent variables $x$ and $y$, it is written as $$ z = f(x, y) $$

Partial differentiation is the process of finding the rate of change of a function with respect to one variable while keeping the other variable(s) constant.

The partial derivative of $z$ with respect to $x$ is denoted by $$ \frac{\partial z}{\partial x} $$ and is obtained by differentiating $z$ with respect to $x$, treating $y$ as a constant.

Similarly, the partial derivative of $z$ with respect to $y$ is denoted by $$ \frac{\partial z}{\partial y} $$ and is obtained by differentiating $z$ with respect to $y$, treating $x$ as a constant.

For example, if $$ z = x^2y + xy^2 $$ then $$ \frac{\partial z}{\partial x} = 2xy + y^2, \quad \frac{\partial z}{\partial y} = x^2 + 2xy $$

Partial differentiation plays an important role in mathematics, physics, engineering, economics, and other applied sciences. It is widely used in topics such as total derivatives, maxima and minima, Euler’s theorem, and differential equations.


========================================================================

Partial Differentiation Problems with Solutions


Problem 1

If $z = x^2y + xy^3$, find $\frac{\partial z}{\partial x}$ and $\frac{\partial z}{\partial y}$.

Solution:

$$ \frac{\partial z}{\partial x} = 2xy + y^3 $$ $$ \frac{\partial z}{\partial y} = x^2 + 3xy^2 $$


Problem 2

If $z = x^3 + 3xy^2$, find $\frac{\partial^2 z}{\partial x^2}$ and $\frac{\partial^2 z}{\partial y^2}$.

Solution:

$$ \frac{\partial z}{\partial x} = 3x^2 + 3y^2 \Rightarrow \frac{\partial^2 z}{\partial x^2} = 6x $$ $$ \frac{\partial z}{\partial y} = 6xy \Rightarrow \frac{\partial^2 z}{\partial y^2} = 6x $$


Problem 3

For $z = x^2y^3$, verify that $$ \frac{\partial^2 z}{\partial x \partial y} = \frac{\partial^2 z}{\partial y \partial x} $$

Solution:

$$ \frac{\partial z}{\partial x} = 2xy^3 \Rightarrow \frac{\partial^2 z}{\partial y \partial x} = 6xy^2 $$ $$ \frac{\partial z}{\partial y} = 3x^2y^2 \Rightarrow \frac{\partial^2 z}{\partial x \partial y} = 6xy^2 $$


Problem 4

If $x^2 + y^2 + z^2 = a^2$, find $\frac{\partial z}{\partial x}$.

Solution:

$$ 2x + 2z\frac{\partial z}{\partial x} = 0 $$ $$ \frac{\partial z}{\partial x} = -\frac{x}{z} $$


Problem 5

If $z = x^2 + y^2$, where $x = r\cos\theta$ and $y = r\sin\theta$, find $\frac{\partial z}{\partial r}$.

Solution:

$$ z = r^2(\cos^2\theta + \sin^2\theta) = r^2 $$ $$ \frac{\partial z}{\partial r} = 2r $$


Problem 6 (Euler’s Theorem)

Verify Euler’s theorem for $z = x^2y + xy^2$.

Solution:

$$ \frac{\partial z}{\partial x} = 2xy + y^2 $$ $$ \frac{\partial z}{\partial y} = x^2 + 2xy $$ $$ x\frac{\partial z}{\partial x} + y\frac{\partial z}{\partial y} = 3x^2y + 3xy^2 = 3z $$


Problem 7

If $z = \ln(x^2 + y^2)$, find $\frac{\partial z}{\partial x}$.

Solution:

$$ \frac{\partial z}{\partial x} = \frac{2x}{x^2 + y^2} $$


Problem 8

If $z = e^{xy}\sin y$, find $\frac{\partial z}{\partial y}$.

Solution:

$$ \frac{\partial z}{\partial y} = e^{xy}(x\sin y + \cos y) $$


Problem 9

If $z = x^2y + y^2$, find the total differential $dz$.

Solution:

$$ \frac{\partial z}{\partial x} = 2xy,\quad \frac{\partial z}{\partial y} = x^2 + 2y $$ $$ dz = 2xy\,dx + (x^2 + 2y)\,dy $$


Problem 10

Find $\frac{\partial}{\partial x}(x^2y^3)$.

Solution:

$$ \frac{\partial}{\partial x}(x^2y^3) = 2xy^3 $$


📘 End of Exam-Oriented Problems

Approximate Matrix Multiplication: Random Sampling of Columns/Rows (Sketching

Approximate Matrix Multiplication using Random Sampling

Background: Approximate Matrix Multiplication (AMM)

Matrix multiplication is a fundamental operation in scientific computing, machine learning, data mining, and numerical linear algebra. Given two matrices \( A \in \mathbb{R}^{n \times d} \) and \( B \in \mathbb{R}^{d \times m} \), the exact computation of \( AB \) requires \( O(ndm) \) arithmetic operations, which becomes computationally expensive when the inner dimension \( d \) is large.

Approximate Matrix Multiplication (AMM) addresses this challenge by computing an approximation \( \widetilde{C} \approx AB \) that is significantly faster to obtain while maintaining provable accuracy guarantees. The key idea is to reduce the dimensionality of the problem using randomized techniques, thereby lowering computational cost and memory usage.

Random Sampling of Columns and Rows (Sketching)

One of the simplest and most intuitive approaches to AMM is random sampling of columns of \( A \) and the corresponding rows of \( B \). Since matrix multiplication can be written as a sum of outer products,

\[ AB = \sum_{i=1}^{d} A_{(:,i)} B_{(i,:)} \]

we can approximate this sum by randomly selecting only a small subset of indices \( S \subset \{1,2,\dots,d\} \) with \( |S| = k \ll d \). The approximation is then given by:

\[ \widetilde{C} = \frac{d}{k} \sum_{i \in S} A_{(:,i)} B_{(i,:)} \]

The scaling factor \( \frac{d}{k} \) ensures that the estimator is unbiased, meaning:

\[ \mathbb{E}[\widetilde{C}] = AB \]

Why Sketching Works

Each column--row pair \( A_{(:,i)} B_{(i,:)} \) contributes differently to the final product. Random sampling exploits the fact that many contributions are often small or redundant, especially in large, structured, or noisy data. By sampling only a few representative pairs, we can closely approximate the full sum with much less computation.

Accuracy and Trade-offs

The accuracy of the approximation depends on the number of samples \( k \). As \( k \) increases, the approximation converges to the exact product. With appropriate sampling strategies (such as importance sampling based on column norms), strong error bounds can be obtained in Frobenius or spectral norm.

Random sampling of columns and rows is widely used as a building block in:

  • Large-scale machine learning algorithms
  • Dimensionality reduction and feature compression
  • Recommender systems
  • Streaming and distributed computation

In the following examples, we illustrate how this technique works through simple numerical demonstrations.


Approximate Matrix Multiplication using Random Sampling of Columns/Rows

Given matrices \( A \in \mathbb{R}^{n \times d} \) and \( B \in \mathbb{R}^{d \times m} \), approximate:

\[ AB \approx \frac{d}{k} \sum_{i \in S} A_{(:,i)} B_{(i,:)} \]

Example 1: Vector × Vector

\( A = [1 \; 2 \; 3 \; 4] \), \( B = [5 \; 6 \; 7 \; 8]^T \)

Exact:

\[ AB = 70 \]

Sample: \( S = \{2,4\} \), \( d=4, k=2 \)

Approximate:

\[ AB \approx 2(2\cdot6 + 4\cdot8) = 88 \]

Example 2: Matrix × Vector

\[ A = \begin{bmatrix} 1 & 3 & 5\\ 2 & 4 & 6 \end{bmatrix}, \quad B = \begin{bmatrix} 1\\2\\3 \end{bmatrix} \]

Exact:

\[ AB = \begin{bmatrix} 22\\28 \end{bmatrix} \]

Sample: \( S=\{3\} \), \( d=3 \)

Approximate:

\[ AB \approx 3 \begin{bmatrix} 5\\6 \end{bmatrix} \cdot 3 = \begin{bmatrix} 45\\54 \end{bmatrix} \]

Example 3: Matrix × Matrix

\[ A = \begin{bmatrix} 1 & 2 & 3\\ 4 & 5 & 6 \end{bmatrix}, \quad B = \begin{bmatrix} 7 & 8\\ 9 & 10\\ 11 & 12 \end{bmatrix} \]

Exact:

\[ AB = \begin{bmatrix} 58 & 64\\ 139 & 154 \end{bmatrix} \]

Sample: \( S=\{1,3\} \)

Approximate:

\[ AB \approx 1.5(A_{(:,1)}B_{(1,:)} + A_{(:,3)}B_{(3,:)}) \]

Example 4: Single Column Sampling

\[ A = [2 \; 4 \; 6], \quad B = [1 \; 3 \; 5]^T \]

Exact: \( 44 \)

Approximate:

\[ AB \approx 3(4\cdot3) = 36 \]

Example 5: Symmetric Vectors

\[ A = B = [1 \; 2 \; 3] \]

Exact: \( 14 \)

Approximate:

\[ AB \approx \frac{3}{2}(1^2 + 3^2) = 15 \]

Example 6: Tall Matrix × Vector

\[ A = \begin{bmatrix} 1 & 0 & 2\\ 0 & 1 & 3\\ 1 & 1 & 1 \end{bmatrix}, \quad B = \begin{bmatrix} 2\\4\\6 \end{bmatrix} \]

Approximate:

\[ AB \approx 3 \begin{bmatrix} 2\\3\\1 \end{bmatrix} \cdot 6 \]

Example 7: Larger Matrix × Matrix

\[ A = \begin{bmatrix} 1 & 0 & 2 & 1\\ 2 & 1 & 0 & 3 \end{bmatrix}, \quad B = \begin{bmatrix} 1 & 2\\ 3 & 4\\ 5 & 6\\ 7 & 8 \end{bmatrix} \]

Approximate:

\[ AB \approx 2(A_{(:,1)}B_{(1,:)} + A_{(:,4)}B_{(4,:)}) \]

Error Bounds and Intuition

The approximation quality in Random Sampling–based Approximate Matrix Multiplication depends on how many column–row pairs are sampled. Let \( \widetilde{C} \) denote the approximate product and \( C = AB \) the exact product.

With uniform random sampling, the expected error measured in Frobenius norm satisfies:

\[ \mathbb{E}\left[\| \widetilde{C} - C \|_F^2 \right] \le \frac{d}{k} \sum_{i=1}^{d} \|A_{(:,i)}\|_2^2 \|B_{(i,:)}\|_2^2 \]

This inequality shows that the error decreases as the number of samples \( k \) increases. In particular, doubling the number of sampled columns roughly halves the expected error.

Intuition Behind the Error Bound

Matrix multiplication can be viewed as summing many outer products. Random sampling replaces this full sum with an estimate based on only a few terms. The approximation error arises from ignoring some column–row pairs. If most of these pairs contribute little to the final product, then the error remains small.

Thus, matrices with redundant, correlated, or low-energy columns are especially well-suited for approximate multiplication.


Importance Sampling

Uniform sampling treats all column–row pairs equally. However, in practice, some columns of \( A \) and rows of \( B \) contribute much more to \( AB \) than others. Importance sampling improves accuracy by sampling indices with higher probability when they have larger impact.

A common choice of sampling probability is:

\[ p_i = \frac{\|A_{(:,i)}\|_2 \|B_{(i,:)}\|_2} {\sum_{j=1}^{d} \|A_{(:,j)}\|_2 \|B_{(j,:)}\|_2} \]

The approximation becomes:

\[ \widetilde{C} = \frac{1}{k} \sum_{i \in S} \frac{1}{p_i} A_{(:,i)} B_{(i,:)} \]

Importance sampling significantly reduces variance and provides tighter error bounds, especially when column norms vary widely.

Key idea: sample what matters most.


Real-World Machine Learning Applications

  • Recommendation Systems: User–item matrices are often large and sparse. Approximate matrix multiplication accelerates similarity computations and matrix factorization.
  • Deep Learning: Large fully connected layers and attention mechanisms can be approximated using sketching techniques to reduce training and inference cost.
  • Natural Language Processing: Word embeddings and document–term matrices benefit from approximate multiplication during similarity search and latent semantic analysis.
  • Kernel Methods: AMM is used to approximate large kernel matrices in support vector machines and Gaussian processes.
  • Large-Scale Linear Regression: Sketching reduces the cost of computing \( X^T X \) and \( X^T y \) in high-dimensional data.

Conclusion: Random sampling of columns and rows significantly reduces computation while providing an unbiased approximation. Increasing the sample size improves accuracy.

Test - 1

Quiz: Big Data Matrix

Instructions: Enter your details, answer all questions, and click Submit Quiz.


Name:

Roll Number:


1. What is a Big Data Matrix?





2. In an Object–Attribute Matrix, rows represent ______.





3. Which matrix is commonly used in recommendation systems?





4. A Sparse Matrix is characterized by:





5. Document–Term Matrix is mainly used in:





6. Which matrix represents relationships between nodes?





7. Why are Big Data Matrices often sparse?





8. Which technology is used to process Big Data Matrices?





9. In a Time-Series Matrix, rows represent:





10. The most basic form of data matrix is:






Prepared by: Brajesh Jha

Limits of function of two variables

Limits of Functions of Two Variables

Limits of Functions of Two Variables

Definition

Let f(x,y) be defined in a neighborhood of the point (a,b), except possibly at the point itself. We say the limit of f(x,y) as (x,y) → (a,b) is L, and write:

lim(x,y)→(a,b) f(x,y) = L

if for every ε>0, there exists δ>0 such that:

0 < √((x-a)² + (y-b)²) < δ ⇒ |f(x,y)-L| < ε

Intuitive Explanation

  • The value of f(x,y) gets closer to L as (x,y) approaches (a,b) from any direction.
  • The limit must be the same along all paths approaching (a,b).

Examples

Example 1

Function: f(x,y) = x + y

Find: lim(x,y)→(1,2) f(x,y)

Solution: f(1,2) = 1 + 2 = 3

Answer: 3

Example 2

Function: f(x,y) = xy / (x² + y²)

Find: lim(x,y)→(0,0) f(x,y)

Solution:

  • Along y=0: f(x,0)=0
  • Along x=0: f(0,y)=0
  • Along y=x: f(x,x)=1/2

Answer: Limit does not exist (different values along different paths)

Example 3

Function: f(x,y) = x² + y²

Find: lim(x,y)→(1,1) f(x,y)

Solution: Direct substitution: f(1,1) = 1² + 1² = 2

Answer: 2

Example 4

Function: f(x,y) = (x² - y²) / (x² + y²)

Find: lim(x,y)→(0,0) f(x,y)

Solution:

  • Along y=0: f(x,0)=1
  • Along x=0: f(0,y)=-1

Answer: Limit does not exist

Example 5

Function: f(x,y) = 3x²y / (x² + y²)

Find: lim(x,y)→(0,0) f(x,y)

Solution:

  • Along y=0: f(x,0)=0
  • Along x=0: f(0,y)=0
  • Along y=kx: f(x,kx) → 0 as x→0

Answer: 0

Summary Table

Function Point Limit Exists?
x + y (1,2) 3
xy / (x² + y²) (0,0)
x² + y² (1,1) 2
(x² - y²) / (x² + y²) (0,0)
3x²y / (x² + y²) (0,0) 0

Function of Two variable:

 

Function of Two variable: Let u be a symbol which has a definite value for every pair of values of x and y, then u is called a function of two independent variable x and y and is written as

u=f(x,y)

 

Function of Two Variables – Graphical Representation

A function of two variables is written as:

z = f(x, y)


1. General 3D Graph

          z
          |
          |        •
          |      •
          |    •
          |  •
          |•____________ y
         /
        /
       x

Explanation: This shows a surface in 3D space where z depends on x and y.


2. Plane Surface (z = x + y)

          z
          |
          |      /
          |    /
          |  /
          |/________ y
         /
        /
       x

Application: Cost, temperature variation.


3. Paraboloid (z = x² + y²)

          z
          |
        __|__
      /   |   \
    /     |     \
  /_______|_______\ y
          |
          x

Application: Heat distribution, potential energy.


4. Saddle Surface (z = xy)

          z
          |
      ___/ \___
     /           \
----/-------------\---- y
    \             /
      \___   ___/
            x

Application: Profit–loss analysis.


5. Contour Diagram (x² + y² = c)

        y
        |
     ○  ○  ○
   ○     ○
 ○    ○    ○
   ○     ○
     ○  ○
        |
        x

Application: Topographic maps, weather maps.


Exam Note:
The graph of a function of two variables is a surface in three-dimensional space.

Function of Three Variables

A function of three variables is a function that depends on three independent variables.

Mathematical Form:

w = f(x, y, z)


Examples of Functions of Three Variables

  • Linear: f(x, y, z) = x + y + z
  • Quadratic: f(x, y, z) = x² + y² + z²
  • Product: f(x, y, z) = xyz
  • Trigonometric: f(x, y, z) = sin x + cos y + tan z
  • Rational: f(x, y, z) = (x + y) / z, z ≠ 0

Graphical Representation

A function of three variables cannot be drawn directly because it requires four dimensions. Hence, it is represented using level surfaces or cross-sections.

Level Surface Example

For: x² + y² + z² = c (Sphere)

          z
          |
       ___|___
    .-'    |    '-.
  .'        |        '.
 |          |          |
  '.        |        .'
    '-._____|_____.-'
          |
          x
         /
        y

Explanation: Each surface represents points where the function has the same value.


Cross-Section Concept

Fix one variable (e.g., z = k) and draw the 3D surface in x–y plane.

        z = k
          |
      ____|____
     /    |    \
    |     |     |
     \____|____/
          |
          x
         /
        y

Real-Life Applications

  • Temperature in a room: T = f(x, y, z)
  • Pressure in fluids
  • Electric and gravitational potential
  • Air pollution concentration
  • Medical imaging (CT, MRI)

Exam Note:
A function of three variables is represented graphically using level surfaces or cross-sections.

Assignment Functions of Two and Three Variables

Functions of Two and Three Variables

Section 1: Functions of Two Variables

Definition

A function of two variables depends on two independent variables x and y and produces a single dependent variable z:

z = f(x, y)

Examples

  • f(x, y) = x + y
  • f(x, y) = x² + y²
  • f(x, y) = xy
  • f(x, y) = sin x + cos y
  • f(x, y) = x / y, y ≠ 0

Graphical Representation

  • Graph is a surface in 3D space.
  • Plane: z = x + y
  • Paraboloid: z = x² + y²
  • Saddle: z = xy

Level Curves

A level curve is given by f(x,y) = c, e.g., x² + y² = 1.

Applications

  • Temperature distribution
  • Population density
  • Cost analysis
  • Pressure on surfaces
  • Topography

Section 2: Functions of Three Variables

Definition

A function of three variables depends on x, y, z and produces a single output w:

w = f(x, y, z)

Examples

  • f(x,y,z) = x + y + z
  • f(x,y,z) = xyz
  • f(x,y,z) = x² + y² + z²
  • f(x,y,z) = sin x + cos y + tan z
  • f(x,y,z) = (x+y)/z, z ≠ 0

Graphical Representation

  • Cannot be plotted directly in 3D (requires 4D).
  • Use level surfaces: f(x,y,z) = c
  • Use cross-sections: fix one variable and plot remaining 2D function

Applications

  • Temperature in a room
  • Pressure in fluids
  • Air pollution modeling
  • Electric/gravitational fields
  • Medical imaging (CT/MRI)

Section 3: Questions and Answers

Functions of Two Variables

Q1: Define a function of two variables.
A1: A function of two variables depends on x and y and produces a single output z.
Q2: Write the general form of a function of two variables.
A2: z = f(x, y)
Q3: How many dimensions are needed to graph it?
A3: Three dimensions (x, y, z)
Q4: What is a level curve? Give an example.
A4: A curve where f(x,y) = c, e.g., x² + y² = 1
Q5: Give any two examples.
A5: f(x,y)=x+y, f(x,y)=x²+y²
Q6: If f(x,y)=x²+y², find f(2,3).
A6: f(2,3)=2²+3²=13
Q7: Find f(-2,4) for f(x,y)=xy.
A7: f(-2,4)=(-2)(4)=-8
Q8: Determine domain of f(x,y)=x/y.
A8: All real x,y with y≠0

Functions of Three Variables

Q1: Define a function of three variables.
A1: Depends on x, y, z and produces a single output w.
Q2: Write the general form.
A2: w = f(x, y, z)
Q3: Why cannot it be plotted directly?
A3: Requires 4D visualization; use level surfaces or cross-sections.
Q4: Give three examples.
A4: f(x,y,z) = x+y+z, f(x,y,z) = xyz, f(x,y,z)=x²+y²+z²
Q5: What is a level surface? Give example.
A5: Surface where f(x,y,z)=c, e.g., x²+y²+z²=1 (sphere)
Q6: If f(x,y,z)=x+y+z, find f(1,2,3).
A6: 6
Q7: If f(x,y,z)=xyz, find f(2,-1,3).
A7: -6
Q8: Determine domain of f(x,y,z)=(x+y)/z.
A8: All real x,y,z with z≠0

Section 4: Diagrams

General Graph of Function of Two Variables z | | • | • | • | • |•____________ y / / x
Plane Surface (z = x + y) z | | / | / | / |/________ y / / x
Paraboloid (z = x² + y²) z | __|__ / | \ / | \ /_______|_______\ y | x
Saddle Surface (z = xy) z | ___/ \___ / \ /-------------\---- y \ / \___ ___/ x
Level Curve (x² + y² = c) y | ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ | x
Level Surface for Function of Three Variables (x² + y² + z² = c) z | ___|___ .-' | '-. .' | '. | | | '. | .' '-.__|__. -' | x / y
Cross-Section for Function of Three Variables z = k | ____|____ / | \ | | | \____|____/ | x / y

Assignment: Probability and Statistics Basic

Sticky Ad Probability Problems with Detailed Solutions Click each question to expand the detailed interpretation and solution. ...