Keyword density is the percentage of times a keyword or phrase appears on a web page compared to the total number of words on the page. In the context of search engine optimization, keyword density can be used to determine whether a web page is relevant to a specified keyword or keyword phrase.

In the late 1990s, the early days of search engines, keyword density was an important factor in page ranking. However, as webmasters discovered how to implement optimum keyword density, search engines began giving priority to other factors beyond the direct control of webmasters. Today, the overuse of keywords, a practice called keyword stuffing, will cause a web page to be penalized.

The formula to calculate your keyword density on a web page for SEO purposes is , where Nkr is how many times you repeated a specific keyword, and Tkn the total words in the analyzed text. The result is a keyword density value. When calculating keyword density, ignore html tags and other embedded tags which will not appear in the text of the page once published.

When calculating the density of a keyword phrase, the formula would be , where Nwp is the number of words in the phrase. So, for example, for a four-hundred word page about search engine optimization where "search engine optimization" is used four times, the keyword phrase density is (4*3/400)*100 or 3 percent.

From a mathematical viewpoint, the original concept of keyword density refers to the frequency (Nkr) of appearance of a keyword in a dissertation. A "keyword" consisting of multiple terms, e.g. "blue suede shoes," is an entity in itself.  T frequency of the phrase "blue suede shoes" within a dissertation drives the key(phrase) density.  It is "more" mathematically correct for a "keyphrase" to be calculated just like the original calculation, but considering the word group, "blue suede shoes," as a single appearance, not three: 

Density = ( Nkr / Tkn ) * 100.

'Keywords' (kr) that consist of several words artificially inflate the total word count of the dissertation. The purest mathematical representation should adjust the total word count (Tkn) lower by removing the excess key(phrase) word counts from the total: 

Density = ( Nkr / ( Tkn -( Nkr * ( Nwp-1 ) ) ) ) * 100.
where Nwp = the number of terms in the keyphrase.

See also
Dynamic Keyword Insertion

References

Search engine optimization