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?
Advertisement
Answer
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