我的数据目前采用长格式。下面是一个示例:
Stock Date Time Price Year
AAA 2001-01-05 15:20:09 2.380 2001
AAA 2002-02-23 10:13:24 2.440 2002
AAA 2002-02-27 17:17:55 2.460 2002
BBB 2006-05-13 16:03:49 2.780 2006
BBB 2006-10-04 10:33:10 2.800 2006
Run Code Online (Sandbox Code Playgroud)
我想通过“股票”和“年份”将其重塑为宽格式,如下所示:
Stock Year Date1 Time1 Price1 Date2 Time2 Price2
AAA 2001 2001-01-05 15:20:09 2.380
AAA 2002 2002-02-23 10:13:24 2.440 2002-02-27 17:17:55 2.460
BBB 2006 2006-05-13 16:03:49 2.780 2006-10-04 10:33:10 2.800
Run Code Online (Sandbox Code Playgroud)
我尝试了此处发布的解决方案Pandas long to Wide reshape并具有以下内容:
df['idx'] = df.groupby(['Stock', 'Year']).cumcount()
df['date_idx'] = 'date_' + df.idx.astype(str)
df['time_idx'] = 'time_' …Run Code Online (Sandbox Code Playgroud) 我目前有代码模拟几何布朗运动,由http://www-math.bgsu.edu/~zirbel/sde/matlab/index.html提供.
但是,我想生成1,000个模拟,并将其显示在图表中.
我目前生成单个模拟的代码如下:
% geometric_brownian(N,r,alpha,T) simulates a geometric Brownian motion
% on [0,T] using N normally distributed steps and parameters r and alpha
function [X] = geometric_brownian(N,r,alpha,T)
t = (0:1:N)'/N; % t is the column vector [0 1/N 2/N ... 1]
W = [0; cumsum(randn(N,1))]/sqrt(N); % S is running sum of N(0,1/N) variables
t = t*T;
W = W*sqrt(T);
Y = (r-(alpha^2)/2)*t + alpha * W;
X = exp(Y);
plot(t,X); % plot the path
hold on
plot(t,exp(r*t),':');
axis([0 …Run Code Online (Sandbox Code Playgroud)