python大数_使用python的金融大数定律

这篇博客介绍了如何在金融领域利用Python实现大数定律。通过Python编程,解释了大数定律的概念并提供了相关示例。
摘要由CSDN通过智能技术生成

python大数

重点(Top highlight)

大数定律:(The Law of Large Numbers:)

Have you ever wondered how casinos make money? The law of large numbers is a concept well known right from the early stages of Statistics. But it is not just in Statistics that we find its use. Its significance is felt in various fields of life ranging from gambling to fixing the price for car insurance or even in guessing who might win the poll.

您是否想过赌场如何赚钱? 大数定律是从统计学的早期阶段就广为人知的概念。 但是,我们发现它的用途不仅仅在于统计。 从赌博到确定汽车保险价格,甚至猜测谁可能赢得民意测验,在生活的各个领域中都感受到了它的重要性。

The law of large numbers points out that as the number of trials or observations increases, the observed or the actual probability approaches the expected mean or the theoretical value. This means that as the sample size grows, the mean of the sample gets closer to the average of the total population.

大量定律指出,随着试验或观察次数的增加,观察到的或实际概率接近预期的均值或理论值。 这意味着随着样本数量的增加,样本的平均值越来越接近总人口的平均值。

The law of large numbers sees its application in various fields of analyses ranging from financial to business to statistics. In the business context, for example, growth rates tend to be a representation of The law of large numbers. This is seen in the form of growth rates more or less converging to the growth rates of the economy. Similar is seen in the financial context where the companies with huge market capitalization more or less become stagnant and don’t really see the growth observed previously.

大数定律将其应用于从金融到商业再到统计的各种分析领域。 例如,在商业环境中,增长率往往代表着“大数定律”。 这可以通过增长率或多或少地收敛于经济增长率的形式看出。 在财务背景下,市值巨大的公司或多或少停滞不前,并没有真正看到以前的增长。

A very powerful and widely quoted example for understanding the concept is by observing the flipping of a coin. The initial few tosses might not necessarily give a 50–50 chance for heads or tails. There might be a higher cumulative probability towards one outcome. So the relative frequency can be much farther from the expected 50% in let’s say 10, 20, 50, 100 flips. But on the other hand, let’s say we flip a coin like a 1000 times or 100000 times, then the cumulative probability of getting a heads is much closer to the expected value of 50%.

理解硬币概念的一个非常有力且被广泛引用的例子是观察硬币的翻转。 最初的几次抛掷不一定会给头部或尾部带来50–50的机会。 一个结果可能有更高的累积概率。 因此,相对频率可以比预期的50%远得多,例如10、20、50、100次翻转。 但是,另一方面,假设我们将硬币翻转了1000或100000次,那么获得正面的累积概率就非常接近50%的期望值。

I have tried to simulate this by writing a code in python.

我试图通过在python中编写代码来模拟这一点。

On observing the cumulative probabilities for different number of flips, we see the probability converging to an expected value of 50.0% when the overall flips start growing higher along with the distribution approaching a normal distribution.

在观察不同翻转次数的累积概率时,我们看到当整个翻转开始随着分布接近正态分布而开始变得更高时,概率收敛到50.0%的期望值。

For just 10 Flips we see the below:

仅10次翻转,我们就会看到以下内容:

Image for post
Probability of heads on flipping the coin 10 times
掷硬币10次的正面概率

For 1000 Flips we see the below:

对于1000次翻转,我们看到以下内容:

Image for post
Probability of heads converging to 50% on flipping the coin 1000 times
将硬币翻转1000次后,头部的概率收敛至50%

The law of large numbers thus helps us understand why a single outcome alone can’t be trusted. That is why we need to be skeptical of the observations that we get from sample populations, especially ones with low numbers.

因此,大数定律有助于我们理解为什么不能单靠一个结果。 这就是为什么我们需要怀疑我们从样本总体中获得的观察结果,尤其是那些数量较少的观察结果。

平均法则: (The Law of Averages:)

Sometimes people tend to interchange The law of large numbers with the law of averages. They are not the same. While the law of large numbers is a mathematical theorem with a theoretical certainty of the results converging to the expected mean with increasing number of trials (large trials), whereas law of averages is more or less a layman term with a belief that the outcomes of the trials will even out within a small sample. This is not a mathematical principle but rather wishful thinking. And this very bad statistics leads to the concept of ‘Gambler’s fallacy’.

有时人们倾向于将大数定律与平均数定律互换。 它们是不相同的。 大数定律是一个数学定理,随着试验次数(大型试验)的增加,结果的理论确定性会收敛到预期均值,而平均值定律或多或少是一个外行术语,并相信这些试验将在一个很小的样本中实现均衡。 这不是数学原理,而是一厢情愿的想法。 这种非常糟糕的统计数据导致了“赌徒谬论”的概念。

赌徒的谬误: (Gambler’s Fallacy:)

This fallacy is a belief that a certain outcome is likely to happen due to a wrongly perceived underlying principal of the law of averages. This leads the ‘gambler’ to believe that since a specific outcome has not happened recently, there will be a probability for it to happen now so as to average out. One will need to understand that there is no relation between the occurrences of the various outcomes and that they are independent of each other.

这种谬论是一种信念,即由于对均值定律的基本原理的错误理解而可能会产生某种结果。 这使“赌徒”相信,由于最近没有发生过特定的结果,因此有可能现在就发生,以求平均。 人们将需要理解,各种结果的发生之间没有关系,并且它们彼此独立。

While the law of large numbers will help the casino with the earnings over a large number of spins even if it loses on a single or few spins of roulette, the law of averages will not help the gambler by assuming that the wheel will now land on black as it previously landed a few times on red. Therefore it is purely random and there is no real concept of law of averages.

尽管大数定律即使在一次或几次旋转的轮盘赌中失败,也将使赌场获得大量旋转收益,即使平均数定律也不会通过假设轮子现在落在赌徒身上而对赌徒有所帮助黑色,因为它之前几次落在红色上。 因此,它纯粹是随机的,没有真正的平均值定律概念。

大数法在贸易和金融中的应用: (Law of large numbers’ applications in trading and finance:)

The law of large numbers sees its applications in the world of trading and finance too. It suggests that a large number of trades with a higher Sharpe ratio (the reward to risk ratio) will tend to be more effective than a smaller number of trades.

大数定律也在贸易和金融领域也看到了它的应用。 这表明,大量具有较高Sharpe比率(风险收益率)的交易将比少量交易更有效。

As George Soros would put it, what matters is not whether you are right or wrong, rather how much will you make when right and how much will you lose when one is wrong makes your strategy more relevant. This boils down to the reason for effective risk management. In this scenario, basis the law of large numbers, a trader can be wrong the majority of the time but still be profitable.

正如乔治·索罗斯(George Soros)所说,重要的不是你是对还是错,而是当正确时你会赚多少,而当一个错误时你会损失多少,这会使你的策略更有意义。 这归结为有效风险管理的原因。 在这种情况下,根据大数定律,交易者大多数时候可能会犯错,但仍然可以获利。

In a lot of these scenarios, the major challenge is to understand how long a period is actually large enough. Experience tells us that backtesting on the longest period possible gets us closer to the mean annualized returns. Let us take the examples of lower frequency trading strategies. Let us try to understand how long it takes to realize a truer Sharpe strategy and its variance.

在许多这样的情况下,主要的挑战是要了解一个周期实际上足够大的时间。 经验告诉我们,尽可能长的回溯测试使我们更接近平均年化收益。 让我们以较低频率交易策略为例。 让我们尝试了解实现更真实的Sharpe策略及其变化所需的时间。

Let’s assume that a backtested strategy has a true Sharpe ratio of 2 (assuming the case as an average for the lower frequency trades). And let us try and realize this over a 40 day trading period which is broadly around 2 months. Assuming a normal distribution with mean returns of 0.1%, let us simulate and see how the Sharpe Ratios tend to be with an increasing number of trades (or trials).

让我们假设一个经过重新测试的策略的真实Sharpe比率为2(假设该情况是低频交易的平均值)。 让我们尝试在40天的交易期内(大约2个月左右)实现这一目标。 假设平均收益率为0.1%的正态分布,让我们模拟并观察随着交易次数(或试验次数)的增加,夏普比率的趋势。

The python code of it is as below.

它的python代码如下。

With just 15 trades made, we see the below Sharpe Ratios:

仅进行了15笔交易,我们就会看到以下夏普比率:

Image for post
15 trades placed
进行了15笔交易

The above histogram shows us with just a few trades, one would end up with a lesser Sharpe Ratio than what was seen through backtesting.

上面的直方图显示了只有几笔交易,而最终的夏普比率却比回测中看到的要少。

With 500 trades made, we observe the below:

进行了500笔交易,我们观察到以下几点:

Image for post
500 trades placed
500笔交易

With 2500 trades, we seen the following:

通过2500笔交易,我们看到了以下内容:

Image for post
2500 trades placed
进行了2500笔交易

From the above graphs, we can clearly see that with fewer trails, we might observe more losses than wins. But as eventually, the trials keep increasing, the Sharpe ratio is tending towards the backtested number (by tending towards a normal distribution). The trick here is again to involve in as many huge number of trials as one ideally can.

从上面的图表中,我们可以清楚地看到,随着步数的减少,我们可能会看到损失多于胜利。 但是随着试验的最终增加,夏普比率趋向于回测数(趋向于正态分布)。 理想的方法是再次进行尽可能多的试验。

行为心理学对LLN的入侵: (Behavioral psychology’s intrusion on the LLN:)

The key to the working of the law of large numbers is to firstly go in trade after trade and secondly not to interrupt it by changing the underlying assumptions time and again. Traders do tend to change the strategies with a hope to beat the markets and hence finding to maintain the self-control very hard. And to place as many trades as possible without really getting discouraged is too difficult a task. Hence the very concept of ‘Patience and Finance’ becomes more important than ever. This information overloaded era acts as a double edged sword. It on one hand helps the long term investor in making informed choices but at the same perturbs another impatient investor into a regular evaluation of the strategies. This tends the investor with proper stock choices but wrong timing to exit the trade strategy or even the trading game altogether, quite early on.

实施大数定律的关键是,首先是先进行贸易,其次是不通过反复改变基本假设来打断贸易。 交易者确实倾向于改变策略,希望能打败市场,从而发现很难保持自我控制。 在不真正灰心的情况下进行尽可能多的交易是一项艰巨的任务。 因此,“耐心和财务”这一概念比以往任何时候都更加重要。 这个信息超载的时代就像一把双刃剑。 一方面,它有助于长期投资者做出明智的选择,但同时又会扰乱另一位耐心的投资者对策略进行定期评估。 这倾向于使投资者选择适当的股票,但是在很早的时候退出交易策略甚至完全退出交易游戏的时机错误。

离别的想法: (Parting thoughts:)

However sound the trading strategies derived from backtesting might feel, it is only as good as your execution and practice of it numerous times. Trading is a job which requires the right technique and skill set, experience and a more important constant attempts at it.

无论从回测中得出的交易策略听起来多么合理,它都与您多次执行和实践一样好。 交易是一项需要正确的技术和技能,经验以及更重要的不断尝试的工作。

Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works without seeking professional advice. See our Reader Terms for details.

Towards Data Science编辑的注意事项:虽然我们允许独立作者按照我们的规则和指南发表文章,但我们不认可每位作者的贡献。 您不应在未征求专业意见的情况下依赖作者的作品。 有关详细信息,请参见我们的阅读器条款

翻译自: https://towardsdatascience.com/law-of-large-numbers-in-finance-using-python-86945eaee444

python大数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值