Closed erineidschun closed 5 years ago
Hi @erineidschun . Thanks for posting this question! The function sum_squares
should contain yfunc
. The provided data is a sequence of ordered pairs, (t_1, y_1), (t_2, y_2), ..., (t_n, y_n)
. The point of the sum_squares
function is to calculate the expression
The yfunc
function if supposed to represent y(t)
and used to calculate y(t_i)
for every i
. Let me know if this helps!
My sum_Squares returns a number, 27.432326688864485, when I run sum_squares(x_initial). This is the value of the sum of squares for this input. So I do not really understand what the next question is asking us to do, if sum_squares already calculates the umber. minimize seems to take a function as an argument, but if sum_squares is returning a number, I don't see what function we should be putting into minimize. It's not like we can write: minimize((sum(y - ydata)**2)) or something equivalent, right?
With my current code, with minimize(x_initial), I get a " 'list' object is not callable" error.
I have: START def minimize(x): y_ss = sum_squares(x) #this is a number xstar = scipy.optimize.minimize(y_ss, 0) return xstar
minimize(x_initial) END
Great questions. Regarding the value of sum_squares
, that's correct. I get
In [36]:
sum_squares(x_initial)
Out[36]:
27.432326688864467
The idea of the next question is to choose a function that best fits the data. The criterion for choosing the best fit is to choose the function that minimizes the sum of squares. The functions that we are choosing from each have the form
The functions differ only in terms of the parameters x_0, x_1, x_2, x_3
. So, you want the optimizer to be choosing from among different values for these parameters. The sum_squares
function should go from parameters x
to the value of the total sum of squares. Then, you should be able to write
opt = minimize(sum_squares, x_initial)
xstar = opt.x
xstar
In your code,
y_ss = sum_squares(x) #this is a number
xstar = scipy.optimize.minimize(y_ss, 0)
your getting the 'list' object is not callable
error because y_ss
is not a function. The expression sum_squares(x)
should be a number (in your case it appears to be a list). sum_squares
, on the other hand, is still a function. The (x)
portion tells Python to treat sum_squares
as a function and evaluate it with the input x
.
Hope this helps!
Thanks. I am now getting a 1 x 4 array which I think is the answer. My last question is, what does opt.x do? Does it run: opt = scipy.optimize.minimize(sum_squares, x)?
It seems that x is: array([[ 0.13794099, -1.85125435], [ 0.14535141, 5.06327053], [-0.02602296, 0.02783364], [-0.24004342, 10.44010092], [-0.0193288 , 0.06885402]])
which I don't see that we've defined above. What is x?
Thanks!
Cool. You should be able to tell if your answer is correct based on the plot of the function using the optimal parameters. It should look like this:
In my code
opt = minimize(sum_squares, x_initial)
xstar = opt.x
xstar
I save the output of minimize(sum_squares, x_initial)
to the variable opt
. The SciPy function minimize
actually returns an object. The object contains a lot of different things, including diagnostics about how the minimization algorithm performs. One of those attributes is x
that I retrieve by writing opt.x
. The x
attribute is the value that minimizes the supplied function, sum_squares
.
I am confused as to what this question is asking: "use scipy.optimize.minimize to minimize the sum of squares, using the function that you wrote previously. Save the optimal parameters to the variable xstar."
Are you saying to disregard yfunc, and to use the minimize function to find a function that will minimize the sum of squares between ydata and this new function, and then to print the value of this sum of squares?
In the problem before, you imply that sum_squares(x) should return a number indicating the value of the sum of squares between yfunc and ydata, when given x_initial. Thus, I do not see how to tie this into the problem above, as it does not seem to me that we should be using yfunc at all.