简体   繁体   中英

Goal seek in percentile calculation in python

def z(Adj_fac):
  mu = 8.988269545509695
  sigma = 2.2470673863774238
  percentile_dist = [(stats.lognorm(sigma, scale=np.exp(mu)).ppf(perceitil/10-Adj_fac))/10 for perceitil in range (1,11)]
  return sum(percentile_dist)

z(0.0204282)

I will get 100000.09757907598, which is closer to my goal = 100,000.

How can I do the reverse case? knowing my goal is 1000,000, to seek the Adj_fac (which is 0.0204282).

I have tried https://github.com/DrTol/GoalSeek_Python/blob/master/ExampleScript.py . I got negative value. Need some help here. Thanks.

This works for me:

import numpy as np
import scipy.stats as stats
from scipy.optimize import differential_evolution

mu = 8.988269545509695
sigma = 2.2470673863774238

def z(Adj_fac, dist):
  percentile_dist = [(dist.ppf(perceitil/10.0-Adj_fac))/10.0 for perceitil in range (1,11)]
  return sum(percentile_dist)

  
def objective(Adj_fac, goal, dist):

    return (z(Adj_fac, dist) - goal)**2.0
  
dist = stats.lognorm(sigma, scale=np.exp(mu))
bounds = [(0.0, 0.1)]
goal = 1e5
res = differential_evolution(objective, bounds, seed=1, polish=True, args=(goal, dist))
xf, yf, fun_evals = res.x, res.fun, res.nfev
print(xf)

It's important to put bounds on your Adj_fac variable as the ppf becomes NaN very quickly outside the defined interval.

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