Python Forum
How to make interations untill a certain value of equation is reached?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make interations untill a certain value of equation is reached?
#1
I need to construct a loop (simulation) that will iterate a certain number of times and display a value of warrant once the new firm value is close to the guess firm value. Specifically, the idea is to start out with a guess for the firm value (for example the stock price multiplied by the number of shares). Then you value the warrant as a call option (the code below) on this value multiplied by dilution factor, using the same volatility as the vol of the share price. You recompute then the value of the firm (number of shares times share price plus number of warrants times warrant price). This value will be different from the value of the firm you started with. Then you redo the procedure and after a few iterations you will see that the difference in values of the firm tends to zero. For this, I have a following code, but what I get is the following:
Error:
warrant[i] = bsm_call_value(New_FirmValue[i]/N,100,1,0.1,0.2)*dilution NameError: name 'New_FirmValue' is not defined
The problem is that 'New_FirmValue' is defined after the function. What I am missing is how to program dynamic dependencies (I tried in code below). For ex, "warrant" depends on "New_FirmValue" which is "Guess_FirmValue"(constant) + "warrant"(newly calculated). The output should be the value of "warrant" that brings this relation "Guess_FirmValue - New_FirmValue[i] == 0"
Please, help me to figure out the error given the code below:
def bsm_call_value(S0, K, T, r, sigma):
    from math import log, sqrt, exp
    from scipy import stats
    S0 = float(S0)
    d1 = (log(S0 / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * sqrt(T))
    d2 = (log(S0 / K) + (r - 0.5 * sigma ** 2) * T) / (sigma * sqrt(T))
    value = (S0 * stats.norm.cdf(d1, 0.0, 1.0) - K * exp(-r * T) *stats.norm.cdf(d2, 0.0, 1.0))
    return value

Guess_FirmValue = S0*N
def warrant_1unobservable(S0, K, T, r, sigma, k, N, M, Iteration):
    for i in range(1, Iteration):  
        dilution = N/(N +k*M)
        warrant[i] = bsm_call_value(New_FirmValue[i]/N,100,1,0.1,0.2)*dilution
        New_FirmValue[i] = Guess_FirmValue + warrant[i]
        Guess_FirmValue - New_FirmValue[i] == 0
    return warrant                                  

print(warrant_1unobservable(100,100,1,0.1,0.2,1,100,10, 1000)) 
Please, advise me this issue.
Reply
#2
(Feb-06-2018, 02:11 PM)Alberto Wrote: The problem is that 'New_FirmValue' is defined after the function.

It would be helpful if you were to post the actual code mentioned in the error. Where and how is New_FirmValue defined? Also, please post the entire error code, not just a small snippet.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
Thanks for the answer. The whole code is provided at the end of the post. "New_FirmValue" is defined in line 15.

Error:
Guess_FirmValue = S0*N def warrant_1unobservable(S0, K, T, r, sigma, k, N, M, Iteration): for i in range(1, Iteration): dilution = N/(N +k*M) warrant[i] = bsm_call_value(New_FirmValue[i]/N,100,1,0.1,0.2)*dilution New_FirmValue[i] = Guess_FirmValue + warrant[i] Guess_FirmValue - New_FirmValue[i] == 0 return warrant print(warrant_1unobservable(100,100,1,0.1,0.2,1,100,10, 1000)) Traceback (most recent call last): File "<ipython-input-50-d418690a1436>", line 11, in <module> print(warrant_1unobservable(100,100,1,0.1,0.2,1,100,10, 1000)) File "<ipython-input-50-d418690a1436>", line 5, in warrant_1unobservable warrant[i] = bsm_call_value(New_FirmValue[i]/N,100,1,0.1,0.2)*dilution NameError: name 'New_FirmValue' is not defined
Reply
#4
He means the rest of the code.

Don't import modules in functions.
Don't use mixed camel case with underscores.
Don't use upper case for variable names.
Use plural for sequences like lists or tupels.
Just put the lists also as argument to your function.
Try to think different.
You're inside a function, the world outside is only reachable with the supplied arguments.
Of course you can do more with Python, because it's dynamic. The problem with your code is,
that you depend on two lists, which are defined outside of the function at some time, maybe also far
away from the function definition.
The caller should have the responsibility to supply the function with everything, which it needs to run.
This makes your code more robust and reusable.


def warrant_1unobservable(S0, K, T, r, sigma, k, N, M, Iteration, firm_values, guess_firm_values):
    # code
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020