Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building Fintech App in Java: Tips & Trick

Building Fintech App in Java: Tips & Trick

The talk is for JVM Meetup #18 featured here: https://www.eventbrite.com/e/jvm-meetup-18-with-tunai-kita-late-night-tech-tickets-58411026954

We discuss the basics of calculating big numbers and do numerical analysis on Java in this presentation.

Source code for the talk can be found at: https://github.com/JVMDeveloperID/jvm-fintech-tips-trick

Alex Xandra Albert Sim

March 28, 2019
Tweet

More Decks by Alex Xandra Albert Sim

Other Decks in Technology

Transcript

  1. THE SPEAKER › Alex Xandra Albert Sim › Principal R&D

    Engineer at Blibli.com › @bertzzie(.sim)
  2. λ 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.
  3. λ Foundational Knowledge › Computational Finance (Keuangan Komputasional) › Emphasis

    on: – Mathematical Finance (Matematika Keuangan; Teknik Finansial) – Numerical Method (Metode Numerik) › Layman terms: – Analysis – Simulation – Approximation
  4. λ Practical Concerns › Calculations in fintech needs to be

    accurate and precise › Calculations needs to be FAST
  5. λ 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
  6. λ 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
  7. λ The Float Number Format Sign Exponent Fraction Normalized value:

    = −1 × 2 × 1. Denormalized value: = −1 × 2−126 × 0.
  8. λ 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 …
  9. λ 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.
  10. λ 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
  11. λ The Fly-Trap › Notice how max value is always

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

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

    always smaller than the positive of min-value? What’s the output? 2147483640 + 10000 = -2147473656
  14. λ 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
  15. λ Binary Example: Power › Simple example, to calculate: =

    › The simple and naïve approach: = ∙ ∙ ∙ ∙ … › Finishes in linear time:
  16. λ 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!
  17. λ 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
  18. λ 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?
  19. λ 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?
  20. λ 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
  21. λ 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)
  22. λ 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
  23. λ 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.
  24. λ Further Discussion › How to store transactions and values?

    › How to do reporting / analytics fast and real-time? › How to create instant decision-support?