The Federal Home Loan Mortgage Co., which buys loans from lenders and sells securities based on the loans, reported record earnings of $301 million ($18.12 per share of preferred stock) in 1987, a 22 percent increase from a year ago.

The increase was partially attributable to a lower 1987 tax rate, the company said.

For the fourth quarter, net income totaled $77 million, an 18 percent increase from a year ago. The fourth-quarter figure was down 9 percent from the 1987 third quarter.

Freddie Mac said the decline, which was anticipated, resulted primarily from a decrease in mortgage prepayments.

Freddie Mac's preferred stock trades on the New York Stock Exchange, but its ownership is limited to thrift institutions.

The corporation said it purchased $77 billion of mortgages during 1987, down from $103 billion in 1986. During 1987, Freddie Mac sold $75 billion of mortgage-related securities, compared with sales of $100 billion in 1986.

Freddie Mac's provision for mortgage loan losses and administrative expenses increased by $95 million, to $325 million, in 1987, primarily because of the increase in its total mortgage portfolio to $226 billion, from $183 billion at the end of 1986. Penril Corp. reported a second-quarter profit of $763,000 (33 cents a share), compared with a loss of $1.3 million (60 cents) in the comparable quarter a year ago.

The 1986 figure included a $1.5 million loss from discontinued operations, and the 1987 quarterly profit reflects a one-time gain of $256,000 from the repurchase of company debt.

Revenue in the current second quarter was $10.1 million, virtually flat compared with the year-ago figure.

For the six months, the company earned $1.1 million (44 cents), compared with a loss of $1.48 million (68 cents) a year ago.

The 1986 figure reflected a loss of $2.3 million for discontinued operations.

Revenue for the six months was $19.2 million, compared with $19.56 million a year ago.

The Rockville-based company manufactures data communications equipment and test and measurement instruments.