FastPrepGradient Descent Linear Regression from Scratch
Problem · Math

Gradient Descent Linear Regression from Scratch

Learn this problem
MediumTiktok logoTiktokNEW GRADOA
See Tiktok hiring insights

Problem statement

Your task is to implement parts of the Gradient Descent optimization algorithm from scratch (i.e., without importing any libraries or packages). You will apply this algorithm for linear regression — finding the coefficients for the following equation:

y = b + θ₁x₁ + θ₂x₂ + ... + θₘxₘ

As a reminder, Gradient Descent comprises following steps:

  1. Randomly set initial values of bias b and thetas θ~i.
  2. Calculate predicted value ŷ.
  3. Calculate partial derivative of a cost function with respect to bias.

    d_b = ∂/∂b (1/n) Σᵢ₌₁ⁿ(yᵢ - ŷᵢ)² = -2/n Σᵢ₌₁ⁿ(yᵢ - ŷᵢ)

  4. Calculate partial derivatives of a cost function with respect to thetas.

    d_θₖ = ∂/∂θₖ (1/n) Σᵢ₌₁ⁿ(yᵢ - ŷᵢ)² = -2/n Σᵢ₌₁ⁿxᵢᵏ(yᵢ - ŷᵢ)

  5. Update bias and thetas (α is a learning rate).

    b = b - αd_b

    θₖ = θₖ - αd_θₖ

  6. Repeat steps 2-5 for the iterations times.

FastPrep execution adapter

The source image is cropped before the complete callable and output contract. For deterministic grading, FastPrep receives the initial bias and theta values that represent the source's randomly set initial state, then returns the final bias followed by the final coefficients. This adapter does not replace the source requirement to begin from initialized values.

Function

fitLinearRegression(xTrain: double[][], yTrain: double[], initialBias: double, initialThetas: double[], learningRate: double, iterations: int) → double[]

Examples

Example 1

xTrain = [[1], [2]]yTrain = [3, 5]initialBias = 0initialThetas = [0]learningRate = 0.1iterations = 1return = [0.8, 1.3]

FastPrep-authored runnable example (not shown in the source image): The initial predictions are both 0, so the residuals are -3 and -5. Therefore dBias = -8 and dTheta[0] = -13. One update with learning rate 0.1 produces bias 0.8 and coefficient 1.3.

Constraints

  • FastPrep execution-adapter constraints (not shown in the source image):
  • xTrain.length == yTrain.length and the arrays are non-empty.
  • Every row of xTrain has exactly initialThetas.length features.
  • learningRate > 0 and iterations >= 0.
  • All inputs and expected final parameters are finite numbers.

More Tiktok problems

drafts saved locally
public double[] fitLinearRegression(double[][] xTrain, double[] yTrain, double initialBias, double[] initialThetas, double learningRate, int iterations) {
    // Write your code here.
}
xTrain[[1], [2]]
yTrain[3, 5]
initialBias0
initialThetas[0]
learningRate0.1
iterations1
expected[0.8, 1.3]
checking account