O'Reilly logo

Clojure for Finance by Timothy Washington

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Applying functions

Let's make a function to determine phases or the start and end of our graph's cycles. We'll write a function that finds the x intercepts of both the polynomial and sine graphs. Recall that these two functions appear as follows:

(defn polynomial [a b c x]
  (->
   (+ (* a
         (Math/pow x 3))

      (* b
         (Math/pow x 2)))
   (- (* c x))))

(defn sine [a b d x]
  (- (* a
        (Math/sin (* b
                     (- x
                        (/ Math/PI 2)))))
     d))

Looking again at the representative graphs, the first thing to note is a constant x intercept of x=0. Next, if we go in either direction, the graph goes in a particular direction, then returns to pass through x, and winds back up again:

Applying functions

The graph ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required