【问题标题】:Stata - Efficiently assigning max/min value from other observations with each conditionStata - 在每个条件下有效地分配其他观察值的最大值/最小值
【发布时间】:2013-07-12 19:37:30
【问题描述】:

在 Stata 中,我想在以下条件下有效地从 [[其他观察]] 中分配 [[bid_price]] 的最大值:

  1. [[bid_time]] [time] [[bid_timelimit]], [[bid_timecanceled]])
  2. [[库存]] = [库存]
  3. [[bid_price]] 已设置

在上面,[[]] 表示来自其他观察的变量,[] 表示来自该观察的变量)

这是我的代码:

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

【问题讨论】:

  • 所以您想在任何给定的time 处为每个stock 找到最高出价?请提供几行数据好吗?生成测试数据的代码加分。 :)
  • @RichardHerron 感谢您的关注,我刚刚添加了示例数据
  • 然后merge返回。

标签: stata


【解决方案1】:

以下应该有效。关键是使用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   

【讨论】:

  • 感谢您的建议,但我不应该破坏原始表格,而只需添加一个变量maxbidprice 满足所有观察的特定标准。很抱歉我没有提到这一点。
  • 然后merge返回。
  • 我合并了我对您对原始问题的回答的评论。
  • 你没有抓住重点。建议是使用 Stata 的合并命令。
  • @z0nam - 请试试我的代码。构成原始数据的字节或电子没有什么特别之处:-)。我认为在这种情况下,最简单的方法是将原始数据保存到临时文件中,然后找到最大有效出价,然后使用merge 命令将其添加回来,这样您就可以获得每个报价的最大有效出价。我怀疑这比您的代码快得多。
猜你喜欢
  • 2012-12-26
  • 1970-01-01
  • 2017-10-17
  • 2023-03-09
  • 2015-03-08
  • 2018-01-10
  • 2022-07-05
  • 1970-01-01
  • 2013-06-08
相关资源
最近更新 更多