pandas應用之分組因子暴露和分位數分析


pandas應用之分組因子暴露和分位數分析

首先感謝原書作者Mes McKinney和batteryhp網友的博文, 俺在此基礎上繼續探索python的神奇功能.

用A股的實際數據, 以書里的代碼為藍本, 做一些實證探索. 發現不少問題

  • pandas版本升級之后, 函數調用的方式必須相應地改變. 比如 pd.rolling_mean 升級為Series.rolling().mean()等等
  • tushare的數據與yahoo財經的數據格式上的差異, 需要規整化, 等等
  • 至少會有兩篇后續的博文詳細記錄探索歷程.

《利用python進行數據分析》讀書筆記--第十一章 金融和經濟數據應用(一) - 司空格子Ored - 博客園 http://www.cnblogs.com/batteryhp/p/5060381.html

第十一章 金融和經濟數據應用(一)

自從2005年以來, Python在金融行業的應用得到了迅猛的增長, 這主要受益於成熟的函數庫和經驗豐富的Python程序員. 很多機構都發現Python不僅非常適合交互式的分析環境, 而且也非常適合開發穩健的系統, 而且所需的時間要比Java或者C++少很多.
The use of Python in the financial industry has been increasing rapidly since 2005, led
largely by the maturation of libraries (like NumPy and pandas) and the availability of
skilled Python programmers. Institutions have found that Python is well-suited both
as an interactive analysis environment as well as enabling robust systems to be devel-
oped often in a fraction of the time it would have taken in Java or C++.

§1 數據規整化方面的話題(data munging topics)

前幾章里涉獵過一些不錯的金融應用領域方面的數據規整化的工具. 本章我將重點介紹這一問題領域的幾個話題.
Many helpful data munging tools for financial applications are spread across the earlier
chapters. Here I’ll highlight a number of topics as they relate to this problem domain.

操作不同頻率的時間序列(Operations with TS of different frequncies)

§2 分組變換和分析(group transforms and analysis)

§3 更多應用實例

在第九章中,已經學習了分組統計的基礎,還學習了如何對數據集的分組應用自定義的變換函數。

2.1 分組因子暴露(group factor exposure)

因子分析(factor analysis)是投資組合定量管理中的一種技術。投資組合的持有量和性能(收益與損失)可以被分解為一個或多個表示投資組合權重的因子(風險因子就是其中之一)。例如,某只股票與某個基准(比如標普500指數)的協動性被稱為其beta風險系數。下面以一個人為構成的投資的投資組合為例進行講解,它由三個隨機生成的因子(通常稱為因子載荷)和一些權重構成。

Factor analysis is a technique in quantitative portfolio management. Portfolio holdings and performance (profit and loss) are decomposed using one or more factors (risk factors are one example) represented as a portfolio of weights. For example, a stock price’s co-movement with a benchmark (like S&P 500 index) is known as its beta, a common risk factor. Let’s consider a contrived example of a portfolio constructed from 3 randomly-generated factors (usually called the factor loadings) and some weights:

2.2 十分位和四分位分析(decimal and quantile analysis)

基於樣本分位數的分析是金融分析師們的另一個重要工具,例如,股票投資組合的性能可以根據個股的市盈率被划分到四分位里(四個大小相同的象限)。通過pandas.qcutgroupby函數可以非常輕松地實現分位數分析.

在下面的這個例子里, 我們利用跟隨策略或者動量交易策略通過SPY-ETF買賣標普500指數. 你可以從yahoo財經下載歷史價格.

Analyzing data based on sample quantiles is another important tool for financial ana-
lysts. For example, the performance of a stock portfolio could be broken down into
quartiles (four equal-sized chunks) based on each stock’s price-to-earnings. Using pandas.qcut combined with groupby makes quantile analysis reasonably straightforward.

As an example, let’s consider a simple trend following or momentum strategy trading
the S&P 500 index via the SPY exchange-traded fund. You can download the price
history from Yahoo! Finance:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM