0

So I have one vector of alpha, one vector of beta, and I am trying to find a theta for when the sum of all the estimates (for alpha's i to n and beta's i to n) equals 60.

math.exp(alpha[i] * (theta - beta[i])) / (1 + math.exp(alpha[i] * (theta - beta[i])

enter image description here

Basically what I did is start from theta = 0.0001, and iterate through, calculating all these sums, and when it is lower than 60, continue by adding 0.0001 each time, while above 60 means.

I found the value theta this way. Problem is, it took me about 60 seconds using Python, to find a theta of 0.456.

What is quicker approach to find this theta (since I would like to apply this for other data)?

def CalcTheta(score, alpha, beta):
    theta = 0.0001
    estimate = [score-1]

    while(sum(estimate) < score):

        theta += 0.00001

        for x in range(len(beta)):
            if x == 0:
                estimate = []

            estimate.append(math.exp(alpha[x] * (theta - beta[x]))  / (1 +  math.exp(alpha[x] * (theta - beta[x]))))

    return(theta)
PascalVKooten
  • 20,643
  • 17
  • 103
  • 160

1 Answers1

1

You could use zip and sum to compute your target function:

  def f(theta):
    return sum(1/(1 + exp(a*(b-theta)))) for a,b in zip(alpha, beta))
xuanji
  • 5,007
  • 2
  • 26
  • 35
  • Really have no clue how to deal with this. It gives me an error on `for`. Should this work on python 3.3? – PascalVKooten Mar 18 '13 at 16:03
  • Added parentheses, then works on Python 3.3. Still I do not see why to use this? – PascalVKooten Mar 18 '13 at 16:08
  • Why does this not work: `return(sum(math.exp(a * (theta - b)) / (1 + math.exp(a * (b- theta))) for a,b in zip(alpha[questions], beta[questions])))`, where `questions` is a list of numbers. – PascalVKooten Mar 18 '13 at 21:20