let gradientDescent (X : Matrix<double>) (y :Vector<double>) (theta : Vector<double>) alpha (num_iters : int) =
let J_history = Vector<double>.Build.Dense(num_iters)
let m = y.Count |> double
theta.At(0, 0.0)
let x = (X.Column(0).PointwiseMultiply(X*theta-y)) |> Vector.sum
for i in 0 .. (num_iters-1) do
let next_theta0 = theta.[0] - (alpha / m) * ((X.Column(0).PointwiseMultiply(X*theta-y)) |> Vector.sum)
let next_theta1 = theta.[1] - (alpha / m) * ((X.Column(1).PointwiseMultiply(X*theta-y)) |> Vector.sum)
theta.[0] = next_theta0 |> ignore
theta.[1] = next_theta1 |> ignore
J_history.[i] = computeCost X y theta |> ignore
()
(theta, J_history)
Even though matrices and vectors are mutable, their dimension is fixed and cannot be changed after creation.
I have theta which is a vector of size 2x1 I'm trying to update theta.[0] and theta.[1] iteratively but when I look at it after each iteration it remains to be [0;0]. I know that F# is immutable but I quoted above from their website that Vectors and Matrix are mutable so I'm not sure why this isn't working.
My hunch is that it has something to do with Shadowing... because I'm declaring a let next_theta0 in a for loop but I'm not too sure
Also, as a follow up question. I feel like the way I implemented this is incredibly terrible. There's literally no reason for me to have implemented this in F# when it would have been much easier in C# (using this methodology) because it doesn't feel very "functional" Could anyone suggest ways to approve on this to make it more functional.