2016-02-27 27 views
-1
import xlrd 
import numpy 

fileWorkspace = 'C://Users/jod/Desktop/' 

wb1 = xlrd.open_workbook(fileWorkspace + 'assign2.xls') 
sh1 = wb1.sheet_by_index(0) 

time,amount,category = [],[],[]    
for a in range(2,sh1.nrows): 
    time.append(int(sh1.cell(a,0).value))  # Pulling time from excel (column A) 
    amount.append(float(sh1.cell(a,1).value)) # Pulling amount from excel (column B) 
    category.append(str(sh1.cell(a,2).value)) # Pulling category from excel (column C) 
#print(time) 
#print(amount) 
#print(category) 
print('\n') 

p_p2 = str(sh1.cell(0,1)) 
p_p1 = p_p2.replace("text:'","") 
pp = p_p1.replace("'","") 
print(pp)       # Printing the type of pay period (Row 1, col B) 
c_p2 = str(sh1.cell(1,1)) 
c_p1 = c_p2.replace("text:'","") 
cp = c_p1.replace("'","") 
print(cp)       # Printing the type of compound period (Row 2, col B) 

netflow = 0 
outflow = 0 
inflow = 0 
flow = 0 

cat = ["Sales", "Salvage", "Subsidy", "Redeemable", "Utility", "Labor", 
     "Testing", "Marketing", "Materials", "Logistics"] 

if pp == "Years" and cp == "Years": # if pay period and compound period are both in years 

    IRR = numpy.irr(amount) * 100   # Calculates the internal rate of return (IRR) 
    print ("IRR:", round(IRR, 2), '%', '\n') # prints (IRR) 

    for i in time:    # for every value in time array 
     if cat[5] in category: # if "Labor" for cat array is in category array or not 

      # calculates the present values using all the amount values (col B) instead of 
      # just using the ones that has "Labor" category label beside them 
      # Need to make every other value 0, such as beside "Redeemable" and "Salvage" 
      flow = amount[i]/numpy.power((1 + (IRR/100)), time[i]) 
      if flow>0:      
       inflow = inflow + flow  
      if flow<0:      
       outflow = outflow + flow 

      print ('Present Value (P) is:', round(flow,0), '\n') 

    netflow = outflow + inflow 
    print("In year 0 or current year") 
    print("-------") 
    print ('Outflow is: ', round(outflow,0)) 
    print ('Inflow is: ', round(inflow,0)) 
    print ('Netflow is: ', round(netflow,0), '\n') 

    outflow2 = (round(outflow,0))*(1+(IRR/100))**(9) 
    inflow2 = (round(inflow,0))*(1+(IRR/100))**(9) 
    netflow2 = outflow2 + inflow2 

    print("In year 9") 
    print("-------") 
    print ('Outflow is: ', round(outflow2,0)) 
    print ('Inflow is: ', round(inflow2,0)) 
    print ('Netflow is: ', round(netflow2,0), '\n') 

我已經評論了重要的代碼行以供說明。 原始問題:使用IRR(Python)計算僅一個類別的未來值

說明了按類別劃分的主要項目收入和費用在第9年的項目未來價值的百分比。圖表還必須清楚地表明該項目在第9年的未來總價值以及內部收益率。

項目可能由10個收入和成本類別組成。這些類別包括:銷售,補救,補貼,可贖回,公用設施,勞動力,測試,營銷,材料和物流。所有的收入和支出將落入這十個類別中的一個。項目支付期和複合期將在Excel工作表頂部標識。薪酬期和複合期可以指定爲以下任何一種:年,季,月。


我越來越困惑,因爲我無法從「勞動」旁邊拉唯一值,「可贖回」,或「打撈」。我只是不知道我犯了什麼錯誤,或者有什麼是不完整的。下面是Excel文件圖像:

Excel File Image 2 Excel File Image 3

回答

0

修訂後,所有現金流都在打折的內部收益率。所做的事情如下:i)確定調整採用支付期(A欄),並在年末結束時調整(如果是每月結束時的年度結算),並且每月的結果爲月份結束(不需要調整)。這將通過12是否需要每年現金流劃分支付期間(每年配混)

ⅱ)IRR進行計算,並配混期間用於調整月薪週期

每月IRR

ⅲ)所有費用在IRR折扣並輸入到cat_contributions ['category_name'] = [打折期1,打折期2 ...]的清單中。

iv)然後淨流入和流出是這些的總和。

我無法在圖像中輸入電子表格中的數據,因爲這需要一段時間,但可能會修改此信息並查看是否可以使其工作。

from __future__ import division 
import xlrd 
import numpy 
import os 
import math 

def main(xls = 'xls_name.xlsx', sh = 0): 
    #save script in same folder as the xls file 
    os.chdir( os.getcwd()) 
    wb = xlrd.open_workbook(xls) 
    sh = wb.sheet_by_index(0) 

    pay_period = sh.cell_value(0,1) 
    compounding_period = sh.cell_value(1,1) 

    compounding_factor, pay_factor = determineAdjustments(
     pay_period, compounding_period) 

    number_of_periods = max(sh.col_values(0, start_rowx = 2)) 

    flow_per_period = [ 0*i for i in range(int(math.ceil(number_of_periods/pay_factor)) + 1) ]#list of length number of pay_periods 
    for r in range(2,sh.nrows): 
     pay_period = int(math.ceil(sh.cell_value(r,0)/pay_factor)) 
     flow_per_period[pay_period] += sh.cell_value(r,1) #unadjusted cash flows 


    irr = calculateIRR(flow_per_period, compounding_factor) 

    cat_contributions = sortExpenditures(sh, irr, pay_factor) 
    total_cat_contributions, netflow, total_outflow, total_inflow = calculateFlows(cat_contributions) 
    printStats(cat_contributions, irr, compounding_factor, pay_factor, 
       total_cat_contributions, netflow, total_outflow, total_inflow) 
    return 

def determineAdjustments(pay_period, compounding_period): 

    if compounding_period == 'years': 
     compounding_factor = 1 
     if pay_period == 'months': 
      pay_factor = 12 
     if pay_period == 'years': 
      pay_factor = 1 
     #assume no days pay periods 

    if compounding_period == 'months': 
     compounding_factor = 12 
     #assume no yearly payouts and that the 
     #all payments are in months 
     pay_factor = 1 


    return compounding_factor, pay_factor 

def calculateIRR(cashflow, compounding_factor): 

    irr = numpy.irr(cashflow) 

    irr_comp = (1 + irr)**compounding_factor - 1 

    #seems like in first example it uses rounded irr, can do something like: 
    #irr_comp = round(irr_comp,4) 
    return irr_comp 

def sortExpenditures(sh, irr, pay_factor): 
    #percentages and discounting occurs at the IRR caculated in the main 
    #function 

    cat = ["Sales", "Salvage", "Subsidy", "Redeemable", "Utility", "Labor", 
     "Testing", "Marketing", "Materials", "Logistics"] 

    #python dictionary to sort contributions into categories 
    cat_contributions = {} 
    for c in cat: 
     cat_contributions[c] = [] 

    # create list of contributions of each list item to FV in a dictionary 
    for r in range(2,sh.nrows): 
     try: 
      #discounted cash flow of each expenditure 
      #using formula FV = expenditure/(1+i)^n 
      cat_contributions[sh.cell_value(r,2)].append(
       sh.cell_value(r,1)/((1 + irr) ** (sh.cell_value(r,0)/pay_factor)) 
       ) 

     except KeyError: 
      print "No category for type: " + sh.cell_value(r,2) +'\n' 

    return cat_contributions 

def calculateFlows(cat_contributions): 

    total_outflow = 0 
    total_inflow = 0 

    total_cat_contributions = {} 

    for cat in cat_contributions: 
     total_cat_contributions[cat] = sum(cat_contributions[cat]) 
     if total_cat_contributions[cat] < 0: 
      total_outflow += total_cat_contributions[cat] 
     else: 
      total_inflow += total_cat_contributions[cat] 

    netflow = total_inflow + total_outflow 

    return total_cat_contributions, netflow, total_outflow, total_inflow 

def printStats(cat_contributions, irr, compounding_factor, pay_period, 
       total_cat_contributions, netflow, total_outflow, total_inflow): 

    print "IRR: "+str(irr*100) +' %' 
    if compounding_factor == 1: print "Compounding: Yearly" 
    if compounding_factor == 12: print "Compounding: Monthly" 

    if pay_period == 1: "Cashflows: Year Ended" 
    if pay_period == 12: "Cashflows: Month Ended" 

    print "Future Value (Net Adjusted Cashflow): " +str(netflow) 
    print "Adjusted Inflows: " + str(total_inflow) 
    print "Adjusted Outflows: " + str(total_outflow) +'\n' 


    for cat in total_cat_contributions: 
     if total_cat_contributions[cat] != 0: 
      print '-----------------------------------------------------' 
      print cat + '\n' 
      print "Total Contribution to FV " + str(total_cat_contributions[cat]) 
      if total_cat_contributions[cat] < 0: 
       print "Contribution to Expenses: " + str (abs(100 * total_cat_contributions[cat]/total_outflow)) 
      else: 
       print "Contribution to Revenues: " + str (abs(100 * total_cat_contributions[cat]/total_inflow)) +'\n' 


main(xls='Book1.xlsx') 
+0

41行錯誤:'flow_per_year [INT(sh1.cell_value(R,0))] + = sh1.cell_value(R,1)#unadjusted現金flows''IndexError:列表索引超出range' –

+0

的對不起,我想我認爲那是來自所提供圖像的表單中的所有行。我已將所有sh1.nrows更改爲值'13'。試試看看是否有效? –

+0

它確實給了我結果。但是,它一次打印所有內容,然後再次打印所有內容。另外,我對你的代碼是什麼都很困惑。讓我看看你到底在做什麼,我會盡快回復你。 –