Home>

I am trying to minimize the loss function of radical estimates for estimating the parameters of the normal distribution

My code looks like this:

import numpy as np
from scipy import stats
from scipy.optimize import minimize
x= [1,2,3,4,5]
def oro (theta, x):
    norma= 0
    b= 1
    u= theta [0]
    o= theta [1]
    x= np.array (x)
    x0= 0
    f0= -(((1 /(o * (2 * 3.14) ** (0.5))) * (2.718) ** -(((x0-u) ** 2) /(2 * (o ** 2 )))) ** b) ** -1
    for i in range (x.size):
        f= (1 /(o * (2 * 3.14) ** (0.5))) * (2.718) ** -(((x [i] -u) ** 2) /(2 * (o ** 2 ))) ** b
        norma += f0 * f
    return norma
theta_init= [0, 1]
res= minimize (oro, theta_init, args= x)
res

But the output shows an error:

<
ipython-input-81-ee81472a023a >
: 8: RuntimeWarning: divide by zero encountered in double_scalars
  f0= -(((1 /(o * (2 * 3.14) ** (0.5))) * (2.718) ** -(((x0-u) ** 2) /(2 * (o ** 2 )))) ** b) ** -1
<
ipython-input-81-ee81472a023a >
: 11: RuntimeWarning: invalid value encountered in double_scalars
  norma += f0 * f
<
ipython-input-81-ee81472a023a >
: 8: RuntimeWarning: divide by zero encountered in double_scalars
  f0= -(((1 /(o * (2 * 3.14) ** (0.5))) * (2.718) ** -(((x0-u) ** 2) /(2 * (o ** 2 )))) ** b) ** -1
<
ipython-input-81-ee81472a023a >
: 11: RuntimeWarning: invalid value encountered in double_scalars
  norma += f0 * f
<
ipython-input-81-ee81472a023a >
: 8: RuntimeWarning: divide by zero encountered in double_scalars
  f0= -(((1 /(o * (2 * 3.14) ** (0.5))) * (2.718) ** -(((x0-u) ** 2) /(2 * (o ** 2 )))) ** b) ** -1
<
ipython-input-81-ee81472a023a >
: 11: RuntimeWarning: invalid value encountered in double_scalars
  norma += f0 * f
      fun: nan
 hess_inv: array ([[9.57096191e + 02, 2.41349815e + 01],
       [2.41349815e + 01, 8.33412317e-01]])
      jac: array ([nan, nan])
  message: 'Desired error not necessarily achieved due to precision loss.'
     nfev: 357
      nit: 4
     njev: 119
   status: 2
  success: False
        x: array ([165623.69347712, 1751.95100725])

Tell me, please, what am I doing wrong?

Add imports

CrazyElf2021-11-25 10:09:02

What is the magic function minimize?

Andrew2021-11-25 10:10:14

Added imports

James Shady2021-11-25 10:14:57

You seem to be overflowing there. If you print print (u, o) inside a function, they fly away there for 1e + 60, and then apparently overflow and are discarded back. I do not know why the minimum is not sought.

CrazyElf2021-11-25 10:40:49