简体   繁体   English

如何计算加权平均值但使用SAS排除对象本身

[英]how to calculate weighted average but exclude the object itself using SAS

There are four variables in my dataset.我的数据集中有四个变量。 Company shows the company's name. Company显示公司名称。 Return is the return of Company at day Date . ReturnCompanyDate日的回报。 Weight is the weight of this company in the market. Weight是这家公司在市场中的权重。

I want to keep all variables in the original file, and create an additional variable which is the market return (exclude Company itself).我想保留原始文件中的所有变量,并创建一个附加变量,即市场回报(不包括Company本身)。 Market return corresponding for stock 'a' is the sum of all weighted stocks' return at the same Date in the market exclude stock a.股票'a'对应的市场回报是市场上除股票a之外的所有加权股票在同一Date的回报之和。 For example, if there are 3 stocks in the market a, b and c.例如,如果市场上有 3 只股票 a、b 和 c。 Market Return for stock a is Return(b)* [Weight(b)/(weight(b)+weight(C))] + Return(C)* [weight(C)/(weight(b)+weight(C)]. Similarly, Market Return for stock b is Return(a)* [Weight(a)/(weight(a)+weight(C))] + Return(C)* [weight(C)/(weight(a)+weight(C)].股票a的市场回报为回报(b)* [权重(b)/(权重(b)+权重(C))] +回报(C)* [权重(C)/(权重(b)+权重(C) )]. 同样,股票 b 的市场回报是 Return(a)* [Weight(a)/(weight(a)+weight(C))] + Return(C)* [weight(C)/(weight(a) )+重量(C)]。

I try to use proc summary but this function cannot exclude stock a when calculate the market return for stock a.我尝试使用 proc summary 但此函数在计算股票 a 的市场回报时不能排除股票 a。

PROC SUMMARY NWAY DATA  ; 
    CLASS Date ; 

    VAR Return / WEIGHT = weight;

    OUTPUT
       OUT = output
       MEAN (Return) = MarketReturn;
RUN;

Could anyone teach me how to solve this please.谁能教我如何解决这个问题。 I am relatively new to this software, so I dont know if I should use loop or there might be some better alternative.我对这个软件比较陌生,所以我不知道我是否应该使用循环或者可能有更好的选择。

This can be done with a bit of fancy algebra.这可以通过一些花哨的代数来完成。 It's not something that's built-in, though.不过,这不是内置的东西。

Basically:基本上:

  • Construct a "total" market return构建“总”市场回报
  • Construct a stock by stock return (so just return of A)通过股票收益构造股票(所以只是 A 的收益)
  • Subtract out the portion that A contributes to total.减去 A 对总数的贡献。

Thanks to the simple math that generates these lists, it's quite easy to do this.多亏了生成这些列表的简单数学,做到这一点非常容易。

Total sum = ((mean of A*Awgt ) + (mean of remainder*sum of their weights))/(sum of Awgt + sum of rest wgts)总和 =(( A*Awgt )+(余数平均值*它们的权重总和))/(Awgt 的总和 + 其余 wgts 的总和)

So, solve that for (mean of rest*mean of rest wgts / sum of rest wgts).所以,解决这个问题(休息的平均值*休息 wgts 的平均值 / 休息 wgts 的总和)。

Exclusive sum: ((mean of all * sum of all wgts) - (mean of A * sum of A wgts)) / (sum of all wgts - sum of A wgts)独占总和:((所有 wgts 的平均值 * 所有 wgts 的总和)-(A 的平均值 * A wgts 的总和))/(所有 wgts 的总和 - A wgts 的总和)

Something like this.像这样的东西。

data returns;
  input stock $ return weight;
  datalines;
A .50 1
B .75 2
C .33 1
;;;;
run;

proc means data=returns;
  class stock;
  types () stock; *this is the default;
  weight weight;
  output out=means_out mean= sumwgt= /autoname;
run;

data returns_excl;
  if _n_=1 then set means_out(where=(_type_=0) rename=(return_mean=tot_return return_sumwgt=tot_wgts));
  set means_out(where=(_type_=1));
  return_excl = (tot_return*tot_wgts-return_mean*return_sumwgt)/(tot_wgts-return_sumwgt);
run;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM