简体   繁体   中英

How to approximate a value from a formula

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 1 to N and beta's 1 to N ) equals 60:

\\ sum_ {i = 1} ^ N \\ frac {e ^ {\\ alpha_i(\\ theta- \\ beta_i)}} {1 + e ^ {\\ alpha_i(\\ theta- \\ beta_i)}} = 60

def CalcTheta(grensscore, alpha, beta):
    theta = 0.0001
    estimate = [grensscore-1]
    while(sum(estimate) < grensscore):
        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)

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 we found the theta.

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)?

如果你知道θ的下限和上限,并且函数在它们之间的范围内是单调的,那么你可以使用二分算法来轻松快速地找到所需的值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM