Home | History | Annotate | Line # | Download | only in wscons
mra.c revision 1.6
      1  1.6  khorben /*	$NetBSD: mra.c,v 1.6 2014/03/14 05:03:19 khorben Exp $	*/
      2  1.1   tsarna 
      3  1.1   tsarna /*
      4  1.1   tsarna  * Copyright (c) 1999 Shin Takemura All rights reserved.
      5  1.1   tsarna  * Copyright (c) 1999 PocketBSD Project. All rights reserved.
      6  1.1   tsarna  *
      7  1.1   tsarna  * Redistribution and use in source and binary forms, with or without
      8  1.1   tsarna  * modification, are permitted provided that the following conditions
      9  1.1   tsarna  * are met:
     10  1.1   tsarna  * 1. Redistributions of source code must retain the above copyright
     11  1.1   tsarna  *    notice, this list of conditions and the following disclaimer.
     12  1.1   tsarna  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1   tsarna  *    notice, this list of conditions and the following disclaimer in the
     14  1.1   tsarna  *    documentation and/or other materials provided with the distribution.
     15  1.1   tsarna  *
     16  1.4    peter  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1   tsarna  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1   tsarna  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.4    peter  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1   tsarna  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1   tsarna  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1   tsarna  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1   tsarna  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1   tsarna  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1   tsarna  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1   tsarna  * SUCH DAMAGE.
     27  1.1   tsarna  *
     28  1.1   tsarna  */
     29  1.1   tsarna 
     30  1.1   tsarna #include <sys/cdefs.h>
     31  1.6  khorben __KERNEL_RCSID(0, "$NetBSD: mra.c,v 1.6 2014/03/14 05:03:19 khorben Exp $");
     32  1.1   tsarna 
     33  1.1   tsarna #include <sys/param.h>
     34  1.1   tsarna #include <sys/systm.h>
     35  1.1   tsarna 
     36  1.2      uwe extern int mra_Y_AX1_BX2_C(const int *, int,
     37  1.2      uwe 			   const int *, int, const int *, int, int, int,
     38  1.2      uwe 			   int *, int *, int *);
     39  1.1   tsarna 
     40  1.1   tsarna /*
     41  1.1   tsarna  * multiple regression analysis
     42  1.1   tsarna  * Y = AX1 + BX2 + C
     43  1.1   tsarna  */
     44  1.1   tsarna int
     45  1.2      uwe mra_Y_AX1_BX2_C(const int *y, int ys,
     46  1.2      uwe 		const int *x1, int x1s, const int *x2, int x2s,
     47  1.2      uwe 		int n, int scale,
     48  1.2      uwe 		int *a, int *b, int *c)
     49  1.1   tsarna {
     50  1.1   tsarna 	int i;
     51  1.1   tsarna 	int64_t X1a, X2a, Ya;
     52  1.1   tsarna 	int64_t X1X1s, X2X2s, X1X2s;
     53  1.1   tsarna 	int64_t YYs, X1Ys, X2Ys;
     54  1.1   tsarna 	int64_t S11, S22, S12;
     55  1.5   martin //	int64_t SYY;
     56  1.5   martin 	int64_t S1Y, S2Y;
     57  1.1   tsarna 	int64_t A, B, C, M;
     58  1.6  khorben #define AA(p, s, i)	(*((const int *)(((const char *)(p)) + (s) * (i))))
     59  1.1   tsarna #define X1(i)		AA(x1, x1s, i)
     60  1.1   tsarna #define X2(i)		AA(x2, x2s, i)
     61  1.1   tsarna #define Y(i)		AA(y, ys, i)
     62  1.1   tsarna 
     63  1.1   tsarna 	/*
     64  1.1   tsarna 	 * get avarage and sum
     65  1.1   tsarna 	 */
     66  1.1   tsarna 	X1a = 0;	X2a = 0;	Ya = 0;
     67  1.1   tsarna 	X1X1s = 0;	X2X2s = 0;	X1X2s = 0;
     68  1.1   tsarna 	X1Ys = 0;	X2Ys = 0;	YYs = 0;
     69  1.1   tsarna 	for (i = 0; i < n; i++) {
     70  1.1   tsarna 		X1a += X1(i);
     71  1.1   tsarna 		X2a += X2(i);
     72  1.1   tsarna 		Ya += Y(i);
     73  1.1   tsarna 
     74  1.1   tsarna 		X1X1s += X1(i) * X1(i);
     75  1.1   tsarna 		X2X2s += X2(i) * X2(i);
     76  1.1   tsarna 		X1X2s += X1(i) * X2(i);
     77  1.1   tsarna 
     78  1.1   tsarna 		X1Ys += X1(i) * Y(i);
     79  1.1   tsarna 		X2Ys += X2(i) * Y(i);
     80  1.1   tsarna 		YYs += Y(i) * Y(i);
     81  1.1   tsarna 	}
     82  1.1   tsarna 	X1a /= n;	X2a /= n;	Ya /= n;
     83  1.1   tsarna 
     84  1.1   tsarna 	S11 = X1X1s - n * X1a * X1a;
     85  1.1   tsarna 	S22 = X2X2s - n * X2a * X2a;
     86  1.1   tsarna 	S12 = X1X2s - n * X1a * X2a;
     87  1.1   tsarna 
     88  1.5   martin //	SYY = YYs - n * Ya * Ya;
     89  1.1   tsarna 	S1Y = X1Ys - n * X1a * Ya;
     90  1.1   tsarna 	S2Y = X2Ys - n * X2a * Ya;
     91  1.1   tsarna 
     92  1.1   tsarna #if 0
     93  1.1   tsarna 	printf("X1a=%d X2a=%d Ya=%d\n", (int)X1a, (int)X2a, (int)Ya);
     94  1.1   tsarna 	printf("X1X1s=%d X2X2s=%d X1X2s=%d\n", (int)X1X1s, (int)X2X2s, (int)X1X2s);
     95  1.1   tsarna 	printf("X1Ys=%d X2Ys=%d YYs=%d\n", (int)X1Ys, (int)X2Ys, (int)YYs);
     96  1.1   tsarna 	printf("S11=%d S22=%d S12=%d\n", (int)S11, (int)S22, (int)S12);
     97  1.1   tsarna 	printf("SYY=%d S1Y=%d S2Y=%d\n", (int)SYY, (int)S1Y, (int)S2Y);
     98  1.1   tsarna #endif
     99  1.1   tsarna 
    100  1.1   tsarna 	M = (S11 * S22 - S12 * S12);
    101  1.1   tsarna 	if (M == 0) {
    102  1.1   tsarna 		/* error */
    103  1.1   tsarna 		return -1;
    104  1.1   tsarna 	}
    105  1.1   tsarna 
    106  1.1   tsarna 	A = (S1Y * S22 - S2Y * S12) * scale / M;
    107  1.1   tsarna 	B = (S2Y * S11 - S1Y * S12) * scale / M;
    108  1.1   tsarna 	C = Ya - (A * X1a + B * X2a) / scale;
    109  1.1   tsarna 
    110  1.1   tsarna #if 0
    111  1.1   tsarna 	printf("A=%d B=%d C=%d\n", (int)A, (int)B, (int)C);
    112  1.1   tsarna #endif
    113  1.1   tsarna 	*a = A;
    114  1.1   tsarna 	*b = B;
    115  1.1   tsarna 	*c = C;
    116  1.1   tsarna 
    117  1.1   tsarna 	return (0);
    118  1.1   tsarna }
    119