wwtwill
wwtwill
2019-05-10 15:47
浏览 1.9k

python中如何表示对一个含未知数的式子做对数运算

代码如下,python中常用求对数是math.log(x),此x必须是数值。此代码是先表示效用函数,再代入biogeme包用多项logit模型,结合极大似然估计法求出,效用函数待标定的参数。想对效用函数V做对数处理,但V里ASC_TRAIN, B_TIME, B_COST为待标定参数,因此用math.log(V)时会报错 TypeError: must be real number, not Minus,小白想求加能不能通过自定义log函数的方法,去掉log后必须是数值的限制,或有无其他解决方法?


import pandas as pd
import biogeme.database as db
import biogeme.biogeme as bio
import math as ma

pandas = pd.read_excel(r"C:\Users\y\Desktop\data\biogeme\mnl\swissmetro.xlsx")
database = db.Database(r"C:\Users\y\Desktop\data\biogeme\mnl\swissmetro.xlsx",pandas)

# The Pandas data structure is available as database.data. Use all the
# Pandas functions to invesigate the database
#print(database.data.describe())

from headers import *

# Removing some observations can be done directly using pandas.
#remove = (((database.data.PURPOSE != 1) & (database.data.PURPOSE != 3)) | (database.data.CHOICE == 0))
#database.data.drop(database.data[remove].index,inplace=True)

# Here we use the "biogeme" way for backward compatibility
exclude = (( PURPOSE != 1 ) * (  PURPOSE   !=  3  ) +  ( CHOICE == 0 )) > 0
database.remove(exclude)


ASC_CAR = Beta('ASC_CAR',0,None,None,0)
ASC_TRAIN = Beta('ASC_TRAIN',0,None,None,0)
ASC_SM = Beta('ASC_SM',0,None,None,1)
B_TIME = Beta('B_TIME',0,None,None,0)
B_COST = Beta('B_COST',0,None,None,0)

SM_COST =  SM_CO   * (  GA   ==  0  ) 
TRAIN_COST =  TRAIN_CO   * (  GA   ==  0  )

#CAR_AV_SP =  DefineVariable('CAR_AV_SP',CAR_AV  * (  SP   !=  0  ),database)
#TRAIN_AV_SP =  DefineVariable('TRAIN_AV_SP',TRAIN_AV  * (  SP   !=  0  ),database)

TRAIN_TT_SCALED = DefineVariable('TRAIN_TT_SCALED',\
                                 TRAIN_TT / 100.0,database)
TRAIN_COST_SCALED = DefineVariable('TRAIN_COST_SCALED',\
                                   TRAIN_COST / 100,database)
SM_TT_SCALED = DefineVariable('SM_TT_SCALED', SM_TT / 100.0,database)
SM_COST_SCALED = DefineVariable('SM_COST_SCALED', SM_COST / 100,database)
CAR_TT_SCALED = DefineVariable('CAR_TT_SCALED', CAR_TT / 100,database)
CAR_CO_SCALED = DefineVariable('CAR_CO_SCALED', CAR_CO / 100,database)

V1 = -ASC_TRAIN - \
     B_TIME * TRAIN_TT_SCALED - \
     B_COST * TRAIN_COST_SCALED
V2 = -ASC_SM - \
     B_TIME * SM_TT_SCALED - \
     B_COST * SM_COST_SCALED
V3 = -ASC_CAR - \
     B_TIME * CAR_TT_SCALED - \
     B_COST * CAR_CO_SCALED

V11 = ma.log(V1)
V22 = ma.log(V2)
V33 = ma.log(V3)
# Associate utility functions with the numbering of alternatives
V = {1: V11,
     2: V22,
     3: V33}

# Associate the availability conditions with the alternatives

av = {1: TRAIN_A,
      2: SM_A,
      3: CAR_A}

logprob = bioLogLogit(V,av,CHOICE)
biogeme  = bio.BIOGEME(database,logprob)
biogeme.modelName = "MNL_Model_final"
results = biogeme.estimate()

# Print the estimated values
betas = results.getBetaValues()
for k,v in betas.items():
    print(f"{k}=\t{v:.3g}")

# Get the results in a pandas table
pandasResults = results.getEstimatedParameters()
print(pandasResults)   

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

相关推荐