source: db-scripts/p6-relational-algebra/p6_q4_annual_investing_diversification_concentration_ra.txt@ b3d5fed

Last change on this file since b3d5fed was 89156c1, checked in by Andrej <asumanovski@…>, 3 months ago

Phase 6 documentation

  • Property mode set to 100644
File size: 2.9 KB
Line 
1
2IB <- pi_{iu.user_id, u.username, u.email}
3 (investor_users iu bowtie_{iu.user_id = u.user_id} users u)
4
5AAL <- pi_{a.user_id, a.ticker_symbol,
6 COALESCE(a.quantity,0)->quantity,
7 COALESCE(a.buy_price,0)->buy_price,
8 COALESCE(a.quantity,0)*COALESCE(a.buy_price,0)->invested_amount,
9 a.buy_date}
10 (sigma_{YEAR(a.buy_date)=Y}(assets a))
11
12TR <- gamma_{user_id, ticker_symbol;
13 SUM(quantity)->total_quantity,
14 SUM(invested_amount)->total_invested_amount,
15 COUNT(*)->lot_count,
16 MIN(buy_date)->first_buy_date,
17 MAX(buy_date)->last_buy_date}(AAL)
18
19PT <- gamma_{user_id;
20 SUM(total_invested_amount)->annual_total_invested,
21 SUM(lot_count)->annual_lot_count,
22 COUNT(*)->distinct_tickers}(TR)
23
24W0 <- TR bowtie_{TR.user_id = PT.user_id} PT
25W1 <- alpha_{total_invested_amount/NULLIF(annual_total_invested,0)->position_weight}(W0)
26W <- omega_{PARTITION BY user_id ORDER BY total_invested_amount DESC, ticker_symbol ASC;
27 DENSE_RANK()->position_rank}(W1)
28
29C <- gamma_{user_id;
30 SUM(position_weight*position_weight)->hhi_concentration,
31 MAX(position_weight)->top_position_weight,
32 MAX_IF(ticker_symbol, position_rank=1)->top_ticker}(W)
33
34IBM <- IB x M
35AY <- sigma_{YEAR(a.buy_date)=Y}(assets a)
36MI0 <- IBM leftouterjoin_{IBM.user_id=a.user_id AND IBM.month_no=MONTH(a.buy_date)} AY
37MI <- gamma_{user_id, month_no;
38 SUM(COALESCE(a.quantity,0)*COALESCE(a.buy_price,0))->monthly_invested_amount}(MI0)
39MS <- gamma_{user_id;
40 AVG(monthly_invested_amount)->avg_monthly_contribution,
41 STDDEV_SAMP(monthly_invested_amount)->contribution_stddev,
42 COUNT_IF(monthly_invested_amount>0)->active_investing_months}(MI)
43
44R0 <- IB
45 leftouterjoin_{IB.user_id=PT.user_id} PT
46 leftouterjoin_{IB.user_id=C.user_id} C
47 leftouterjoin_{IB.user_id=MS.user_id} MS
48R1 <- alpha_{COALESCE(annual_total_invested,0)->annual_total_invested_nz,
49 COALESCE(annual_lot_count,0)->annual_lot_count_nz,
50 COALESCE(distinct_tickers,0)->distinct_tickers_nz,
51 COALESCE(avg_monthly_contribution,0)->avg_monthly_contribution_nz,
52 COALESCE(active_investing_months,0)->active_investing_months_nz,
53 COALESCE(active_investing_months,0)/12.0->activity_ratio,
54 COALESCE(hhi_concentration,0)->hhi_concentration_nz,
55 1-COALESCE(hhi_concentration,1)->diversification_index,
56 COALESCE(top_position_weight,0)->top_position_weight_nz,
57 COALESCE(contribution_stddev/NULLIF(avg_monthly_contribution,0),0)->contribution_volatility_cv}(R0)
58R <- omega_{ORDER BY diversification_index DESC,
59 annual_total_invested_nz DESC,
60 active_investing_months_nz DESC,
61 user_id ASC;
62 DENSE_RANK()->investing_annual_rank}(R1)
63
64
Note: See TracBrowser for help on using the repository browser.