|
改了一下,这次运算应该没啥问题了) W3 {: A7 q/ i1 M
6 o/ ?/ g1 {- a- i0 r* z7 _
5 x2 V1 t9 o# k% t
capture program drop hello
, i2 x2 b8 D1 [' w" l+ J8 C
% E6 ?$ L: q E g& I6 u* Cprogram hello* {* }$ X: G) X
) ~6 T: b, i; ?9 x3 Y quietly {2 F9 x M& g5 y4 f u1 x( D! z/ |; h
7 [( z9 ?9 ~9 i; sclear
3 w! G; a& ]/ i/ j! Q4 ~& |9 U
" ] G. s( S9 w( l8 |) L* qcd e:\finance\stock\20110101\source3 ?4 M7 O X) p6 r6 M
$ w4 m% q# Q8 }- k+ R
insheet using `1'.txt+ C4 W( f/ R* G* ?
" B9 F3 [* M/ Q& ` keep if v5>0; P; a/ ]1 E+ H3 J6 q
$ E2 T4 G n, U4 W. R. ]; N! |% \3 y
gen vt=_n
4 F3 j! h, Q6 E: B+ X$ {( \9 i ) S9 V; E" w. j, z/ f5 K8 G
tsset vt
$ p4 Y# V6 Q1 _" v6 c; B. o% q: `' k1 ], A5 D: R' v6 _. I* H
gen zf=D1.v5
) C8 d+ y! m/ `( x# A
+ |# ~5 U& q- S2 `8 z" S8 H gsort -v15 ?: \* Y0 Z5 e& h8 c
4 L6 ~/ _# t; U% }) n- m. ` gen avol=v7/v5; A7 n6 V- ^+ x* ]4 m% i$ M% i3 c8 ]
: s: v- P- F0 Z7 ^2 q% k) ]& [ keep if avol>0
7 ?+ s# W0 A) u, ^
! i2 w" p3 `% v o2 ?( n3 e drop vt
8 _. g' J1 O4 r& y9 N$ C/ [ ; X6 e5 X4 J9 }' X
gen vt=_n
) F3 t9 U( P* _$ N( y$ x, ^ 3 `: F* I4 f) A) |' S
tsset vt
6 y/ S w* ~8 f$ }; R / h; Y1 Y9 ~/ }0 b& J
sum v5 [aw =avol]3 E1 u# l# j2 Z: w
# i6 `0 w; m3 v5 R W
gen amean=r(mean) in 1
8 f* g4 e( e( A t 1 h: ^% K+ q0 J n. d+ h
* 总加权平均值 amean
) b' x2 c6 f: k. p / M; S. _# e9 X$ S: s
8 y- K' ^# |& n8 `, O; x
keep in 1/240
+ b& @% f7 j5 T0 P" R, F6 u; V2 j
sum v5 [aw =avol]
! t. C/ w" L8 g( m7 {' v$ s. X0 v
gen mean240=r(mean) in 1- |! w+ _" y3 r' C
2 S( c. [5 {& ~5 p3 v *240加权平均值 mean240
& K( V6 G) n- i9 X7 j' x/ v8 [ 8 Z& a }1 Q& L* B M* l; j8 n5 E5 F0 I) k
set obs 500
9 f: ~% \' k9 L: m" K( P
' b) v: u8 L- d9 R kdensity v5 [aw =avol],gaussian n(500) generate(dx240 dy240) nograph) t: v+ c5 j7 U/ y
i8 Q* r1 i9 o7 V( S6 I *240 kdensity(dx240 dy240)+ x( g m* O8 o1 Z
6 c! D9 w2 S" ^" P6 \ sum dy240
8 T9 y5 B0 G2 j
9 B5 O3 D, D6 S# j8 V2 C$ @ gen yxis=r(max) in 1
. g) ]1 F8 j K2 S# Z) c& w
5 u$ m, {; c& {" o- G gen yxis2=yxis/2
$ j1 `# E- W5 v; ]6 M- Z3 W6 @$ e
) N" \! s# K: D3 D/ A" A, N *yxis
0 n5 U2 z# w+ `
2 V8 r5 W$ ]6 H) D& U gen dy240max=r(max)- `6 `( L2 R4 p) C# X3 E0 y! F. N; T
6 b, B. H8 ?% K- _$ Z/ l& L gen dy240minp=r(min)
, @( x) D: H( D- I' G
8 a3 m( [& k& h3 z0 L6 k gen vtt=_n. _3 c4 |, S! P A* |
. p8 `' A/ L5 m tsset vtt+ j- |. s N: T* C
" E- \: w9 w8 E gen ddy240=D1.dy240/D1.dx240! p5 A' F$ O: N% u) C4 b( n p
+ I' r$ D" B3 B$ W2 h3 A3 [( c) ^ sum ddy240
) j) P5 R8 R5 e ]8 q1 @
$ z2 ?, {; A: o* Z1 F) c gen ddy240max=r(max)
6 L! U6 T$ `) w' p
: x& U8 h! ]9 x; s+ G7 ? gen ddy240min=r(min)9 i- t% U% a H
, L7 Y0 R% x: e" C2 U. Y1 O gen ddy240g=ddy240*(dy240max-dy240min)/(ddy240max-ddy240min)' ~* z, V1 U6 Q" q2 r# P9 S8 g' k
0 I' N) B- |9 |8 g3 f1 p sum ddy240g
& L$ ?& o* y8 Q3 k$ |8 ~
1 Y) b ?: u4 V+ {! l T) N gen yxis3=r(min) in 13 a W$ r8 a0 l- h }, T8 e$ ^
8 \3 N* Q/ a1 e* T Y: L gen yxis4=0 in 10 k/ S/ K4 S' x4 ^" k E/ K1 _& Q
+ `; [3 y2 L. x0 ^; Y *ddy240g
( n! x/ M# s* I) `- i0 G+ v& E5 e! ~% f " F! K- ], r. M* e$ t# T2 V q
4 ^$ P0 ^3 R) U2 K( m; V" T preserve
( Y; p- M8 ^. Q- A$ \( `
* o& ~: h; q# X, k* o! ^) S sum v5 [aw =avol]1 J! F0 ?4 E; }* ~8 z
! |9 f9 A9 k9 ]& ]8 D3 Y keep if dx240<r(mean)! {( {4 w3 K8 ~! s# f
2 i* h* O5 l7 F2 T% B
sum ddy240. B% a" X$ e9 c
* D' g) V8 x. ~8 V
if r(min)<0 {0 M: a% Z/ Z9 E
# B- [ u/ n L4 i9 W keep if ddy240<=0: O0 y7 v g \' ~! H8 H8 V z
& L/ M3 ]: Y9 v) d6 \: L
count
0 Z; i, j( N* K+ E0 G: n& E5 O- O 4 z0 R) n) V) I. E* P4 h
if r(N)>2 {
. b- Z; @8 a' ^! n8 D2 q
+ S0 [4 V. X6 u. j) a# s- K0 G pkexamine dx240 dy2403 C7 u- i$ G! R
. w2 f0 [- M* ]2 j1 I
local tempminp=r(tomc)
+ o/ s: R% O7 A- A, l" H% z restore / _; r3 N2 Y3 A4 K' _
}$ c/ W! b# h* ~' P
8 @1 u6 Y. x' I6 r3 A3 M9 s/ P else {. w4 k: T9 S) I4 N+ r
* V/ I& v7 n# w; f4 A( w) J di "cannot find minp"
3 {. m! }' m3 @; y
; F* `' ~$ C7 p5 X+ e restore 8 U2 m8 [& e) ^; A D0 P
sum dx240
$ D" s1 N6 Y2 g, l local tempminp=r(mean)
7 ~' V# C* A" k- X8 R B 8 [/ z. G5 B b
}+ H# i7 i- ^* Z# p- I0 ?/ ^
}
+ N$ }) _+ ]: W, ?8 ~8 B else {
4 l' k" t* i# K( q! M* N 9 h: D D7 t5 D$ T" N
keep if ddy240<=r(min)
# |* q3 N( ], y' r w# b
* k, r% D* S5 J# U sum dx240
/ [' `, Q* J' J% {9 u% u. T- i: X ' i! |: u9 d1 L" f$ O. X2 q1 v
local tempminp=r(min)7 k3 x0 s% R4 d
restore
2 V* V2 l6 T$ q* E* n4 m }5 k5 g* ~0 o5 u
" H+ m$ w$ S$ k/ {9 F( l
! G9 ^& H4 `( J
2 t) e9 {. L" t) x gen minp=`tempminp' in 1
* j) x2 J2 o6 x7 @: a% w *240 minp) N6 r7 A; }4 n& h6 c- r$ c
% q: Q5 N) ^, K5 G! g9 _( d6 S
+ n7 r% y/ R# [- j9 d( c- v& R1 |) ]
7 _9 {( a( W' `3 Z preserve
3 {& |3 ]- F% Y+ w, J! N3 K7 I- h) E% P
% p: B# Q3 B6 J( i5 _ sum v5 [aw =avol]
" Q) _: {5 A3 E x" [0 R/ P. s
keep if dx240>r(mean)" t& R+ ^5 O$ k
L% s# ? l7 i% z* H( e9 j7 V. n
sum ddy240
4 ?4 ] m2 R O2 U# Y6 d/ b 6 j' Z# `9 c9 H9 Z8 p
if r(max)>0 {7 W4 K1 v, H; Q2 `
; H( v* C7 }% `0 r# F% ] keep if ddy240>=0# F# o6 `# t' [! m" M+ W+ J& M
* J, Q3 K0 c2 J7 t count8 e. n. a( m8 G' _
s; V/ q% o+ G, T. J4 ^1 o' e8 j if r(N)>2 {" ]" E3 G+ E5 Z7 L5 \' w
1 ^9 H5 [0 a1 T- p+ C; L
pkexamine dx240 dy240
' w. R* `; n# l+ i Q ) F( ]3 Q+ ^) W; }9 x
local tempmaxp=r(tomc) 4 ]4 `' f: l' p! _1 Q
restore
& R R' N) d3 Q" z }
/ l+ k, g. E) l6 P6 R# I 8 e" p2 i/ [3 R1 `' R( \
/ D' E1 J( E# [9 L9 p else {2 d2 e4 z. Q& R' [
restore
* J. B$ S7 m) u: M) ], e0 C a di "cannot find maxp"% u2 M L- } s4 |7 S- d7 T
( b3 h( }. p+ b$ g" T c8 W
V& p# n0 g+ M& u) f sum dx240+ L# b3 }* @4 E m
local tempmaxp=r(mean)
" Y+ a5 P, {. C
: V+ W5 x/ L! a6 w/ p }
$ `. V- v z3 ~ ( T3 k: t5 a) j3 f
}
+ o; @" v! [) k1 s 3 G/ _" r: c& T& {/ ^ A5 v
else {
: Z( n! r* v" c+ a4 C
: d& {7 m# x0 h4 S$ Q1 P: u keep if ddy240>=r(max)
5 H# r; I1 X9 I( ?" t8 |. f
; ?1 o" E! u0 J6 P6 T* r sum dx2401 `. n" A s9 V; M: j4 U$ A! D
' V% ]. ?- f3 | local tempmaxp=r(max) ) J' D* I, f; w5 u
restore 2 T- M* a5 P2 W* o$ q$ s" {
}
1 U/ X K8 r- h+ N' T p$ Z, V( L, [4 h3 P! W
3 J2 k0 K/ X' p# j5 f! `- ~
( w% H* C; L: Y$ }. n. u2 m j, D) b! O5 N1 [8 K
gen maxp=`tempmaxp' in 1
7 V5 U7 Q! V: }3 ?) q - i" _8 Y7 d4 D# _1 T. \
*240maxp' z7 ?8 ^9 r9 G4 o) K2 D
V& V, V" f0 S' Z0 q& |; }
" t9 [( L. ~: V! W *-------------ddy240pminp左侧高峰值-----
) R# @2 }2 g! [) R
* ]& }% W* {1 ^- u) `9 V preserve
9 N/ t2 C3 b5 s5 h8 L 0 C" L2 O) G& w* Q
keep if dx240<`tempminp'0 G0 ]) ` @' u% V
) n5 d% S( ^6 M/ ] keep if ddy240>0 n, T* j+ o- y0 z+ B
0 r& z. o! x* E
count
; m5 M, I8 b2 p+ S6 E * B' j9 Q! F9 i+ ]/ D1 n0 J0 o
if r(N)>2 {2 b4 R" ~# Z% j+ w
( {3 `8 `" e. H
pkexamine dx240 ddy240
8 j3 ?9 ]/ H! D/ V% L 1 d9 _4 Q$ R i, F+ R* i
local tempminphddy240=r(tomc) 2 n8 b- e2 d) l
restore4 g3 U/ I% }9 i- C
}
3 ?5 }6 [) p- m' F) S
$ v: t: T0 I& S# } [$ f8 w else {" T! z% g @9 B: v4 E
restore
; ~- w4 J' n% m8 ` di "cannot find pminphddy240". @. m. _2 e0 z- g: @
( n6 s+ u. k1 N* q" b* {
) q7 @! \( T( F
sum dx240
/ R. |7 d8 m0 W; z2 g0 z2 K local tempminphddy240=r(mean) * m$ A4 g; ?# {. q+ n6 r$ Y' F) A* y
8 i( P8 D6 ?3 n# u
& U& t9 @7 E5 }% ^& j. z }
$ X$ l% i/ I2 B# Q
$ Y, L8 j- F1 V, Y
- Q4 a, {; d/ g! g+ h9 f* L
$ ?# }3 v, j+ f' d( r 3 Q- T" N( |# w
5 N3 A3 d, r" Q5 p4 }2 Z4 C3 M3 ^
0 P8 }, i# x f6 z/ D8 v gen pminphddy240=`tempminphddy240' in 13 N' ^$ R2 E; b
- m7 B5 N6 l; A6 K) I4 S/ W" R
$ |9 b6 @6 k% D *-------------ddy240pminp右侧低峰值 -----------------
& @3 D% a" F; u preserve
! d& N: G1 E* J# }/ E / }+ L% Z. a) q5 G& h6 m6 [; |9 J# s
keep if dx240>`tempminp'9 x5 W4 ~$ V% z+ s
9 M C/ `" y, Q- }( S8 L% s. v
keep if dx240<`tempmaxp'4 G# R" e& D6 L- s( F( C
7 r3 k7 S3 ?, {2 k keep if ddy240<06 B1 b4 p2 c; ?: G6 i# D
" x! G( g! T: C( W' ^
gen temddy240=-ddy2406 g, j+ m F$ ]; y u- J
" `# W3 f) L0 f4 S
count% u$ T! \1 x( ~
. p7 c% x! @" o8 s( | if r(N)>2 {
! k+ w }. x# L $ N6 n, S& g$ Q, I7 l8 z/ X0 s
pkexamine dx240 temddy240
9 Y1 x! g) R6 g
3 Q8 ^, K$ Q, p# M% F local tempminplddy240=r(tomc)
+ U. J, N4 N T9 I3 m restore
, O' F& A& E( w6 W; P8 T- Q' z }
( h% C ]: D3 j9 c( T & ~( M. D# o( q
else {
% p" k# i8 Y- A' Q6 o0 o% c! k1 t 7 |) l& x! c1 T4 Q1 Z
di "cannot find pminplddy240"
3 X" R. m' v4 K: g; ^3 u+ u! t" \ restore
" S8 A- H( m9 j& z1 g ( t# u2 X! S @5 y2 \+ @, W
sum dx240& U: p) {. b. X- W( i# |' V
local tempminplddy240=r(mean)) N$ B# L2 ~0 d5 K6 B5 k
+ Q% J" k7 |4 Z k0 X8 n
4 w# o9 }# l0 b- q( b) x: Y: H
}
( I; c0 e8 y& O. B" R- e5 H) b" C1 ^
. Y0 I- L* } _* O ( B1 c3 ^6 ]" i9 B
1 H' a5 W: u4 _
- }& `$ F1 ]4 _3 n; s! G
) F) P# |: j* l$ r4 ^
gen pminplddy240=`tempminplddy240' in 1
5 i/ O6 Y1 _( W; H5 d 6 W! B% }: c7 b: z: B) F, g' J( f2 T
*-------------ddy240pmaxp左侧高峰值 -------------------
5 d$ t3 p' C2 H, m
/ N8 H' T, |0 t$ L. D# L+ ^* V8 n preserve $ n# z5 p. k9 ]
# N* C. l, N; u* F( T1 z keep if dx240>`tempminp'
/ Z$ Q( A$ K$ T) T3 t2 g # S- y; Z- @. D) Z6 B
keep if dx240<`tempmaxp'
, {( a' c0 D- P , K- K3 G0 r# x) X9 p7 t9 R
keep if ddy240>0
% m( T5 L' G# E( w, y5 ?$ I8 M count2 ~, r6 H! h' [4 O: j4 y. G* h
$ {, y: E% ^. |8 t" Y' I if r(N)>2 {% M, x" H. b$ m! ^$ J8 N; Y8 x+ x
0 B# |* \9 n; f! d
pkexamine dx240 ddy2402 k- ?4 i. o I- I: |/ H6 g
& y# l0 W' V0 |2 l* D h( m
local tempmaxphddy240=r(tomc)
4 c# p, s, K# N! G; l 7 z3 G, f5 ~" F# g- L8 G; _
restore. p# W/ q' K7 X0 b0 n' H
}
: o9 l+ K) E8 B `* n# }0 C4 w. S: f/ E- J
else {4 z* g! K) Z9 D+ d0 i
4 E7 T7 K' A1 H( E; s& l di "cannot find pmaxphddy240"2 \ ]0 c4 d8 j- u- Z" B' b
restore
% `! {0 O" w2 W7 Y- } N: F ) S: |/ T7 K& e$ R1 ]" B- }
sum dx240
, |% {. n$ I; a! A, {% s! K local tempmaxphddy240=r(mean)
5 q) H7 C% j; u N% @ , s- I7 w2 O* h8 y2 K" k
4 V7 v9 W l2 u# B }
0 s: r, ]1 B$ J. G+ |( k
* \7 v$ S" z( D: \6 K
: b2 @( p0 x7 N2 O& o+ N
) P4 P c! R7 Z. E' L( f- \/ u
8 n0 c0 R# p! Q
1 @4 l: w! Q' \( W6 y E X& g% h. Q5 n9 f3 g$ c8 M
gen pmaxphddy240=`tempmaxphddy240' in 16 d! ]: n: ~! G& F Y# K
2 A/ h+ G* u3 W1 r8 Y3 {
- |" x' ~8 }4 D' a *-------------ddy240pmaxp右侧低峰值 -------------------
/ Z) C9 D& b+ I! J: I$ `
( S7 _, H# j% H* m# R0 y. o preserve + m0 a; ~8 n3 a5 ?$ R- ]: ^
/ o+ F. ^7 ?2 a- Y* G) C
keep if dx240>`tempmaxp'
% w- Z0 I( l' S- d* g) p ( g7 m K0 n, S; ]
gen temddy240=-ddy240
! e) v) \8 S4 |0 V1 r h5 `, e
7 A) P7 R* q8 j keep if temddy240>0
6 u* n: z7 @9 T! l. I% m9 q" J6 J
+ |8 ]! Z4 E9 |: N e count
3 X' } P, R, i1 q+ l0 R" r3 O
1 k! }$ y F+ o& P9 ] if r(N)>2 {: K' Q9 e# T7 M) y; V( ~/ ]- J/ l# `
/ `+ x% q4 F2 x0 \: K( W/ D pkexamine dx240 temddy240/ z4 }1 P! ^" A3 L4 O' `) X) p
# \7 L0 D$ ?) t L$ t2 A8 S local tempmaxplddy240=r(tomc) , k5 U. Y! |5 G, O
restore
5 a# T# V9 k( j) N9 x4 Y1 u }$ {0 t K# l, a
, o2 G3 O: Q* p
else {( i# ?; ?5 ?2 w. a) V; j$ H* H0 M2 `
restore- O: Q7 d3 z$ ]" p5 g
di "cannot find pmaxlhddy240"
' G6 ~. x$ x" ~8 F7 P
+ ]7 t2 F+ G0 }$ n
2 y- v) I1 b; j7 f sum dx2407 d; |6 B3 _# i4 }' u3 T
local tempmaxplddy240=r(mean) in 1
1 {/ z$ A# `- d1 O$ i7 D& ?
6 F' p) D! U- Q
( q, Y E$ L' S6 ]1 p& l }8 i7 d! Y- [4 J
" r/ W/ J: }" P( ~
( x g9 V7 G: x. G- A , x) R7 t+ o- s& U% E6 C
( l4 q. D3 O% S \2 [
' v o* U- J4 h1 G 5 _! o3 U7 c( ?: \, f
gen pmaxplddy240=`tempmaxphddy240' in 1. F1 h) y: H0 H. u! @& L* v! r
; X1 C6 r! Z: Z6 e
*-------------------------------------8 H, q7 q, t# ]3 G! L% o9 C
7 C1 M9 ~8 y( l; z7 t
gen price5d=v5 in 1/20
4 g7 w; r% t+ R' o 2 S8 P/ q# r7 L1 O2 ]) P6 x6 m: z
gen price5yy=_n in 1/207 n/ n5 v; S1 t x: s* f5 g
3 W1 S; ~/ E8 I- N$ ^! k( s( V
gen price5y=price5yy*(dy240max-dy240min)/20, @6 D+ z; V; n1 i: K' A
1 ^6 M2 I) x" G! O( \0 W3 m
*price5y &price5d
" p. b) X; O) ~9 ^! B+ T4 g0 \ / v0 @' N; c" Q, \0 |, P8 [( h+ z
gen priceny=price5y in 1
5 P: H- c& ]$ ]$ o; k , ^- ^% J; q8 H, `- N# q$ R
gen pricen=v5 in 1
. q3 _( G) b* n
0 {, w: k. m- \" n9 q, } + A+ v' Y) w8 `8 l- B }
*priceny &pricen
$ i! B1 }) y; L) M) c& K7 s+ F$ K
/ E9 j* `* z" J' X* O! W! ]; A & _ M, [1 ? l# o/ }4 }% N
*-------------------------graph------------------
/ K5 }" e, G v" M P
, b0 j- z) c# t$ b# M0 ]" J1 r( L tw(line dy240 ddy240g dx240)(spike yxis amean,lc(blue))(spike yxis mean240, lc(red))(spike yxis maxp, lp(-) lc(black))(spike yxis minp, lc(black))(scatter yxis2 amean, lc(blue) msymbol(+) mlabel(amean)) (scatter yxis2 mean240,lc(red) msymbol(+) mlabel(mean240) lc(red))(scatter yxis maxp,msymbol(+) mlabel(maxp) lc(black))(scatter yxis minp, msymbol(+) mlabel(minp) lc(black))(scatter yxis4 pminphddy240, msymbol(O) c(l[-]) mlabel(pminphddy240))(scatter yxis3 pminplddy240, msymbol(O) c(l[-]) mlabel(pminplddy240))(scatter yxis4 pmaxphddy240, msymbol(O) c(l[-]) mlabel(pmaxphddy240))(scatter yxis3 pmaxplddy240, msymbol(O) c(l[-]) mlabel(pmaxplddy240))(scatter price5y price5d, msymbol(O) mfc(black) mlc(black) c(l[-])) (scatter priceny pricen, msymbol(O) mfc(red) mlc(red)), title("`f'") xtitle("`1'") ytitle("density") legend(off)
# O+ L+ B2 b- B" i+ a0 r
; r0 l8 F, F8 y6 w* ---------可选项目----------------------
9 i3 v( W5 \1 E% F6 f+ \
1 z, y, e1 A- }1 k* K0 y}* W2 g6 S0 W! R5 k/ P+ O
end+ B" P% {. e. l5 T" c. Y
|
|