1. A pattern inspection apparatus having a microprocessor comprising: an optical image acquisition unit configured to acquire an optical image of a target workpiece to be inspected on which a pattern is formed; a reference image generation unit configured to generate a reference image to be compared with the optical image; a least-squares method calculation unit configured, by using a mathematical model in which a parallel shift amount displaced from a position where the optical image and the reference image are aligned by a pixel unit, an expansion and contraction error coefficient, a rotation error coefficient, a gray-level offset and an image transmission loss ratio are parameters, to calculate each of the parameters by a least-squares method; a corrected image generation unit configured to generate a corrected image by shifting a position of the reference image by a displacement amount displaced from the position aligned by the pixel unit, based on the each of the parameters; and a comparison unit implemented by the microprocessor and configured to compare the corrected image with the optical image, wherein the least-squares method calculation unit calculates a gray-level offset, an image transmission loss ratio, a parallel shift amount in X direction, a parallel shift amount in Y direction, an expansion and contraction error coefficient in the X direction, an expansion and contraction error coefficient in the Y direction, a rotation error coefficient in the X direction, and a rotation error coefficient of the Y direction so that a first difference obtained by subtracting a second sum that is a sum of a product of the expansion and contraction error coefficient in the X direction, a value obtained by partially differentiating a pixel value of the reference image with respect to x, and a variable x, a product of the expansion and contraction error coefficient in the Y direction, a value obtained by partially differentiating the pixel value of the reference image with respect to y, and a variable y, and a product of the rotation error coefficient in the Y direction, the value obtained by partially differentiating the pixel value of the reference image with respect to y, and the variable x from a first sum that is a sum of the gray-level offset, a product of the image transmission loss ratio and the pixel value of the reference image, a product of the parallel shift amount in the X direction and the value obtained by partially differentiating the pixel value of the reference image with respect to x, a product of the parallel shift amount in the Y direction and the value obtained by partially differentiating the pixel value of the reference image with respect to y, and aproduct of the rotation error coefficient in the X direction, the value obtained by partially differentiating the pixel value of the reference image with respect to x, and the variable y may be equal to a second difference obtained by subtracting a pixel value of the optical image from the pixel value of the reference image.