-1

I am trying to solve for the following equation, however, there is very little documentation on the SciPy reference guide so I am not sure how to go about doing it.

I have an array of 10 uniform random variables, let's call it X.

I have the following function which takes a parameter theta and a uniform RV from the X's and returns their product raised to the power of e:

def f(x_i, theta):

    return math.exp(x * theta)

I am trying to find theta such that this equation holds:

enter image description here

ok you can move the 20 to the other side of the equation so that it is 0, but still I am not sure how to optimize for this quantity given the summation?

Any help would be appreciated! Thank you.

skidjoe
  • 493
  • 7
  • 16

1 Answers1

0

I am wondering what the hiccup is. In essence, you just have a function g(theta) with

 g(theta) = sum(i, f(x[i],theta))/10 - 20

I assume x is data.

So, we could do:

from scipy.optimize import root_scalar
from random import uniform, seed
from math import exp

def f(x_i, theta):
  return exp(x_i * theta)

def g(theta, x):
  s = 0
  for x_i in x:
    s += f(x_i,theta)
  return s/10 - 20

def solve():
  n = 10
  seed(100)
  x = [uniform(0,1) for i in range(n)] 
  print(x)
  res = root_scalar(g,args=x,bracket=(0,100))
  print(res)

solve()

I see:

[0.1456692551041303, 0.45492700451402135, 0.7707838056590222, 0.705513226934028, 0.7319589730332557, 0.43351443489540376, 0.8000204571334277, 0.5329014146425713, 0.08015370917850195, 0.45594588118356716]
      converged: True
           flag: 'converged'
 function_calls: 16
     iterations: 15
           root: 4.856897057972411
Erwin Kalvelagen
  • 15,677
  • 2
  • 14
  • 39