Is it legal to publish the code of a published algorithm? Let's say I want to implement an algorithm based on a paper or book and publish it under a non-proprietary but not necessarily non-commercial-friendly license (e.g. on a blog).
Is it legal to do this? 
I know that a general all covering answer cannot be either yes or no, so I want to know additionally how to find out quickly whether this is legal or not. Some examples would be great. Here are some examples for discussion


*

*Apache Commons Math stated in their developer guide, that all developers should check for license issues before committing and link to Numerical Recipes as an example. This is the first time ever  I saw such a warning. I do not have access to this book (neither online nor in dead-tree-format). What does the legal warning looks like ?

*Papers linked for download on the page of the author normally do not contain a legal warning. Does it mean that the algorithms are ... free?

*According to wikipedia, Random Forest is a trademarked term. Does that mean, that noone is allowed to implement this algorithm ? One can give it another name (like Good-Luck-Forests), since reproducing the exact algorithm given that not all details are published in paper (normally) is nearly impossible. 

*What about papers where the access is both restricted and not. See for example the paper "Alternatives to the Median Absolute Deviation", which has been published in the Journal of the American Statistical Association, but which can be bought now via JSTOR or be downloaded from this page. Did Frank Masci, the uploader of this paper, break the law?


A lot of papers with or without restricted access can be found online. 
Disclaimer: No answer should be treated as legal advice one can refer to before court.
 A: As mentioned by the OP, this is probably not the right place for expert advice on legal issues, but we all have to live with such things as software licenses and try not to get into trouble, so here are a few things that I have learned. 


*

*On the NR homepage you can find the license information and information on redistribution. This is solely a copyright issue regarding the source code provided in the books. The algorithms themselves are not copyrighted, and to my understanding you can't get into trouble by implementing and sharing an algorithm that happens to be in NR unless your implementation is derived from the source code distributed with NR. 

*Hmm, nothing is free ... There is almost always a copyright holder. If the paper is published, the copyright may be transferred to the journal, but in some cases the author retain the right to distribute the paper via his or her homepage, say. Patent law is a completely different ball game, but I actually don't know of any examples related to statistical and machine learning algorithms where a patent protection of the algorithm was a problem. Hence, to me, the most important aspect regarding algorithms in papers, whether published in journals or on the authors homepage, is not to violate the copyright.

*A trademark is a third thing. To my understanding it protects only the name or symbol. So you just can't implement a program and call it "Random Forests", but you can implement and distribute the random forest algorithm. 

*I have no idea if Frank Masci broke the law, but it is a copyright issue as I see it. Who holds the copyright and which rights did the copyright holder give to others?


From my point of view, though violations of patent rights might be serious, the more important issue for the average statistician is that of copyright. If you implement an algorithm from a paper from scratch and cite the paper I don't see any obvious problem with distributing the implementation regardless of the media. But you might think about what is the best way for you to use your copyright on the implementation. I don't know anything about how blogs are generally copyrighted, but if the copyright is transferred to the blog owner automatically, it might, in principle, be a bad idea to post hours of valuable implementations on a blog and thereby effectively give up the copyright of the work to somebody else.
If you, on the other hand, modify an existing implementation, you could run into problems with the copyright license. If the license for the original implementation is GPL, the license for the redistribution has to be GPL. Hence, you have to distribute in a way so that the distribution can be under GPL. This works the other way too. If you want to distribute an implementation under GPL but have to rely on a library that is not distributed under the GNU license, then you might not be able to include the library in your distribution $-$ even if the library is open source and "free". The library might be distributed under a copyright license that is incompatible with GPL.
A: The classic example of a patented algorithm is RSA, by the way.  Rules for patents of algorithms are rather nebulous and changing quite a bit.  In practice, implementations are okay, but distribution (including commercialization and free release) is where one tends to run afoul of things.  What's more, release can be constrained, regardless of patent and copyright - Americans cannot export source code for cryptographic algorithms to certain prohibited countries, for instance, and the same may be true of many other countries.
A distinction has to be made between copyright and patents.  Copyright is unlikely to affect you, and I believe the primary thing to consider is whether or not a patent may apply (and it need not be patented already: some countries allow some time after disclosure for the innovator to patent their work).  Copyrights tend to affect distribution rights of original materials (so, you can't just republish an article - the journal or the author owns the copyright), patents affect distribution of the implementation of the idea.  If there's no patent and the time for patenting has elapsed, then you should be good to go.
I'd said that patents are nebulous: in some jurisdictions it isn't all that easy to patent algorithms.  In some it is, but the laws and interpretations of those laws are changing.
I hope this helps in focusing your analysis.  I wouldn't worry about the case-by-case approach (e.g. is it published in this type of journal or that type), and instead focus on who holds the patent rights and how are they exercising those rights.
It's also courteous to talk with the original innovator.  If they intend to patent or they want to collaborate, you are better off working with them.  They can help suggest ideas for implementations and they may encourage others to use your implementation as a reference.
A: My understanding is that publication disqualifies an invention for patenting.  Thus any algorithm that has been published can be used freely.  That does not apply to the code itself!  If you learn the algorithm, understand it, teach it to another person without ever letting them see the code in the original, and they implement it, it is completely unencumbered.  That's the gold standard of clean room implementation (used for legally safe reverse engineering), but if you read the paper and write your own version, you're probably fine.
