# Laplace Matrix For ThinkOrSwim

#### MerryDay

Staff member
Staff
VIP
Found on JohnnyQ's OneNote per poster request @griftingthrulife
It should be noted that the author doubts its usefulness:
15:10 AlphaInvestor: I have the Laplace Distribtion coded ... no idea how to turn it into something useful

Ruby:
``````# Laplace Matrix
# AlphaInvestor
# 6.12.2019

# Laplace Probability Density Function
#
# Location is the mean
# Scale specifies the spread of the distribution  (for Laplace dist
# scale = standard deviation / square root(2)). The ideal scale factor
# for a Laplace distribution is the standard deviation of the population
# divided by the square root of two.
#
# The calibrated scale factor I [paper author] used to match the event
# frequency in the 5 sigma or larger tails was the ideal scale factor
# times 1.19 of the close price with respect to the low to high range
# for the same period.
#
# Source:
#
# Predicting Stock Market Returns—Lose the Normal and Switch to Laplace
# February 21, 2019 by Vance Harwood
# Six Figure Investing
# Everyone agrees the normal distribution isn't a great statistical model
# for stock market returns, but no generally accepted alternative has
# emerged. A bottom-up

declare lower;

input length = 60;
input avgType = averagetype.SIMPLE;
input price = close;
input scaleMult = 1.19; # 1.19 suggested by paper author for S&P 500

script LaplacePDF {
input x = 0   ; # the close
input u = 1   ; # the mean
input b = 2   ; # the scale parameter
def a1 = exp(-(absvalue(x - u)/b));
def a2 = 1 / (2 * b);
def a3 = a2 * a1;
plot Lpdf = a3;
}

def location = MovingAverage(avgType, price, length);
def scale = stdev(price,length) / sqrt(2);  # SPX 1.19, IWM 1.17, AAPL 1.12 (to fit 5 sigma move)
def myScale = scale * scalemult;

plot Laplace = LaplacePDF(price,location,myScale);
Laplace.setdefaultcolor(color.light_gray);

# End Study``````

Last edited:
Another version from the chat archives. Not sure of the original author. It was provided in the chat by Mobius but all his scripts have headers so not sure where this script came from originally.
Ruby:
``````# Laplace Transform

declare lower;

input n = 252;

script f
{
input n = 20;
input A = 1;
def c = close;
def acc = Cos(2 * Double.Pi / (n / A));
def dis = Cos(.1 * 2 * Double.Pi / (n / A));
def sino = 1 / dis - Sqrt(1 / Sqr(dis) - 1);
def osc = if IsNaN(c + c[2]) then 0 else (1 - sino) *
(c - c[2]) / 2 + acc * (1 + sino) *
osc[1] - sino * osc[2];
plot F = osc;
}
def p1 = f(n, 1);
def p2 = f(n, 2);
def p3 = f(n, 3);
def q1 = n / (2 * Double.Pi) * (p1 - p1[1]);
def q2 = n / (2 * Double.Pi) * (p2 - p2[1]);
def q3 = n / (2 * Double.Pi) * (p3 - p3[1]);
def t1 = Sum(Sqr(p1) + Sqr(q1), n);
def t2 = Sum(Sqr(p2) + Sqr(q2), n);
def t3 = Sum(Sqr(p3) + Sqr(q3), n);
plot lp = if isNaN(close)
then double.nan
else p1 + Sqrt(t2 / t1) *
p2 + Sqrt(t3 / t1) *
p3;
plot for = if isNaN(close)
then double.nan
else n / (4 * Double.Pi) * (lp - lp[2]);
plot "0" = if isNaN(close) then double.nan else 0;

Last edited:
Deep quantitative research suggests the only viable discrete Probability Density Function viable for predicting accurate market moves is the Poisson Distribution.. A Laplace is good for sussing out the average of all the different variable sine wave functions, similar to Tidal Wave motion prediction, also oddly very similar to the market as a whole.

Kyle Algo v1 For ThinkOrSwim Custom 1
Auto Fibonacci Combo For ThinkOrSwim Custom 1
Regularized-Moving-Average Oscillator Suite for ThinkOrSwim Custom 0
RCI with EMA&MACD test For ThinkOrSwim Custom 1
RCI with Stochastic For ThinkOrSwim Custom 1

87k+ Posts
274 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?