Any help with this error in Scipy minimize function?

In summary, the code snippet produces the following optimization result: -2*F[11] + (F[10] - 2*F[11] + F[0]) ** 2 + (F[11] - 2*F[10] + F[0]) ** 2.
  • #1
member 428835
Hi PF!

When I execute the code below:
Python:
import numpy as np
from scipy.stats import t
import scipy.optimize as optimizeglobal data
data = np.random.normal(loc=50, scale=1, size=(2400, 1)).flatten()

def L(F):
    M = 250
    lmda = 0.97
    sig_0 = F[0]
    for i in range(1, 12):
        sig_0 += F[i]

    number_of_days = len(data)

    sig = np.ones(number_of_days)

    for day in range(M, number_of_days):
        sig[day] = lmda * sig[day - 1] + (1 - lmda) * np.square(data[day - 1]) / F[day % 12] * sig_0

    data_days = np.delete(data, range(M - 1), 0)

    L_mat = np.log(t.pdf(data_days, df=3))
    L_sum = L_mat.sum(axis=0)

    mu = F[12]
    Reg = (F[11] - 2 * F[0] + F[1]) ** 2 + (F[10] - 2 * F[11] + F[0]) ** 2
    for i in range(1, 11):
        Reg += (F[i - 1] - 2 * F[i] + F[i + 1]) ** 2
    Reg *= -1 / sig_0 ** 2
    Reg *= -mu / sig_0 ** 2
    Reg += 12 / 2 * np.log(mu)

    print(F)

    final_L = -(L_sum + Reg)
    return final_L

bnds = [(None, None), (None, None), (None, None), (None, None), (None, None), (None, None), (None, None),
        (None, None), (None, None), (None, None), (None, None), (None, None), (2, 100)]
IC = 3*np.ones(13)
result = optimize.minimize(L, method='TNC', bounds=bnds, x0=IC)
print(result.F)

I get an error after many iterations that says:

Code:
Traceback (most recent call last):  File "/usr/local/lib/python3.9/site-packages/scipy/optimize/_optimize.py", line 124, in __getattr__    return self[name]KeyError: 'F'The above exception was the direct cause of the following exception:Traceback (most recent call last):  File "/Users/joshmccraney/Desktop/ewma/test_seas_ewma.py", line 45, in <module>    print(result.F)  File "/usr/local/lib/python3.9/site-packages/scipy/optimize/_optimize.py", line 126, in __getattr__    raise AttributeError(name) from eAttributeError: F
Do you know why this error is being thrown? I know the code below is messy, but this is the simplest I could make it to produce the error.
 
Technology news on Phys.org
  • #2
The OptimizeResult returned by scipy.optimize.minimize has no 'F' attribute. You want the value of its 'x' attribute.
 
  • Like
Likes member 428835 and pbuk
  • #3
pasmith said:
The OptimizeResult returned by scipy.optimize.minimize has no 'F' attribute. You want the value of its 'x' attribute.
Or just print(result) and then you will (i) see what attributes it does have, and (ii) gain other useful information about the optimization.
 
  • Like
Likes member 428835
  • #4
This was SO helpful! Thank you both!
 

Related to Any help with this error in Scipy minimize function?

What is the Scipy minimize function?

The Scipy minimize function is a numerical optimization method used to find the minimum value of a given function. It is commonly used in scientific computing and data analysis.

What does the "error" in the Scipy minimize function mean?

The "error" in the Scipy minimize function refers to any issues or problems encountered during the optimization process. This could include mathematical errors, convergence errors, or other technical errors.

How can I solve the error in the Scipy minimize function?

The best way to solve the error in the Scipy minimize function is to carefully review the documentation and any error messages provided by the function. This can help identify the source of the error and provide guidance on how to fix it.

Can I use the Scipy minimize function for any type of function?

Yes, the Scipy minimize function can be used for a wide range of functions, including both simple and complex mathematical functions. However, it is important to note that the function being optimized must be differentiable.

Are there any alternatives to the Scipy minimize function?

Yes, there are several alternative optimization methods available in Scipy, such as the "fmin" and "fmin_powell" functions. Additionally, there are other optimization libraries and packages available for scientific computing and data analysis, such as Optuna and PyTorch.

Similar threads

  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
13
Views
7K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
4
Views
3K
Back
Top