I would like to implement geometric progression using Python / Pandas / Numpy.
Here is what I did:
N = 10
n0 = 0
n_array = np.arange(n0, n0 + N, 1)
u = pd.Series(index = n_array)
un0 = 1
u[n0] = un0
for n in u.index[1::]:
u[n] = u[n-1] * 1.2
print(u)
I get:
0 1.000000
1 1.200000
2 1.440000
3 1.728000
4 2.073600
5 2.488320
6 2.985984
7 3.583181
8 4.299817
9 5.159780
dtype: float64
I wonder how I could avoid using a for loop.
I looked at
https://fr.wikipedia.org/wiki/Suite_g%C3%A9om%C3%A9trique
and found that u_n can be expressed as: u_n = u_ {n_0} * q ^ {n-n_0}
So i did it
n0 = 0
N = 10
n_array = np.arange(n0, n0 + N, 1)
un0 = 1
q = 1.2
u = pd.Series(map(lambda n: un0 * q ** (n - n0), n_array), index = n_array)
This is fine ... but I'm looking for a way to define it in a repeating way, like
u_n0 = 1
u_n = u_{n-1} * 1.2
But I don’t see how to do this using Python / Pandas / Numpy ... I wonder if this is possible.
source
share