フィルターのクリア

How to create subsets?

2 ビュー (過去 30 日間)
Luca
Luca 2021 年 4 月 21 日
回答済み: Harimurali 2023 年 10 月 17 日
Hi,
I have a dataset of couple of thousend stocks, which is attached to this question. I calculated the market capitalisation of each stock for a year.
My goal know is to create 5 portfolios. Where every portfolio holds 20% of the stocks according to their size. The size is the market capitalisation. The portfolio should be recalculate every year. So the portfolio with the biggest 20 % of the stocks should incldude the biggest 20% for every year.
Unfortunatly I' dont know how to do it. I was thinking about the splitapply() function. But i did not find a function for this.
Data description:
ID is stands for the diffrent stocks
years are the oberved years (1962-2016)
sumMC is the market capitalization of each stock in every year
Does someone know how to do it?
Thanks in advance

回答 (1 件)

Harimurali
Harimurali 2023 年 10 月 17 日
Hi Luca,
I understand that you want to create five portfolios, where each portfolio holds 20% of the stocks according to their market capitalization. The portfolio should be recalculated every year, such that the portfolio with the biggest 20% of the stocks should include the biggest 20% for each year.
This can be achieved by following these steps:
  • Create a for loop where the loop index goes from start year to the end year.
  • For each loop, filter the table such that the table only contains rows corresponding to the loop index year.
Here is the sample MATLAB code for filtering the table inside the loop assuming the loop variable is “year”:
yearFilteredTable = subset2(subset2{:, 2} == year, :);
In this way, five portfolios can be created for a year, where each portfolio holds 20% of the stocks according to their market capitalization for that particular year.
I hope this helps.

カテゴリ

Help Center および File ExchangePortfolio Optimization and Asset Allocation についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by