# Negative eigenvalues when computing MultiDimensional Scaling given nonnegative distance matrix

I am using the Smile MDS
https://github.com/haifengl/smile/blob/master/core/src/main/java/smile/mds/MDS.java and occasionally running into:

Caused by: java.lang.IllegalArgumentException:
Some of the first 2 eigenvalues are < 0


The data being sent in is a square symmetric distance matrix. I have not put any restrictions on the distance matrix values except they must be nonnegative - which in fact all are between 0 and 1 inclusive.

So two questions:

• under what conditions may such a (nonnegative) matrix result in negative eigenvalues?
• how might I condition the input similarities matrix to avoid the negative values?
• (extra credit): are there other JVM libraries that might help to deal with the existing (arbitrary non-negative) dataset as is?
• You'll have to describe how this implementation works for this question to be answerable. If the software is computing an eigendecomposition of the raw distance matrix, your answer is immediate: distance matrices have 0s on the diagonal. – Sycorax says Reinstate Monica Jul 10 '18 at 17:27

Classical MDS works by using the distance matrix to compute an inner product (Gram) matrix $G$. $G$ contains the dot products between each pair of data points, assuming the distances were Euclidean. It then performs an eigendecomposition of $G$ to embed points into a vector space in a way that that preserves the dot products as well as possible. $G$ can have negative eigenvalues if the original distances were not Euclidean. This can still be ok in some cases. But, if there are many negative eigenvalues, it may indicate that classical MDS is not a good fit for the problem. In this case, nonclassical MDS (of which there are multiple forms) may work better. As Sycorax pointed out, the distance matrix should have zeros on the diagonal.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8235047,"math_prob":0.96981406,"size":753,"snap":"2020-10-2020-16","text_gpt3_token_len":165,"char_repetition_ratio":0.10680908,"word_repetition_ratio":0.0,"special_character_ratio":0.2058433,"punctuation_ratio":0.107913665,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9874397,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-02-26T04:33:24Z\",\"WARC-Record-ID\":\"<urn:uuid:b9e9f44a-d365-45e9-aba1-82a6fde64f31>\",\"Content-Length\":\"140105\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d83c93d9-4f74-4b27-a325-3aa62bdaf172>\",\"WARC-Concurrent-To\":\"<urn:uuid:d69fbb45-33a2-498b-b570-0a9b88a257ba>\",\"WARC-IP-Address\":\"151.101.1.69\",\"WARC-Target-URI\":\"https://stats.stackexchange.com/questions/355456/negative-eigenvalues-when-computing-multidimensional-scaling-given-nonnegative-d\",\"WARC-Payload-Digest\":\"sha1:IWRNFBQEAWIYNBTONTGMVO4F6VZ7X6Z3\",\"WARC-Block-Digest\":\"sha1:GN43OHV72TZCZDALJE6TNIRS6UHXA7PO\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-10/CC-MAIN-2020-10_segments_1581875146186.62_warc_CC-MAIN-20200226023658-20200226053658-00366.warc.gz\"}"}