MrMarc

3163 Reputation

18 Badges

17 years, 132 days

MaplePrimes Activity


These are answers submitted by MrMarc

ok, thanx for your patience with me.

I can tell you one thing and that is that I would not have been able to set it up without your help.

I am not really at your level of programing skills yet .   but who knows maybe one day, ha ha....  thanx   :-)

ok, a couple of questions
 
seq( fff( X, 100, 4, 100 ), i=1..10 );
 
is that the prof_  (profit without stop) or prof_s (profit with stop)  ? and where is the other one?
 
and
 
time( seq( fff( X, 100, 4, 100 ), i=1..1000 ) );

what is the purpose of that?

the notion :- means what??

also why do you assigne the dist parameter to rand_ ?? random variable??  . It is not meant to be a random varable

thanx acer !  I am very grateful! 

I now just have to spend some time to try to understand what you have done.  It looks great but I have to understand it.

thanx again

 

ok I give you guys the code so you can see for yourself

 

##########################################

restart;

with(Statistics):

 

# parameters

s[1]:=100: # Starting price stock

d:=4: # Trailing distance

n:=100: # Number of periods

 

# Stock price

randomize():

rand_:=Sample(RandomVariable(Normal(0,1)),n):

for i from 2 to n do s[i]:=s[i-1]+rand_[i] end do:

A:=[seq(s[i],i=1..n)]:

 

# Trailing Stop-Loss

B[1]:=(A[1]-d): for i from 2 to n do B[i]:=`if`(A[i]>A[i-1] and A[i]>(A[i]-d) and (A[i]-d)>B[i-1],(A[i]-d),B[i-1]) od:

C:=[seq(B[i],i=1..n)]:

r[1]:=0: for i from 2 to n do r[i]:=`if`(A[i]>C[i],0,1) od:

w:=[seq(r[i],i=1..n)]:

wp[1]:=0: for i from 2 to n do wp[i]:=(w[i]+wp[i-1]) od:

w_w_:=[seq(wp[i],i=1..n)]:

ww[1]:=0: for i from 2 to n do ww[i]:=(w_w_[i]+w_w_[i-1]) od:

w_w:=[seq(ww[i],i=1..n)]:

 

# Final Corrections

C_[1]:=(A[1]-d): for i from 2 to n do C_[i]:=`if`(w_w[i]=0,C[i],C_[i-1]) od:

C_C:=[seq(C_[i],i=1..n)]:

 

############## Results #########

 

# Exit period due to stop loss

u[1]:=0: for i from 2 to n do u[i]:=`if`(w_w[i]=1,i,0) od:

Exit_period:=convert([seq(u[i],i=1..n)],'`+`' );

 

# Exit price stop-loss

Exit_price:=`if`(Exit_period=0,0,C_C[n]);

 

# Last close stock price

Last_close:=`if`(A[n]<0,0,A[n]);

 

# Profit/loss with stop loss

prof_s:=`if`(Exit_price=0,Last_close-A[1],Exit_price-A[1]);

 

# Profit/loss without stop loss

prof_:=Last_close-A[1];

 

##############################

 

#########################################################

 

on each execut loop I want to have the value of prof_s and prof_

any ideas??

 

I was trying to give you guys the code so you can see but I have a hard time pasting it only get html notation hummm

hummm,

so I guess it does not matter if we have two equations

w_1:=0.4573:  w_2:=0.5427     # w_1+ w_2= 1

w_1 = (15.3440*r_1 - 0.6329+5.7540*r_2 ) / (21.0981*r_1 -3.1780 + 84.8348*r_2 );

w_2= (5.7540*r_1 - 2.5450 + 79.0808*r_2 ) / (21.0981*r_1 - 3.1780 + 84.8348*r_2):

We still cannot simultaniously solve for

r_1=0.175  and  r_2=0.055  ??

sorry it should be

( 15,3440 * r_1 - 0.6329 + 5.7540 * r_2  )   / 

( 21.0981 * r_1 - 3.1780 + 84.8348 *r_2 )    =    0.4573

 

The maple code for the equation is

0.4573 = (15.3440*r_1 - 0.6329+5.7540*r_2 ) / (21.0981*r_1 -3.1780 + 84.8348*r_2 );

 

The solution should be

r_1=0.175  and  r_2=0.055

 

 

I can solve it the other way around

w_1:= (15.3440*r_1-.6329+5.7540*r_2 ) / ( 21.0981*r_1-3.1780+84.8348*r_2 );

r_1:=0.175:   r_2:=0.055:

# which gives the output

w_1=0.4573

 

but not the other way..humm

I have done some work that I would like to share! 

If you have any ideas for improvements or comments feal free to post

 

########################  Random coin and the number of heads  ################################

# Note that n is the number of coin tosses
# Note that nseq is the number of sequences
# Note that 1 is heads and 0 is tails
# Note that since #heads= 1- #tails the distribution will look the same for tails

restart:
n:=20:                   # parameter 1 
nseq:=1000:       # parameter 2   (for a normal distribution n=1000)

randomize():
coin:=rand(0..1):
coin_1:=proc(n) seq(coin(),i=1..n) end:
x_1:=seq([coin_1(n)],i=1..nseq):
x_2:=[seq(numboccur(1,x_1[i]),i=1..nseq)]:
with(Statistics):
Histogram(x_2,frequencyscale=relative,discrete = true,color=red );

 

#######################  Random coin and sequence lenght  ###############################

# Note that n is the number of coin tosses
# Note that 1 is heads and -1 is tails

restart:
n:=10000:            # parameter 1 

randomize():
coin:=rand(0..1):
coin_1:=proc(n) seq(coin(),i=1..n) end:
L:=[coin_1(n)]:
LL:=[seq(subs(0=-1,L[i]),i=1..n)]: # Transform to a -1 and 1 coin toss
x_1:=remove(Testzero, map(-nops, [ListTools:-Split(`=`,LL, 1)])): # Sequence -1
x_2:=remove(Testzero, map(nops, [ListTools:-Split(`=`,LL, -1)])): # sequence 1

y:=[op(x_1),op(x_2)]:
with(Statistics):
Histogram(y,frequencyscale=relative,discrete = true,color=red );

LLL:=Vector[row]([LL]):
Statistics:-Correlation(LLL[1..(n-1)],LLL[2..n]);

                               -0.003387130345

 

#######################  Serial correlated coin and the number of heads ############################

# A threshold value of 0 is a random coin (no positive serial correlation)
# A threshold value of between 0 to 10 gives a coin with positive serial correlation
# The higher the threshold the more positive serial correlation the coin will have
# Note that n is the number of coin tosses
# Note that nseq is the number of sequences

restart;
n:=20:                    # parameter 1
threshold:=7:       # parameter 2
nseq:=1000:        # parameter 3

randomize():
coin:=rand(0..1):
coin_1:=proc(n) seq(coin(),i=1..n) end:
x1:=seq([coin_1(n)],i=1..nseq):
c10:=rand(1..10):
x2:=seq(['c10()'$n],i=1..nseq):

F := proc(n_1,n_2)
if n_2 = 1 then return x1[n_1,n_2] end if:
if n_2 <> 1 and x2[n_1,n_2]<=(10-threshold) then return x1[n_1,n_2] end if:
if n_2 <> 1 and x2[n_1,n_2]>(10-threshold) then return F(n_1,(n_2 - 1)) end if:
end:

x_1:=seq( [seq( F(n_1,n_2), n_2=1..n)],n_1=1..nseq):

x_2:=[seq(numboccur(1,x_1[i]),i=1..nseq)]:
with(Statistics):
Histogram(x_2,frequencyscale=relative,discrete = true );

 

##########################   Serial correlated coin and sequence lenght  ################################

# A threshold value of 0 is a random coin (no positive serial correlation)
# A threshold value of between 0 to 10 gives a coin with positive serial correlation
# The higher the threshold the more possitive serial correlation the coin will have
# Note that n is the number of coin tosses

restart;
n:=50000:       # parameter 1
threshold:=4:   # parameter 2

randomize():
coin:=rand(0..1):
coin_1:=proc(n) seq(coin(),i=1..n) end:
x1:=[coin_1(n)]:
c10:=rand(1..10):
x2:=['c10()'$n]:
 
F := proc(n)
if n = 1 then return x1[n] end if:
if n <> 1 and x2[n]<=(10-threshold) then return x1[n] end if:
if n <> 1 and x2[n]>(10-threshold) then return F(n - 1) end if:
end:

L:=[seq( F(i), i=1..n)]:
LL:=[seq(subs(0=-1,L[i]),i=1..n)]: # Transform to a -1 and 1 coin toss
x_1:=remove(Testzero, map(-nops, [ListTools:-Split(`=`,LL, 1)])): # Sequence -1
x_2:=remove(Testzero, map(nops, [ListTools:-Split(`=`,LL, -1)])): # sequence 1
y:=[op(x_1),op(x_2)]:

with(Statistics):
Histogram(y,frequencyscale=relative,discrete = true );
 

 LLL:=Vector[row]([LL]):
Statistics:-Correlation(LLL[1..(n-1)],LLL[2..n]);

                                0.3956779623

 

I managed to achive that by simply using the code

with(plots):
A:=(y-2*x)^3*(y+2*x):
a:=plot3d(A,x=-2..2,y=-2..2,axes=boxed):
b:=contourplot3d(A,x=-2..2,y=-2..2,color=black,thickness=2, contours=[-2,2,-30,-80,-140,-200,-280,-340]):
display(a,b);

and then I stretched it by pulling on the picture until I got blue in my face.I saved it as jpeg by left clicking and croped the top part by using picasa2.... sweet 

I think this is as good as it gets !

actually I think I was a little to hasty.

The point was that  _F1 or should become  (y - 2 x) ^3  (y + 2 x) and not

1 / ( (y - 2 x) ^3  (y + 2 x) )   ......  humm 

thanx man !  Yes it finaly worked.

Just to even the score with the solve function I have renamed expr _F1. The irony is complete, he he

restart;
diff(F(x,y),x)*(x+ y)+diff(F(x,y),y)*(4*x + y);
sol:=pdsolve(%); 
id:=x->x:
sol1 := eval(sol, _F1=id):
_F1 := rhs(sol1);

 

ok, point taken _F1 is a function ! but still !

I want to go from A-B-C-D in a linear fashon and this _F1 just mess everything up for me.

From my perspective _F1 is just some aribrary name which I should be able to solve for..humm

"can be replaced with adding orientation = [-90,0] in the plot3d command"

That is actually quite useful because I have manually rotated the plots up until now

But I think I will use that "orientation = [-90,0]" command from now on...thanx

I must say that I find it quite hard to see the exact property (elevations etc) of the 3D graph other than looking directly at the contours.

If I stretch out the 3D graph really wide and tall in Maple and look really close I can see the two elevations (on opposite

side) that results in the small inverted U contours but it is not easy..

First 20 21 22 23 24 Page 22 of 24