MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 16th, 2002, 04:20 PM   #1
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Dear XL,

From this thread (among others)...

http://mrexcel.com/board/viewtopic.p...8550&forum=2&5

Since you feel it is your duty to berate others on this board whom you obviously feel are beneath you, I have a question that has been quite challenging for me, but will undoubtedly only require a moment of your time.

Pre-amble:
The investment performance measurement technique recommended by AIMR is called the BAI Iterative method. The rate, r, is found by the following algorithm

Vo(1+r) + Sum(Cd*(1+r)^((n-d)/n) = Ve

Vo = opening value
Ve = ending value
n = number of days in the measurement period
d = day cashflow occurred
Cd = Cashflow amount on day d

This cannot be solved analytically (as you already know, of course).

Problem: The following UDF works for almost all situations -- that is, I can get r to converge to a value nearly every time. My request *to you* is to correct the flaws when this starts to diverge. I am unable at present to "anneal" a sequence which begins to diverge.

(By anneal, I mean that once the algorithm starts to diverge, I need to "kick it" back into a converging series.)

Helpful hints:
1. I cannot get this to converge if the "real" r is -.9 or below. Above that, it works as I want, so concentrate on the problem range.

2. I do not handle the data correctly whenever
a) The values shift over the zero (+ beg / - end market values or vice versa)
b) The cashflows cause an interim value to cross the zero.

I would wish you good luck on this task, but I am sure you will not need it.

Code:
Function BAI_ITERATION(BeginMktValue As Double, EndMktValue As Double, _
BeginDate As Date, EndDate As Date, Optional Flows, Optional FlowDates, _
Optional FlowWeighting, Optional Annualized As Boolean, _
Optional Period As Byte) As Double

Dim PeriodDays As Double, FlowFactor As Double
Dim FlowCalc(), x As Long, z As Long, FlowCount As Long
Dim Sum_of_Flows As Double, Sum_of_Wtd_Flows As Double
Dim Modified_Dietz As Double, Dietz_Denominator As Double
Dim Calculated_EMV As Double, Sum_of_ROR_Flows As Double
Dim BAI_Rate As Double
Dim lngCount As Long
Dim CalcFactor As Double
Dim FlowCell As Range, FlowDateCount As Long
Dim FlowDateLoad(), FlowLoad()
Dim FlowMatchCount As Long
Dim Calculated_EMV_Base As Double

If BeginDate >= EndDate Then
    BAI_ITERATION = CVErr(xlErrValue)
    Exit Function
End If

If IsMissing(Period) Then Period = 0
If Period <> 0 Then Period = 1

If IsMissing(FlowWeighting) Then FlowWeighting = 0
Select Case FlowWeighting
    Case Is = 1
        FlowFactor = 1 'Beginning of Day
    Case Is = 2
        FlowFactor = 0.5 'Mid day
    Case Else
        FlowFactor = 0 'End of day (default)
End Select
PeriodDays = Period + EndDate - BeginDate
' change to 1 + EndDate - BeginDate for begin-of-current-period
' to end-of-current-period calculations

If IsMissing(Flows) Then
    BAI_ITERATION = (EndMktValue - BeginMktValue) / BeginMktValue
    Exit Function
Else
    ' count the number of flows
    If TypeName(Flows) = "Range" Then
        FlowCount = Flows.Count
    Else   'If TypeName(Flows) = "Variant()" Then
        For z = LBound(Flows) To UBound(Flows)
            FlowCount = FlowCount + 1
        Next z
    End If
    ReDim FlowLoad(1 To FlowCount)
    For z = 1 To FlowCount
        FlowLoad(z) = Flows(z)
    Next z
        
    ' make sure the flow dates are legitimate
    If TypeName(FlowDates) = "Range" Then
        FlowDateCount = FlowDates.Count
    Else
        For z = LBound(FlowDates) To UBound(FlowDates)
            FlowDateCount = FlowDateCount + 1
        Next z
    End If
    ReDim FlowDateLoad(1 To FlowDateCount)
        For z = 1 To FlowDateCount
            If Len(FlowDates(z)) Then
                FlowDateLoad(z) = FlowDates(z)
            Else
                FlowDateLoad(z) = BeginDate
                FlowLoad(z) = 0
            End If
        Next z
    
    FlowMatchCount = WorksheetFunction.Min(FlowDateCount, FlowCount)
    ReDim FlowCalc(1 To FlowMatchCount, 1 To 5)
    
    For x = 1 To FlowMatchCount
        If IsMissing(FlowLoad(x)) Then FlowLoad(x) = 0
        FlowCalc(x, 1) = FlowLoad(x) ' Flow Amount
        Sum_of_Flows = Sum_of_Flows + FlowLoad(x)
        
        FlowCalc(x, 2) = FlowDateLoad(x) 'Flow Date
        
        FlowCalc(x, 3) = WorksheetFunction.Min(PeriodDays, _
        WorksheetFunction.Max(0, Period + FlowDateLoad(x) - BeginDate)) 'Day of Flow
        ' change to 1 + FlowDates(x) - BeginDate for begin-of-period
        ' to end-of-period calculations
        
        FlowCalc(x, 4) = FlowCalc(x, 1) * _
        WorksheetFunction.Min(PeriodDays, _
        (PeriodDays + FlowFactor - FlowCalc(x, 3))) / PeriodDays ' Weighted flow
        Sum_of_Wtd_Flows = Sum_of_Wtd_Flows + FlowCalc(x, 4)
        
        FlowCalc(x, 5) = (PeriodDays + FlowFactor - FlowCalc(x, 3)) / PeriodDays 'ROR exponent
    Next x
End If

Dietz_Denominator = BeginMktValue + Sum_of_Wtd_Flows

If Dietz_Denominator = 0 Then Dietz_Denominator = 0.000001
Modified_Dietz = (EndMktValue - BeginMktValue - Sum_of_Flows) / Dietz_Denominator
BAI_Rate = Modified_Dietz
    
    For x = 1 To FlowMatchCount
        Sum_of_ROR_Flows = Sum_of_ROR_Flows + _
        (FlowCalc(x, 1) * ((1 + BAI_Rate) ^ FlowCalc(x, 5)))
    Next x
    Calculated_EMV = BeginMktValue * (1 + BAI_Rate) + Sum_of_ROR_Flows
    Calculated_EMV_Base = Calculated_EMV
    On Error Resume Next
    Do Until Abs(Calculated_EMV - EndMktValue) <= 0.00000000001
        lngCount = lngCount + 1
        If lngCount > 100000 Then
            If Abs(Calculated_EMV - EndMktValue) < Abs(Calculated_EMV_Base - EndMktValue) Then
                BAI_ITERATION = BAI_Rate
            Else
                BAI_ITERATION = Modified_Dietz
            End If
            Exit Function
        End If
    CalcFactor = WorksheetFunction.Min(0.05, Abs((Calculated_EMV - EndMktValue)) / _
    WorksheetFunction.Max(Abs(Calculated_EMV), Abs(EndMktValue)))
    If Calculated_EMV < EndMktValue Then
        BAI_Rate = BAI_Rate + Abs(BAI_Rate * CalcFactor)
    Else
        BAI_Rate = BAI_Rate - (BAI_Rate * CalcFactor * Sgn(BAI_Rate))
    End If
            Sum_of_ROR_Flows = 0
            For x = 1 To FlowMatchCount
                Sum_of_ROR_Flows = Sum_of_ROR_Flows + _
                (FlowCalc(x, 1) * ((1 + BAI_Rate) ^ FlowCalc(x, 5)))
            Next x
            Calculated_EMV = BeginMktValue * (1 + BAI_Rate) + Sum_of_ROR_Flows
    Loop
    If Annualized Then
        BAI_ITERATION = (1 + BAI_Rate) ^ (365 / PeriodDays) - 1
    Else
        BAI_ITERATION = BAI_Rate ' default = false
    End If
End Function
For more details, search for the answer on the internet. Search google.com for BAI Iteration and Excel and you will see a few posts by me explaining it in more detail.

Idiots like me haven't had any numerical analysis courses and don't know any programming languages other than VBA for Excel, so I am unable to read, let alone translate, any C or Fortran routine which may help. I am sure the great XL can do it while sleeping.

Thanks for your *PROMPT* resolution to this problem.

Regards,
Jay

P.S. For all others, any help greatly appreciated. My new hero, XL, should come through on this one, but if you need any explanation or details, please let me know.

(Sarcasm throughout fully intended)



[ This Message was edited by: Jay Petrulis on 2002-05-16 16:09 ]
Jay Petrulis is offline   Reply With Quote
Old May 16th, 2002, 05:43 PM   #2
dimrod
 
Join Date: May 2002
Posts: 73
Default

I think you're being a bit hard on XL.
I'm sure he would be first in line to praise those greater than he.
It's probably just that he can't find anyone.

[ This Message was edited by: dimrod on 2002-05-16 16:44 ]
dimrod is offline   Reply With Quote
Old May 16th, 2002, 06:01 PM   #3
James
Board Regular
 
Join Date: Feb 2002
Location: Huntington Beach, CA USA
Posts: 327
Default

Hi Jay,

Kudos to you my friend! It's all about helping but some people just don't get it.

With that said, i will be more than happy to help you with your code problem as soon as i
get my own figuered out. I'm trying to sum
in Cell C1 what i have in A1 and B1.

Regards,
James
James is offline   Reply With Quote
Old May 16th, 2002, 06:04 PM   #4
dimrod
 
Join Date: May 2002
Posts: 73
Default

Quote:
On 2002-05-16 17:01, James wrote:
Hi Jay,

Kudos to you my friend! It's all about helping but some people just don't get it.

With that said, i will be more than happy to help you with your code problem as soon as i
get my own figuered out. I'm trying to sum
in Cell C1 what i have in A1 and B1.

Regards,
James

James

Mail me your workbook.

No promises, but I might be able to help.

Dimrod
dimrod is offline   Reply With Quote
Old May 16th, 2002, 06:10 PM   #5
James
Board Regular
 
Join Date: Feb 2002
Location: Huntington Beach, CA USA
Posts: 327
Default

Hi Dimrod

Thanks for offering your assistance, but i would feel better about myself if i could figure this out on my own.

Regards,
James
James is offline   Reply With Quote
Old May 16th, 2002, 06:23 PM   #6
dsnbld
Board Regular
 
Join Date: May 2002
Posts: 206
Default

James- Good luck :^)
dsnbld is offline   Reply With Quote
Old May 16th, 2002, 06:28 PM   #7
Nimrod
MrExcel MVP
 
Join Date: Apr 2002
Location: Vancouver BC , Canada
Posts: 6,259
Default

I've read the posting of XL's that you refer to ! And I feel that children throwing tamtrums are best left alone until they learn to play with others. XL... maybe you should take a "time out" until you can come back and apologize to Jack.
Nimrod is offline   Reply With Quote
Old May 16th, 2002, 06:31 PM   #8
dimrod
 
Join Date: May 2002
Posts: 73
Default

James

Let me know how you get on.

Would be good if you could post your solution when you get there - so that everyone can benefit.

dimrod
dimrod is offline   Reply With Quote
Old May 16th, 2002, 06:39 PM   #9
James
Board Regular
 
Join Date: Feb 2002
Location: Huntington Beach, CA USA
Posts: 327
Default

Dimrod,

I always try to help and share all i know, (which could fill half a thimble) BUT if i figure this one out i'll probably keep it to myself.

Regards,
James
James is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 05:55 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes