英文:
log returns (almost) same as percent change
问题
我有这个函数来计算收益的对数。它按预期工作。
def log_returns(prices):
return np.log(prices / prices.shift(1))
data.apply(lambda x: log_returns(x))
返回的值与pct_change
方法非常接近。这是预期的吗?
data.pct_change()
英文:
I have this function to calculate log of returns. It works as expected.
def log_returns(prices):
return np.log(prices / prices.shift(1))
data.apply(lambda x: log_returns(x))
The values returned are very close to pct_change method. Is this expected?
> data.pct_change()
答案1
得分: 2
是的,这确实不太奇怪。对于一个小的y,有y ≈ log (1+y)。更多信息请参见此 Mathematics Exchange 帖子。
百分比变化的计算方式是x<sub>i+1</sub>/x<sub>i</sub>-1,而您计算log(x<sub>i+1</sub>/x<sub>i</sub>)。因此,如果我们用y替代y = x<sub>i+1</sub>/x<sub>i</sub>-1,我们会看到近似值出现。
英文:
Yes, this is indeed not that strange. For a small y, it holds that y ≈ log (1+y). See for more information this Mathematics Exchange post.
A percentage change is calculated as x<sub>i+1</sub>/x<sub>i</sub>-1, whereas you calculate log(x<sub>i+1</sub>/x<sub>i</sub>). If we thus substitute y for y = x<sub>i+1</sub>/x<sub>i</sub>-1, we see the approximation pop up.
答案2
得分: 2
这是代码的一部分,我将为您翻译其中的非代码部分:
"It is, for small variations in the natural log are almost equal to percentage change, that's not a code issue."
这句话的意思是:对于自然对数的微小变化,几乎等于百分比变化,这不是代码问题。
"Since :"
这是一个过渡词,表示接下来将提供一个解释或原因。
"log(A/B) = log(A) - log(B)"
这是一个数学公式,表示log(A/B)等于log(A)减去log(B)。
"and in your case, A is equal to some small change e
of B."
在您的情况下,A等于B的一些小变化e
。
"log(A/B) = log(A) - log(B) = log(B(1+e)) - log(B)"
"log(A/B) = log(B) + log((1+e)) - log(B) = log(1+e)"
对于小值的e
,这意味着log
在1附近是一个很好的近似。
"log(1+e) ≈ e"
这表示对于小的e
值,log(1+e)
近似等于e
。
"See for yourself with this code :"
您可以使用以下代码自行查看:
"import pandas as pd
import numpy as np
small = np.linspace(0.01, 0.1, 100)
df = pd.DataFrame({"vals" : small})"
这部分代码用于创建一个包含数值的DataFrame。
"df["changes"] = df["vals"].pct_change()"
这部分代码计算了数值列的百分比变化。
"df["log div"] = np.log(df["vals"]/df["vals"].shift())"
这部分代码计算了数值列的自然对数除法。
"diff_log = np.log(df["vals"]) - np.log(df["vals"].shift())"
这部分代码计算了数值列的自然对数差异。
"diff_log = diff_log[~np.isnan(diff_log)]"
这一行代码删除了包含NaN值的差异。
"log_div = df["log div"].dropna().values"
这部分代码获取了自然对数除法的数值。
"assert(np.allclose(log_div, diff_log))"
这部分代码用于断言自然对数除法和差异是否非常接近。
"and df.head(10)
:"
这是一个指示,接下来将展示DataFrame的前10行数据。
希望这些翻译对您有所帮助。如果您需要更多解释或有其他问题,请随时提问。
英文:
It is, for small variations in the natural log are almost equal to percentage change, that's not a code issue.
Since :
log(A/B) = log(A) - log(B)
and in your case, A is equal to some small change e
of B.
log(A/B) = log(A) - log(B) = log(B(1+e)) - log(B)
log(A/B) = log(B) + log((1+e)) - log(B) = log(1+e)
For small values of e
, meaning that the log
is a good approx. around 1
log(1+e) ≈ e
For a more mathy explanation, see this SO post.
See for yourself with this code :
import pandas as pd
import numpy as np
small = np.linspace(0.01, 0.1, 100)
df = pd.DataFrame({"vals" : small})
df["changes"] = df["vals"].pct_change()
df["log div"] = np.log(df["vals"]/df["vals"].shift())
diff_log = np.log(df["vals"]) - np.log(df["vals"].shift())
df["diff log"] = diff_log
diff_log = diff_log[~np.isnan(diff_log)]
log_div = df["log div"].dropna().values
assert(np.allclose(log_div, diff_log))
and df.head(10)
:
values changes log div diff log
0 0.010000 NaN NaN NaN
1 0.010909 0.090909 0.087011 0.087011
2 0.011818 0.083333 0.080043 0.080043
3 0.012727 0.076923 0.074108 0.074108
4 0.013636 0.071429 0.068993 0.068993
5 0.014545 0.066667 0.064539 0.064539
6 0.015455 0.062500 0.060625 0.060625
7 0.016364 0.058824 0.057158 0.057158
8 0.017273 0.055556 0.054067 0.054067
9 0.018182 0.052632 0.051293 0.051293
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论