2

The following code is meant to find for how much Bitcoin one is meant to buy in order to break even given the expected inflation, the holding period, and the amount of savings in dollars:

#!/usr/bin/env python3

from sympy import Eq, EmptySet, init_printing, symbols, solveset
from sys import float_info

init_printing(use_unicode=True)

x = symbols("x", real=True, positive=True)
years = float(input("How many years, i.e. the time horizon of your investment?\n"))
times = float(input("How many times do you intend to multiply your fiat money with Bitcoin?\n"))
times_yearly = times / years#How much on average Bitcoin will multiply per year
cpi = float(input("What yearly inflation do you expect to happen throughout the period?\n")) / 100


a = float(input("What is the amount of savings that you start with?\n"))
i = 1
while i <10000:
    left = a
    right = ((times_yearly * x + (a - x)) * (1 - cpi)) * years
    eqn = Eq(left, right)
    if solveset(eqn) != EmptySet:
        print("In order to break even you need to buy ")
        print(solveset(eqn))
        print(a)
    a = a + float_info.min
    i = i + 1

When the program is fed with years=3, times=4, cpi=8, and a=1000 it does not find a solution. Because of it I introduced a loop that is mean to find a solution for a that is minimally larger than 1000. Sadly, to no avail. How might I find approximate solution of this equation? Is sympy a good tool for this job? Is my code correct or have I missed something?

John Smith
  • 835
  • 1
  • 7
  • 19
  • 1
    I'm not quite sure I get what exactly you're trying to compute. How much money to invest such that after `years` years you have the same amount as before? What exactly does 'multiplying your money with Bitcoin' mean? – isaactfa May 01 '22 at 20:07
  • @isaactfa Yes, how much to put in Bitcoin in order to break even. When times is `2` it means that after the time `years` your Bitcoin will be worth two times its initial value in dollar terms. But in the meantime the dollar has go down which is reflected in my equation by `cpi`. – John Smith May 02 '22 at 04:47

1 Answers1

1

The problem here is that you defined that x is only allowed to be positive (positive=True when defining x as symbol). But the solution to your equation with the given numbers is x equals -1913.04.

I guess you have to double check your equation OR it was a bad investment :)

from sympy import solve, init_printing, symbols, solveset, Eq, EmptySet
from sys import float_info

init_printing(use_unicode=True)

x = symbols("x", real=True) # I removed the positive=True statement here
years = float(input("How many years, i.e. the time horizon of your investment?\n"))
times = float(input("How many times do you intend to multiply your fiat money with Bitcoin?\n"))
times_yearly = times / years#How much on average Bitcoin will multiply per year
cpi = float(input("What yearly inflation do you expect to happen throughout the period?\n")) / 100


a = float(input("What is the amount of savings that you start with?\n"))

left = a
right = ((times_yearly * x + (a - x)) * (1 - cpi)) * years
eqn = Eq(left, right)
print('Solution for x: ', solveset(eqn))

Output:

How many years, i.e. the time horizon of your investment?
 3
How many times do you intend to multiply your fiat money with Bitcoin?
 4
What yearly inflation do you expect to happen throughout the period?
 8
What is the amount of savings that you start with?
 1000

Solution for x:  {-1913.04347826087}

After discussion in chat:

from sympy import solve, init_printing, symbols, solveset, Eq, EmptySet
from sys import float_info

x = symbols("x", real=True)

years = list(range(1,5,1)) 
multiplier = list(range(1,5,1)) 
infl = [8/100, 4/100, 2/100]
start_cash = [1000, 10000, 100000]

res=[]
for year in years:
    for m in multiplier:
        for cpi in infl:
            for a in start_cash:             
                left = a
                right = ( x*m + (a-x) ) * ((1-cpi)**year)
                eqn = Eq(left, right)
                out = solveset(eqn)
                if out:
                    res.append([year,m,cpi,a,out.args[0]])
                    print(f"{year=} {m=} {cpi=} {a=} --> {out=}")

df = pd.DataFrame(res, columns=['years', 'multiplier', 'inflation', 'startcash', 'result'])
df['result'] = df['result'].astype(int) 

output = df.pivot(index=['years','multiplier', 'inflation'], columns='startcash', values='result')
print(output)
startcash                   1000    10000   100000
years multiplier inflation                        
1     2          0.02           20     204    2040
                 0.04           41     416    4166
                 0.08           86     869    8695
      3          0.02           10     102    1020
                 0.04           20     208    2083
                 0.08           43     434    4347
      4          0.02            6      68     680
                 0.04           13     138    1388
                 0.08           28     289    2898
2     2          0.02           41     412    4123
                 0.04           85     850    8506
                 0.08          181    1814   18147
      3          0.02           20     206    2061
                 0.04           42     425    4253
                 0.08           90     907    9073
      4          0.02           13     137    1374
                 0.04           28     283    2835
                 0.08           60     604    6049
3     2          0.02           62     624    6248
                 0.04          130    1302   13028
                 0.08          284    2842   28421
      3          0.02           31     312    3124
                 0.04           65     651    6514
                 0.08          142    1421   14210
      4          0.02           20     208    2082
                 0.04           43     434    4342
                 0.08           94     947    9473
4     2          0.02           84     841    8416
                 0.04          177    1773   17737
                 0.08          395    3958   39588
      3          0.02           42     420    4208
                 0.04           88     886    8868
                 0.08          197    1979   19794
      4          0.02           28     280    2805
                 0.04           59     591    5912
                 0.08          131    1319   13196
Rabinzel
  • 7,757
  • 3
  • 10
  • 30
  • What might be wrong with my equation? Would you propose an equation that would help me compute the amount of Bitcoin needed to offset the inflation or, in other words, to break even? – John Smith May 02 '22 at 06:37
  • 1
    wow I must have been tired, glad you corrected it, thanks! I didn't think of a break even but wanted to know if there is something wrong with your equation and did vary your 4 input variables. I'll update my answer with that so you can have a look and I look into your new question. – Rabinzel May 02 '22 at 08:34
  • There must be something wrong with my equation. Intuitively, I would expect something like: out of $1000 put $100 in Bitcoin(that will quadruple in price) ... I noticed in your data that the result is always either negative or larger than the startcash whenever the number of `years` is not `1`. On the other hand when I run my my script with `years = 1` it gives a small positive result - the one you would expect from a financial advisor. But I cannot reproduce it in your script by tweaking `years = list(map(float, range(1,5,1)))`. – John Smith May 02 '22 at 10:19
  • 1
    did you come up yourself with that equation ? Can't really give you advice on that one. I really agonized about your question and all I came up with is you did the right thing (regarding to the code and what you calc). search a `x` (how much bitcoin you need to buy) where the equation gets equal to your starting cash given the other parameters. BUT I just noticed (and I think that is not correct) if your variable `times_yearly` gets `1` (`years` equals `times`), `x` will disappear from the equation (which always leads to "EmptySet") – Rabinzel May 02 '22 at 10:33
  • 1
    oh wait, big mistake. My `cpi` is wrong in the DOE. I forgot to divide that value by `100` like you do. I'll correct that – Rabinzel May 02 '22 at 10:38
  • Yes, I myself did think up this. The equation can be simplified to `(times_yearly * x + a - x) * (1 - cpi)` but it still gives unexpected results. You are right about disappearing `x`. – John Smith May 02 '22 at 11:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/244399/discussion-between-rabinzel-and-john-smith). – Rabinzel May 02 '22 at 11:05