Slide 1

Slide 1 text

λ Building Fintech App in Java Tips & Trick

Slide 2

Slide 2 text

THE SPEAKER › Alex Xandra Albert Sim › Principal R&D Engineer at Blibli.com › @bertzzie(.sim)

Slide 3

Slide 3 text

λ Disclaimer Presentations are intended for educational purposes only and not to replace independent professional judgment. The views and opinions expressed in this presentation do not necessarily reflect the official policy or position of blibli.com. Audience discretion is advised.

Slide 4

Slide 4 text

λ Fundamentals of Financial Computation

Slide 5

Slide 5 text

λ Foundational Knowledge › Computational Finance (Keuangan Komputasional) › Emphasis on: – Mathematical Finance (Matematika Keuangan; Teknik Finansial) – Numerical Method (Metode Numerik) › Layman terms: – Analysis – Simulation – Approximation

Slide 6

Slide 6 text

λ Practical Concerns › Calculations in fintech needs to be accurate and precise › Calculations needs to be FAST

Slide 7

Slide 7 text

λ How?

Slide 8

Slide 8 text

λ Accuracy and Precision Java Fintech Tip & Trick #1

Slide 9

Slide 9 text

λ #1 Floating-point Numbers are FAKE NEWS

Slide 10

Slide 10 text

λ Let’s look at some code Click for code

Slide 11

Slide 11 text

λ Output

Slide 12

Slide 12 text

λ Notice › When the fraction is power of 2 (1/2, 1/4) we got correct result › 1/5 is correct before being summed › 1/3, 1/6, and 1/7 have tiny roundoff-error › 1/3 has round-up error on fraction, but round-down error on sum. Vice versa for 1/6 and 1/7

Slide 13

Slide 13 text

λ But HOW?

Slide 14

Slide 14 text

λ The Float Number Format Sign Exponent Fraction Sign: positive (0) or negative (1) Exponent: 127 or -126 to +127 Fraction: the actual fraction value with leading 1 for positive (normalized) and leading 0 for 0 and others (denormalized) -5.5 = 1 1000 0001 011 0000 0000 0000 0000 0000

Slide 15

Slide 15 text

λ The Float Number Format Sign Exponent Fraction Normalized value: = −1 × 2 × 1. Denormalized value: = −1 × 2−126 × 0.

Slide 16

Slide 16 text

λ The Float Number Format Sign Exponent Fraction -5.5 = 1 1000 0001 011 0000 0000 0000 0000 0000 011 0000 0000 0000 0000 0000 1 + 0 + 1 21 + 1 22 + 0 + 0 + 0 + ⋯ 0 1 1 0 0 0 …

Slide 17

Slide 17 text

λ Example! Let’s do some math!

Slide 18

Slide 18 text

λ Conclusion › Float (and double) should not be used to count money (unless you’re counting my debt – then by all means) › They are still useful because it’s fast and quite accurate. And sometimes the mistakes are funny.

Slide 19

Slide 19 text

λ Conclusion (cont.) Use Float (and Double) with Discretion! When in doubt, use BigDecimal

Slide 20

Slide 20 text

λ Overflow Java Fintech Tip & Trick #2

Slide 21

Slide 21 text

λ #2 Watch out for Integer Overflows!

Slide 22

Slide 22 text

λ Java’s Integer Integer Type Size (bits) Min Value Max Value byte 8 -128 127 short 16 -32.768 32.767 char 16 0 65.535 int 32 -2.147.483.648 2.147.483.647 long 64 -9.223.372.036.854.775.808 9.223.372.036.854.775.807

Slide 23

Slide 23 text

λ The Fly-Trap › Notice how max value is always smaller than the positive of min-value? What’s the output? -2147483648 -2147483648

Slide 24

Slide 24 text

λ The Fly-Trap (2) › Notice how max value is always smaller than the positive of min-value? What’s the output? -2147483648 -2147483648

Slide 25

Slide 25 text

λ The Fly-Trap (3) › Notice how max value is always smaller than the positive of min-value? What’s the output? 2147483640 + 10000 = -2147473656

Slide 26

Slide 26 text

λ More Unsafe Code Example

Slide 27

Slide 27 text

λ Safe Code (Java 8+) There’s also Math.subtractExact and Math.negateExact

Slide 28

Slide 28 text

λ Safe Code (Bound Check)

Slide 29

Slide 29 text

λ Safe Code (Bound Check) (2)

Slide 30

Slide 30 text

λ Safe Code (Bound Check) (3) How to do bound check for abs?

Slide 31

Slide 31 text

λ Safe Code (Casting)

Slide 32

Slide 32 text

λ Safe Code Simply: Use BigInteger

Slide 33

Slide 33 text

λ As Binary As Possible Java Fintech Tip & Trick #3

Slide 34

Slide 34 text

λ Binary › Everything inside our computer is represented by binary › There’s usually a way to efficiently calculate stuff with binary, since both CPU and Memory works in Binary

Slide 35

Slide 35 text

λ Binary Example: Power › Simple example, to calculate: = › The simple and naïve approach: = ∙ ∙ ∙ ∙ … › Finishes in linear time:

Slide 36

Slide 36 text

λ Power: the Better Approach = = 14 Get binary rep. of 1110 Convert to power of 2 (right to left) 1 ∙ 2 ∙ 4 ∙ 8 Remove the 0 bit 2 ∙ 4 ∙ 8 Calculate!

Slide 37

Slide 37 text

λ Power Comparison! Click for code

Slide 38

Slide 38 text

λ Power Comparison Result! Result "tech.namas.sharing.numbers.PowerComparison.powBinary": 1188449.776 ±(99.9%) 133027.111 ops/ms [Average] (min, avg, max) = (938259.958, 1188449.776, 1379370.603), stdev = 153194.219 CI (99.9%): [1055422.665, 1321476.887] (assumes normal distribution) Result "tech.namas.sharing.numbers.PowerComparison.powMath": 11479.744 ±(99.9%) 429.096 ops/ms [Average] (min, avg, max) = (9987.543, 11479.744, 12010.515), stdev = 494.148 CI (99.9%): [11050.648, 11908.840] (assumes normal distribution) Benchmark Mode Cnt Score Error Units PowerComparison.powBinary thrpt 20 1188449.776 ± 133027.111 ops/ms PowerComparison.powMath thrpt 20 11479.744 ± 429.096 ops/ms

Slide 39

Slide 39 text

λ Benchmarking Questions › ~10x difference? Is this real life? Or is this fantasy? › Why do we need the (min, max, avg) numbers? › Why is the std-dev differs so much? › What does “normal distribution means?

Slide 40

Slide 40 text

λ Interpolation & Approximation Java Fintech Tip & Trick #4

Slide 41

Slide 41 text

λ Prediction › A lot of fintech use case is analyzing / predicting future values › Will person x, given his/her history, pay his/her debt in time and consistently? › Will investing in share A yield good value given its share value history?

Slide 42

Slide 42 text

λ Interpolation

Slide 43

Slide 43 text

λ Interpolation (n) /in-​ˌtər-pə-​ˈlā-shən/ a method of constructing new data points within the range of a discrete set of known data points Approximation (n) /əˌpräksəˈmāSH(ə)n/ a process of matching two functions that closely match, usually from the same class for specific task

Slide 44

Slide 44 text

λ The Common Polynomial Form › Most function to calculate stuff like this will be shown in polynomial form: = 0 + 1 + 2 2 + ⋯ + › Which are very expensive to calculate (look at all the Math.pow)

Slide 45

Slide 45 text

λ The Newton Form › To simplify, we could use the Newton Form: = 0 + 1 − 0 + 2 − 0 − 1 + … + − 0 − 1 … − −1 › which allows us to cache calculations and apply dynamic programming techniques

Slide 46

Slide 46 text

λ Polynomial to Newton form

Slide 47

Slide 47 text

λ Let’s look at some code Click for code

Slide 48

Slide 48 text

λ Final Result

Slide 49

Slide 49 text

λ Closing

Slide 50

Slide 50 text

λ Summary › Don’t use float. It’s not accurate and it adds up. › Beware of overflows, specifically for big calculations. › Optimize by exploiting the fact that our computer uses binary internally. › Approximate and interpolate where possible.

Slide 51

Slide 51 text

λ Further Discussion › How to store transactions and values? › How to do reporting / analytics fast and real-time? › How to create instant decision-support?

Slide 52

Slide 52 text

λ Question and Answer