Compound interest
Example 3: Compound interest¶
The third example calculates the compound interest \(C\) starting from a principal value \(P\) with annualt interest \(I\) after \(N\) years:
Very common formula in anything related to finance.
Again, we start with the plain code, as you would implement it right away:
calc_compound_interest.py | |
---|---|
1 2 3 4 5 6 7 8 |
|
You can run this example with $ python ci.py
and it should print 7908.47
. Which is the compound
interest after 10 years if you started with 10000 units that grow by 6% each year.
There are several problems with this solution:
1. You need to change the code to run it for other inputs
2. Units are unclear! principal
is a currency, but that actually does not matter. The real problem
is the interest
. Is it decimal or in %?
Annotate with lmrtfy¶
Using lmrtfy, you would annotate the script as follows:
calc_compound_interest_lmrtfy.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Now, we run python calc_compound_interest_lmrtfy_1.py
to generate the profile.
Warning
The type annotation are not enforced when run locally. LMRTFY checks the types and units only if jobs are submitted through its API. This guarantees that you can run your code without our service.
Deployment¶
After creating the profile we can easily deploy with
$ lmrtfy deploy examples/compound_interest/calc_compound_interest.py --local
Call compound_interest
from code¶
Similar to the other examples we just need to import the catalog
and call the correct function:
call_compound_interest.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
<your_namespace>
is your private namespace on LMRTFY, which is typically your nickname. Available namespaces are shown when importingcatalog
or when callingcatalog.update()
.
Call compound_interest
from the CLI¶
The output should be similar to this:
INFO Profile_id to be used for requests: <profile_id>
The <profile_id>
is important to submit jobs.
To submit a job you are currently required to save the input parameters as JSON (e.g. input.json
):
{
"argument_values": {
"annual_interest": 6.0,
"principal": 5000.0,
"years": 10
},
"argument_units": {
"annual_interest": "%"
}
}
Now, we have everything that is needed to start a job:
$ lmrtfy submit <profile_id> input.json
The job id for this job is printed to the terminal:
INFO Job-id: <job_id>
We need the <job_id>
later to fetch the results from the computation.
Alternative Annotation¶
A more compact but working alternative is to create the result as follows:
Alternative annotation | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
It's not necessarily prettier to look at, but also works!