由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Quant版 - how to generate truncated gamma distribution in python (转载)
相关主题
急问: return dispersion是啥?[合集] 雷曼兄弟的AVP (associate vice president)到底算个啥?
quant interview questions(statistics)[合集] A statistics question
问一个matlab画图的问题 (转载)[合集] MFE有多难?
interview questions from GS[合集] What's the meaning of ATM?
[合集] Wavelet Re: How old are you guys?[合集] 问一个简单的问题。 (转载)
[合集] one more wierd problem[合集] 能源交易跟其他交易相比有特殊的地方吗? Thanks!
[合集] 选校请教,FE of Columbia or Computational Finance of CM[合集] help on trading assistant interview
[合集] jp morgan真不象话新手问道题
相关话题的讨论汇总
话题: bins话题: shape话题: scale话题: gamma话题: truncated
进入Quant版参与讨论
1 (共1页)
m******t
发帖数: 273
1
【 以下文字转载自 Statistics 讨论区 】
发信人: myregmit (myregmit), 信区: Statistics
标 题: long time generating gamma distribution in python
发信站: BBS 未名空间站 (Thu Mar 13 19:04:24 2014, 美东)
I need to generate a truncated gamma distribution pdf curve and histogram in
python 3.
2 on win7.
import numpy as np
import matplotlib.pyplot as plt
import scipy.special as sps
shape, scale = 2., 2. # mean and dispersion
counter =0
s = []
upper_bound = 4
lower_bound = 0.5
while (counter <= 1000):
t = np.random.gamma(shape, scale, 1)
if (lower_bound <= t <= upper_bound) :
s.append(t)
counter += 1
count, bins, ignored = plt.hist(s, 50, normed=True)
// this part take s very long time
y = bins**(shape-1)*(np.exp(-bins/scale) /
(sps.gamma(shape)*scale**shape))
plt.plot(bins, y, linewidth=2, color='r')
plt.show()
I find that "y = bins*(shape-1)(np.exp(-bins/scale) / (sps.gamma(shape)scale
*shape))"
take very long time and the pop-up figure window becomes no-responding.
If I remove the lower and upper bounds for the gamma distribution, it runs
very fast.
Any help would be appreciated.
Thanks !
c*******y
发帖数: 1630
2
我这里显示瓶颈在 上面一行
%time count, bins, ignored = plt.hist(s, 50, normed=True)
CPU times: user 36.4 s, sys: 751 ms, total: 37.2 s
Wall time: 37.2 s

3.

【在 m******t 的大作中提到】
: 【 以下文字转载自 Statistics 讨论区 】
: 发信人: myregmit (myregmit), 信区: Statistics
: 标 题: long time generating gamma distribution in python
: 发信站: BBS 未名空间站 (Thu Mar 13 19:04:24 2014, 美东)
: I need to generate a truncated gamma distribution pdf curve and histogram in
: python 3.
: 2 on win7.
: import numpy as np
: import matplotlib.pyplot as plt
: import scipy.special as sps

c*******y
发帖数: 1630
3
贴到stackoverflow去问问,很多是写library的在上面晃
%prun -l 5 count, bins, ignored = plt.hist(s, 50, normed=True)
45928256 function calls (45910189 primitive calls) in 52.608
seconds
Ordered by: internal time
List reduced from 243 to 5 due to restriction <5>
ncalls tottime percall cumtime percall filename:lineno(function)
366366 14.342 0.000 14.342 0.000 weakref.py:267(__init__)
50050 6.618 0.000 15.825 0.000 artist.py:883(get_aliases)
50050 3.088 0.000 3.088 0.000 {dir}
15215201 2.842 0.000 2.842 0.000 {method 'startswith' of 'str'
objects}
467467 1.866 0.000 2.471 0.000 weakref.py:47(__init__)

3.

【在 m******t 的大作中提到】
: 【 以下文字转载自 Statistics 讨论区 】
: 发信人: myregmit (myregmit), 信区: Statistics
: 标 题: long time generating gamma distribution in python
: 发信站: BBS 未名空间站 (Thu Mar 13 19:04:24 2014, 美东)
: I need to generate a truncated gamma distribution pdf curve and histogram in
: python 3.
: 2 on win7.
: import numpy as np
: import matplotlib.pyplot as plt
: import scipy.special as sps

m******t
发帖数: 273
4
Thanks for your reply !
Would you please tell me how to get the detailed timing information and run-
time profiles for python ?
Thanks !

【在 c*******y 的大作中提到】
: 贴到stackoverflow去问问,很多是写library的在上面晃
: %prun -l 5 count, bins, ignored = plt.hist(s, 50, normed=True)
: 45928256 function calls (45910189 primitive calls) in 52.608
: seconds
: Ordered by: internal time
: List reduced from 243 to 5 due to restriction <5>
: ncalls tottime percall cumtime percall filename:lineno(function)
: 366366 14.342 0.000 14.342 0.000 weakref.py:267(__init__)
: 50050 6.618 0.000 15.825 0.000 artist.py:883(get_aliases)
: 50050 3.088 0.000 3.088 0.000 {dir}

1 (共1页)
进入Quant版参与讨论
相关主题
新手问道题[合集] Wavelet Re: How old are you guys?
[合集] 请教一个期望值还有一个积分的问题,牛人给点提示也好,谢谢[合集] one more wierd problem
Help on Garch model simulation..[合集] 选校请教,FE of Columbia or Computational Finance of CM
[合集] 请问如何了解某个领域各个ib的表现[合集] jp morgan真不象话
急问: return dispersion是啥?[合集] 雷曼兄弟的AVP (associate vice president)到底算个啥?
quant interview questions(statistics)[合集] A statistics question
问一个matlab画图的问题 (转载)[合集] MFE有多难?
interview questions from GS[合集] What's the meaning of ATM?
相关话题的讨论汇总
话题: bins话题: shape话题: scale话题: gamma话题: truncated