Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Thursday, 28 November 2019

How to stringify JSON for hashing and easy comparison in Java?

Or how to implement https://github.com/substack/json-stable-stringify in Java?
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.SerializationFeature;

    public static void stringify(final String jsonPath) throws Exception {
        final byte[] bytes = Files.readAllBytes(Paths.get(jsonPath));
        final String jsonData = new String(bytes);

        ObjectMapper om = new ObjectMapper();
        om.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
        Map map = om.readValue(jsonData, HashMap.class);
        String json = om.writeValueAsString(map);
    }

Thursday, 13 October 2016

Android SDK

Is it time for Google to start switching from Java to Kotlin for its Android development language? This makes sense right? First move is to use IntelliJ, second step, introduce Kotlin as an alternative language, third step, remove Java.

Monday, 4 October 2010

Brent

Whoever is interested in p2y/y2p interpolations, or others root-finding techniques, should check-out the Brent's root-finding algorithm implemented as part of Apache Commons Math.

Wednesday, 29 September 2010

Thursday, 11 February 2010

Free Desktop Password Manager

I wrote this little piece of software that uses AES-256 to encrypt your passwords. Comments welcomed.

Check it out.

Wednesday, 27 January 2010

Android First App: Bezier Curve (2)

A fancier version.
I have created my Android Market account, paid the fee... now I will polish this simple first app and upload it to the market.
I already have another idea for my next app: A Password Manager. I know there are quite a few around, but security wise, only trust yourself. I will also provide a desktop version (Web Started).


Tuesday, 26 January 2010

Thursday, 21 January 2010

Standard Deviation at n+1

I faced a little problem recently @ work: the need to generate real time standard deviation for millions of data (streamed continuously).

So the idea is to find a recurrence relationship for the standard deviation. From the initial Wiki formula, and after 5 lines of easy maths, you end up with:



From that you see that you just need to store the previous standard deviation, the sum of xi, and the sum of the xi square. You just have to initialize with two values, and after that, you calculate the new standard deviation on the fly for the new xi.

Looks like this in Java


public class StandardDeviation {
private double sumXis = Double.NaN, sumXis2 = Double.NaN;
private double n = Double.NaN;
private double stdev2 = Double.NaN;
private double result = Double.NaN;

public StandardDeviation() {
}

public double stdDev(final double[] xs) {
final int length = xs.length;
n = length;

double sdx1 = 0.0;
double sdx2 = 0.0;
for(int i = 0; i < length; i++) {
sdx1 += xs[i] * xs[i];
sdx2 += xs[i];
}
sumXis = sdx2;
sumXis2 = sdx1;
sdx2 = sdx2 * sdx2;

final double std2 = (length * sdx1 - sdx2) / (length * (length-1));
stdev2 = std2;
final double std = Math.sqrt(std2);

result = std;

return std;
}

public double stdDev(final double xs) {
final double stdev2n1 = (n * (n - 1.0) * stdev2 + n * xs * xs + sumXis2 - 2 * xs * sumXis)
/ (n * (n + 1));

sumXis += xs;
sumXis2 += xs * xs;
stdev2 = stdev2n1;
n += 1;

final double result = Math.sqrt(stdev2n1);

this.result = result;

return result;
}

public double getResult() {
return result;
}
}

Thursday, 31 December 2009

Java for Quants? Step 2

Following up the conversation, another LinkedIn member, Daniel D wrote:


Some personal comparisons between Java and C++.
1. Advanced, mature generic programming (templates); Java 1.5 seems to be an afterthought, I suspect it is not even wrong.


Daniel is right. It is an after thought. You want "real" and nice templates, use Scala (runs on the JVM)


2. Memory management is deterministic in C++ (Garbage collectors are non-deterministic)


Again, I agree. However, does it matter? When I write C++ code, I have yet to find a pattern where I need the deterministic behavior that Daniel is talking about; in Java, you can always put the reference to null, or use fancier weak references classes if needed; or use jRockit realtime, or start experiencing with G1.


3. Language features; C++ supports modular programming, in addition to OOP and GP. In Java everthing is an *object*; very convenient, but too restrictive


Well, yes and no. Too much to talk about here. Not everything is an object, like primitive types, but they have a type, ie, you can write int.class if you want to. Scala is much more modular, but everything is an object in Scala. But that does not bother me.


4. Java does not support operator overloading (cannie say Matrix m3 = m1*m2)


Use Scala.


5. C++ interfaces well with the hardware and software drivers


Java as well, via JNI.


6. C++ is terse; Java tends to be verbose


It all depends on the programmer, so not an argument, but in any case, want to use a concise language on the VM? Use Scala.

Wednesday, 30 December 2009

Java for Quants?

There was an lively debate on LinkedIn with respect of using Java for Quantitative work.
As usual, some people were quick at pointing out Java's “slowness” compared to C++.
Obviously, yours truly, tried to demystify that point.

Java is as fast and sometimes faster than C++. Do not believe me, try it out for yourself. Try Java 6, in server side mode with asynchronous garbage collection, the performance will be mind-blowing, or at least, if you were sceptical, will be unbelievable.
Try java -server +XX:UseConcMarkSweepGC with a program, develop the same one in C++ and compare the performance between the two.
Make sure you read this entry first.

My answer to Java for quants is not about the language but about the architecture and design.
It does make sense to write a quant lib in C/C++, why? Because of speed, no – because if speed is your concern, you'd go C/ASM or a hardware solution; because of ease of use with other systems: writing a .dll or .so allows your lib to be called from .NET, Java (via JNI), Excel, etc.

People are very sensitive about C++ vs. Java – what about trying it yourself and be objective?

Also, let's not talk about Java only, it is also and mainly about the VM, which supports many other languages, in particular Scala, OCAML, Jruby, ...

Wednesday, 25 November 2009

Sunday, 4 October 2009

CEP - Complex Event Processing

Interested in developing complex algo trading for the financial world?
Check out this CEP framework, open-source, called Esper

Saturday, 26 September 2009

C++/C# versus Java coding productivity

I still do not get it. I see some colleagues at work coding in C# using Visual Studio .NET – mainly GUIs. I see other colleagues writing C++ (Server-side), finally others doing both in Java (Swing, JDK 6). In terms of productivity it is blatantly obvious that the Java boys – they deliver fast, and well. The C# boys are struggling with Visual Studio (what a pile of c..p) – when you compare it with Eclipse (or even Netbeans) – I am not even talking about the C++ guys stuck with VC++ 6.0 (1998!).

What I do not get is why people get stuck in inferior technologies and do not even have the will do move to another platform and take advantage of powerful IDEs (Eclipse, Netbeans) and very nice monitoring and management tools (VisualVM).

I am not going to even start talking about performance (Java is slow (FALSE!), C++ is faster (FALSE!), Java does not use native threads (FALSE!), Java is not suited for multi-cores processors (FALSE!)...

Ah la la!

Tuesday, 22 September 2009

Blog Archive