PineScript Question - How to test calculations/debugging - debugging

I'm currently converting an old Basic program into Pine Script. In Basic it is easy to output variable's values to test they are correct, using the PRINT statement. Would you be able to advise how I can see the values of calculations in Pine Script, please? Here is an example:
fn_z_to_v(c,u) =>
u = abs(c)
z3 = int(u)
q = int(z3 / 30) + 1
z7 = int(fnw((z3 / 30 - int(z3 / 30)) * 30))
It should be noted that these are intermediate calculations and are not intended for display in a chart. Their values would make no sense in the context of time or price.
How might I see the values of u, z3, q and z7 (assuming I correctly pass the values c and u to the function and that the function fnw is correctly formed)?
Thanks,

Related

How to make a graph of a three-branches function in matlab

How can I make this function's graph in Matlab, so that its body is depicted in the same graph (plot or subplot)?
t0=0.15
x(t)= 1, if 0<=t<(t0/2)
-2, if (t0/2)<=t<=(3/2)*t0
0, else
The real question you should be asking is "How to define a function that has branches?", since plotting is easy once the function is defined.
Here's a way using anonymous functions:
x_t = #(t,t0)1*(0<=t & t<t0/2)-2*(t0/2<=t & t<=(3/2)*t0); %// the 1* is redundant, I only
%// left it there for clarity
Note that the & operator expects arrays and not scalars.
Here's a way using heaviside (aka step) functions (not exactly what you wanted, due to its behavior on the transition point, but worth mentioning):
x_t = #(t,t0)1*heaviside(t)+(-1-2)*heaviside(t-t0/2)+2*heaviside(t-t0*3/2);
Note that in this case, you need to "negate" the previous heaviside once you leave its area of validity.
After defining this function, simply evaluate and plot.
t0 = 0.15;
tt = -0.1:0.01:0.5;
xx = x_t(tt,t0);
plot(tt,xx); %// Or scatter(), or any other plotting function
BTW, t0 does not have to be an input to x_t - if it is defined before x_t, the value of t0 that exists in the workspace at that time will be captured and used, but this also means that if t0 changes later, this will not affect x_t.
I'm not sure of what you want, but would it be it?
clc
close all
clear
t0 = 0.15;
t = 0:0.01:0.15;
x = zeros(size(t));
x(0 <= t & t < (t0/2)) = 1;
x((t0/2) <= t & t <= (3/2)*t0) = -2;
figure, plot(t, x, 'rd')
which gives,
Everything depends on the final t, for example if the end t is 0.3, then you'll get,

Smooth damp or tween algorithm

I would like to know an algorithm for smooth damp or as some people call it, tween. I would like it in Lua preferably but anything will help.
I have tried watching unity tutorials but can't transfer the code without a algorithm to substitute for the smooth damp function.
If I understand the question correctly, you're looking for an easing function. There is a Lua library that provides a set of easing functions on GitHub: https://github.com/EmmanuelOga/easing
An example would be:
local function inOutQuad(t, b, c, d)
t = t / d * 2
if t < 1 then
return c / 2 * pow(t, 2) + b
else
return -c / 2 * ((t - 1) * (t - 3) - 1) + b
end
end
Where t = time, b = begin value, c = change in value, and d = duration.
More information on these easing functions is available directly from Robert Penner here (this is where the function above is derived from): http://www.robertpenner.com/easing/

envelope function (spatstat) - error "unused arguments"

I would like to ask your help for finding the reason why when I use the function envelope, my arguments are not accepted, but defined "unused arguments".
The data I'm using are ppp without marks and I would like to create a L function graph with simulated data and my data.
Here the code for my ppp data:
map2008MLW = ppp(xy2008_BNGppp$x, xy2008_BNGppp$y, window = IoM_polygon_MLWowin)
And then:
L2008 = Lest(map2008MLW,correction="Ripley")
OP = par(mar=c(5,5,4,4))
plot(L2008, . -r ~ r, ylab=expression(hat("L")), xlab = "d (m)"); par(OP)
L2008$iso = L$iso - L$r
L2008$theo = L$theo - L$r
Desired number of simulations
n = 9999
Desired p significance level to display
p = 0.05
And at this point the envelope function doesnt seem very happy:
EL2008 = envelope(map2008MLW[W], Lest, nsim=n, rank=(p * (n + 1)))
Error in envelope(map2008MLW[W], Lest, nsim = n, rank = (p * (n + 1))) :
unused arguments (nsim = n, rank = (p * (n + 1)))
It seems a generic error and I am not sure it is caused by the package spatstat. Please, help me in finding a solution to this, as I can't proceed with my analyses.
Thank you very much,
Martina
The argument rank should be nrank.
Also the relationship between the significance level and the argument nrank is not correct in the example. For a two-sided test, the significance level is alpha = 2 * nrank/(nsim+1), so nrank= alpha * (nsim+1)/2.
You have chosen a significance level of 0.95 but I assume you mean 0.05. So with nsim=9999 you want nrank=0.05 * 10000/2 = 250 to get a test with significance level 0.05.
Such a large number of simulations (9999) is unnecessary in this kind of application. Monte Carlo tests are valid with small values of nsim. In your example I would normally use nsim=39 and nrank=1.
See Chapter 10 of the spatstat book.

Optimizing a program by vectorized notation

Hi all I am working on Image processing and have written a short piece of code in MATLAB. The code is quite slow.
I am giving my code snippet here
for i=1:10
//find c1,c2,c3
//c1 c2 and c3 change at each iteration
u = (1./((abs(P-c1))^m) + 1./((abs(P-c2))^m) + 1./((abs(P-c3))^m));
u1 = 1./((abs(P-c1))^m)./u;
u2 = 1./((abs(P-c2))^m)./u;
u3 = 1./((abs(P-c3))^m)./u;
end
Let me explain the variables here:
P,u,u1,u2 and u3 are all matrices of size 512x512
c1,c2 and c3 are constants of dimension 1x1
m is a constant with value = 2
I want to repeat this operations in a loop (say 10 times). However my code is quite slow.
The results of the profiler are given below :
The total running time of the program was 4.6 secs. However the four steps listed above itself takes abour 80% of the time.
So I wanted to make my code run faster.
MY FIRST EDIT
My changed code snippet
for i=1:10
//find c1 and c2
//c1 and c2 changes at each iteration
a=((abs(P-c1))^m);
b=((abs(P-c2))^m);
c=((abs(P-c3))^m);
x=1./a; y=1./b; z=1./c;
u = (x + y + z);
u1 = x./u;
u2 = y./u;
u3 = z./u;
end
Now the program computes in 2.47 seconds computation time for the above steps are given below:
So this is way much more faster than my first method.
2nd edit
for i=1:10
//find c1,c2,c3
//c1 c2 and c3 change at each iteration
a=(P-c1).*(P-c1);
b=(P-c2).*(P-c2);
c=(P-c3).*(P-c3);
x=1./a; y=1./b; z=1./c;
u = (x + y + z);
u1 = x./u;
u2 = y./u;
u3 = z./u;
end
Now the program computes in 0.808 seconds.
The four steps described above computes above very quickly.
I am sure it can be made even faster. Can you guys please help me to further optimize my code.
It would be extremely helpful for matrices larger size than 512 such as 1024 , 2048 or likewise.
Thanks in advance.
Your current code is:
a=((abs(P-c1))^m);
b=((abs(P-c2))^m);
c=((abs(P-c3))^m);
x=1./a; y=1./b; z=1./c;
u = (x + y + z);
u1 = x./u;
u2 = y./u;
u3 = z./u;
Firstly, realize that the absolute value function is multiplicative. So |AB| = |A|x|B|. Now, abs(P-C1)^m is equivalent to abs( (P-C1)^m ).
Just a preliminary glance at it suggests that some of the computation in the bottleneck can be reused. Specifically, since c1,c2 and c3 are constants, the computation can be sped up a little bit if you try to reuse them (at the expense of additional memory).
temp_P2 = P*P;
temp_PCA = P*ones(size(P));
temp_PCB = ones(size(P))*P;
a = abs(temp_P2 - c1*temp_PCA - c1*temp_PCB + c1^2 * length(P))
The computation of temp_PCA and temp_PCB can also be avoided since multiplication by a constant matrix always amounts to the construction of a rank 1 matrix with either constant rows or columns.
I don't claim that any of these modifications will speed up your code but they are definitely worth trying.
The first suggestion is:
if m = 2 and it is not changing, why you don't try these alternatives:
A*A
and if m = 2 then do you really need abs ?
this part that you are doing
1./a
is faster than
a.^(-1)
so I don't see any better option in this part.
Another thing you can try is this. instead of:
x=1./a; y=1./b; z=1./c;
u = (x + y + z);
u1 = x./u;
u2 = y./u;
u3 = z./u;
You can have this:
u = (x + y + z);
u1 = 1./(a.*u);
u2 = 1./(b.*u);
u3 = 1./(c.*u);
this way I guess it is a little bit faster by removing 3 variables. but the code becomes less readable.

Converting an algerberic expression into a function?

I have been trying to create an expression to calculate brick and glass prices.
I'm working with a, b and c
so the price is $30m^2 for bricks and $20m^2 for glass
A and B are walls C is a round window radius
A = 3m (not m^2)
B = 2m (not m^2)
C = 1m (not m^2)
I believe that my expression (a*b)*30 – (c^2*20) works but how can I turn this into a java script function that in future I could use in a calculator to calculate prices etc...
Bit of a newbie with java script.
thanks all!
I believe the following function cost(a,b,c) should do what you want, assuming I've understood the question:
function cost(a,b,c) {
return (a * b * 30) - (Math.PI * Math.pow(c,2) * 20)
}
This returns 117.16814692820414 given the parameters a = 3, b = 2 and a = 1.
An example of the cost function in use would be:
var price = cost(3,2,1);

Resources