Stata - 从每个条件的其他观察中有效地分配最大/最小值

时间:2013-07-12 19:37:30

标签: stata

在Stata中,我希望使用以下条件从 [[其他观察]] 有效地指定 [[bid_price]] 的最大值:

  1. [[bid_time]] < [时间] < min( [[bid_timelimit]] [[bid_timecanceled]]
  2. [[stock]] = [股票]
  3. [[bid_price]] 已设置
  4. 在上文中, [[]] 表示来自其他观察的变量, [] 表示来自此观察的变量)

    这是我的代码:

    gen maxbidprice=.
    
    su no
    
    forvalues i = `r(min)'/`r(max)'{
        disp `i'
        gen double current = time[`i']
        egen bidtag=tag(bid_price) if stock==stock[`i'] & bid_price!=. & current>bid_time & current<bid_timelimit & current<=bid_timecanceled
        quietly su bid_price if bidtag
        replace maxbidprice = r(max) if no==`i'
        drop bidtag current
    }
    

    我认为我的代码非常低效。数据集的大小超过30k,此代码的运行时间为几个小时。它似乎工作,但我认为应该有更高效的代码。

    我不应该销毁原始表,只需添加一个变量maxbidprice就可以满足所有观察的某些标准。重点是从与某些条件匹配的其他观测值中分配一个值。

    有人可以提出替代方案吗?

    示例数据:

    no,time,price,quantity,seller_pid,buyer_pid,bid_no,bid_price,bid_quantity,bid_time,bid_timelimit,bid_timecanceled,bid_pid,pid,action,stock
    300,31oct2012 13:42:03,10000,10,1919,1545,,,,,,,,1919,3,3
    301,31oct2012 13:42:03,10000,30,1919,454,,,,,,,,1919,3,3
    302,31oct2012 13:42:05,1000,10,,,152,1000,10,31oct2012 13:42:05,04nov2012 00:00:00,31oct2012 13:48:27,2450,2450,1,1
    303,31oct2012 13:42:06,10000,10,1919,1545,,,,,,,,1919,3,3
    304,31oct2012 13:42:06,10000,20,1919,1252,,,,,,,,1919,3,3
    305,31oct2012 13:42:08,10000,18,1919,1648,,,,,,,,1919,3,3
    306,31oct2012 13:42:15,10000,4,1919,2151,,,,,,,,2151,4,1
    307,31oct2012 13:42:15,10000,10,2450,2151,,,,,,,,2151,4,1
    308,31oct2012 13:42:23,6500,15,1919,655,,,,,,,,1919,3,1
    309,31oct2012 13:43:58,6000,10,1919,1127,,,,,,,,1919,3,1
    310,31oct2012 13:44:15,5000,82,1919,1842,,,,,,,,1919,3,1
    311,31oct2012 13:44:41,5000,10,,,153,5000,10,31oct2012 13:44:41,04nov2012 00:00:00,31oct2012 23:36:58,2450,2450,1,1
    312,31oct2012 13:46:21,5000,100,,,154,5000,100,31oct2012 13:46:21,16nov2012 00:00:00,01nov2012 00:18:04,1919,1919,1,1
    313,31oct2012 13:46:25,5000,3,733,1842,,,,,,,,733,3,1
    314,31oct2012 13:46:28,5000,20,,,155,5000,20,31oct2012 13:46:28,02nov2012 00:00:00,31oct2012 14:14:54,1721,1721,1,1
    315,31oct2012 13:46:54,7000,10,,,156,7000,10,31oct2012 13:46:54,06nov2012 00:00:00,31oct2012 20:36:08,209,209,1,3
    316,31oct2012 13:48:11,9700,10,,,,,,,,,,1373,2,2
    317,31oct2012 13:48:14,6000,10,,,157,6000,10,31oct2012 13:48:14,06nov2012 00:00:00,31oct2012 13:55:07,209,209,1,1
    318,31oct2012 13:48:55,10000,10,,,,,,,,,,1373,2,3
    319,31oct2012 13:49:53,10000,30,,,,,,,,,,1919,2,1
    320,31oct2012 13:50:24,9000,50,,,158,9000,50,31oct2012 13:50:24,04nov2012 00:00:00,31oct2012 17:15:46,1919,1919,1,2
    321,31oct2012 13:50:29,10000,10,1919,1725,,,,,,,,1725,4,1
    322,31oct2012 13:50:42,9000,40,,,159,9000,40,31oct2012 13:50:42,04nov2012 00:00:00,31oct2012 17:15:48,1919,1919,1,3
    323,31oct2012 13:51:10,6000,10,,,160,6000,10,31oct2012 13:51:10,04nov2012 00:00:00,31oct2012 14:42:27,2450,2450,1,1
    324,31oct2012 13:51:14,10000,20,,,,,,,,,,1919,2,2
    325,31oct2012 13:51:23,10000,20,,,,,,,,,,1919,2,2
    326,31oct2012 13:51:54,9000,20,,,161,9000,20,31oct2012 13:51:54,04nov2012 00:00:00,31oct2012 17:15:50,1919,1919,1,3
    327,31oct2012 13:52:05,10000,8,1725,1648,,,,,,,,1725,3,3
    328,31oct2012 13:52:05,10000,2,1725,1648,,,,,,,,1725,3,3
    329,31oct2012 13:52:39,9900,10,,,162,9900,10,31oct2012 13:52:39,04nov2012 00:00:00,31oct2012 13:53:16,277,277,1,1
    330,31oct2012 13:53:12,9700,10,,,163,9700,10,31oct2012 13:53:12,04nov2012 00:00:00,31oct2012 14:31:31,277,277,1,2
    

1 个答案:

答案 0 :(得分:3)

以下内容应该有效。关键是使用collapseif来查找符合条件的最大bid_price

/* make some data */
clear
set seed 2001
set obs 10
generate stock = _n  
expand 100
bysort stock: generate time = _n
expand 100
generate bid_time = time + 10*uniform() - 5
generate bid_timelimit = time + 100*uniform() - 50
generate bid_timecancelled = time + 100*uniform() - 50
generate bid_price = 100 + 50*uniform() - 25

/* find max active bid */
tempfile original_data
save `original_data'
collapse (max) bid_price ///
    if (time > bid_time) & (time < min(bid_timelimit, bid_timecancelled)), ///
    by(stock time)  
merge 1:m stock time using `original_data'

/* check results */    
list in 1/10