Account structure with multiple nested and recursive parent / child hierarchy - tsql
This is my very first question asked (ever). So, please be patient while I try to explain my problem.
I have a table as follows which is giving me the hierarchy structure for some account:
declare #AccountDefinitions table
(
AccountID int identity(1, 1)
primary key
not null,
AccountNumber nvarchar(11) not null,
AccountType nvarchar(1) not null,
SumSign1 smallint,
SumAccount1 nvarchar(11),
SumSign2 smallint,
SumAccount2 nvarchar(11),
SumSign3 smallint,
SumAccount3 nvarchar(11)
)
If an account is summed up (added to another acount) then the parent account is in SumAccount1. So, SumAccount1 can be found in AccountNumber again. That's the ordinary parent / child hierarchy which could be easily solved with a CTE as described in multiple solutions.
Yet, there are two more parent columns with the names SumAccount2 and SumAccount3. This means that an account may have up to three parents. But sometimes there are only two parents or no parents. In such cases SumAccount3 (and / or the others) would be NULL.
Basically, now I would need a nested CTE to deal with these multiple possible parents which may lead to multiple different paths. If an account has three parents and each of these have another three parents then there are already nine different paths this account could be potentially added up to.
Here is some test data to play around with:
insert into #AccountDefinitions
(AccountNumber, AccountType, SumSign1, SumAccount1, SumSign2,
SumAccount2, SumSign3, SumAccount3)
values (N'K07_005', N'U', 0, null, 0, null, 0, null),
(N'K07_010', N'U', 0, null, 0, null, 0, null),
(N'K07_010A', N'U', 0, null, 0, null, 0, null),
(N'K07_020', N'R', 0, null, 0, null, 0, null),
(N'K07_020A', N'R', 0, null, 0, null, 0, null),
(N'K07_020AA', N'R', 0, null, 0, null, 0, null),
(N'K07_021', N'U', 0, null, 0, null, 0, null),
(N'K07_022', N'U', 0, null, 0, null, 0, null),
(N'K07_025', N'U', 0, null, 0, null, 0, null),
(N'K07_025A', N'U', 0, null, 0, null, 0, null),
(N'K07_035', N'U', 1, N'K07_060', 0, null, 0, null),
(N'K07_060', N'U', 1, N'K07_930', 0, null, 0, null),
(N'K07_090', N'U', 0, null, 0, null, 0, null),
(N'K07_091', N'U', 0, null, 0, null, 0, null),
(N'K07_091P', N'U', 0, null, 1, N'K07_O091', 0, null),
(N'K07_092', N'U', 0, null, 0, null, 0, null),
(N'K07_095', N'U', 0, null, 0, null, 0, null),
(N'K07_096', N'U', 0, null, 0, null, 0, null),
(N'K07_100', N'U', 0, null, 1, N'K07_1000', 0, null),
(N'K07_1000', N'I', 1, N'K07_O100', 0, null, 0, null),
(N'K07_1010', N'I', 1, N'K07_100', 1, N'K07_101P', 1, N'K07_C130'),
(N'K07_101P', N'U', 1, N'K07_O101', 0, null, 0, null),
(N'K07_110', N'I', 1, N'K07_130', 0, null, 0, null),
(N'K07_120', N'I', 1, N'K07_130', 0, null, 0, null),
(N'K07_120P', N'U', 1, N'K07_O120', 0, null, 0, null),
(N'K07_160', N'I', 1, N'K07_170', 0, null, 0, null),
(N'K07_170', N'I', 1, N'K07_251', 0, null, 0, null),
(N'K07_180', N'U', 1, N'K07_190', 0, null, 0, null),
(N'K07_185', N'S', 1, N'K07_190', 0, null, 0, null),
(N'K07_190', N'S', 0, null, 0, null, 0, null),
(N'K07_200', N'I', 1, N'K07_2000', 1, N'K07_250', 0, null),
(N'K07_2000', N'U', 0, null, 0, null, 0, null),
(N'K07_201', N'I', 1, N'K07_C810', 1, N'K07_900', 1, N'K07_200'),
(N'K07_202', N'I', 1, N'K07_202P', 1, N'K07_251', 0, null),
(N'K07_202P', N'U', 0, null, 0, null, 0, null),
(N'K07_205', N'C', 1, N'K07_091P', -1, N'K07_251', 0, null),
(N'K07_250', N'U', 1, N'K07_300', 1, N'K07_2500', 0, null),
(N'K07_2500', N'U', 0, null, 0, null, 0, null),
(N'K07_251', N'I', 1, N'K07_301', 1, N'K07_251P', 0, null),
(N'K07_251P', N'U', 0, null, 0, null, 0, null),
(N'K07_270', N'T', -1, N'K07_300', 1, N'K07_2700', 0, null),
(N'K07_2700', N'T', 1, N'K07_O270', 0, null, 0, null),
(N'K07_271', N'C', -1, N'K07_301', 1, N'K07_271P', 0, null),
(N'K07_271P', N'T', 0, null, 1, N'K07_O271', 0, null),
(N'K07_280', N'T', -1, N'K07_300', 1, N'K07_2800', 0, null),
(N'K07_2800', N'T', 1, N'K07_O280', 0, null, 0, null),
(N'K07_281', N'C', -1, N'K07_301', 1, N'K07_281P', 0, null),
(N'K07_281P', N'T', 0, null, 1, N'K07_O281', 0, null),
(N'K07_300', N'U', 1, N'K07_400', 1, N'K07_3000', 0, null),
(N'K07_3000', N'U', 0, null, 0, null, 0, null),
(N'K07_301', N'I', 1, N'K07_401', 1, N'K07_301P', 0, null),
(N'K07_301P', N'U', 0, null, 0, null, 0, null),
(N'K07_320', N'T', 1, N'K07_400', 0, null, 0, null),
(N'K07_321', N'I', 1, N'K07_401', 1, N'K07_342', 0, null),
(N'K07_322', N'I', 1, N'K07_401', 1, N'K07_342', 0, null),
(N'K07_330', N'T', 1, N'K07_400', 0, null, 0, null),
(N'K07_331', N'I', 1, N'K07_401', 0, null, 0, null),
(N'K07_340', N'T', 1, N'K07_400', 0, null, 0, null),
(N'K07_341', N'I', 1, N'K07_401', 1, N'K07_342', 0, null),
(N'K07_342', N'C', 0, null, 0, null, 0, null),
(N'K07_350', N'T', 1, N'K07_400', 0, null, 0, null),
(N'K07_351', N'C', 1, N'K07_401', 0, null, 0, null),
(N'K07_400', N'U', 1, N'K07_600', 0, null, 0, null),
(N'K07_401', N'C', 1, N'K07_601', 1, N'K07_510', 0, null),
(N'K07_420', N'T', 1, N'K07_500', 0, null, 0, null),
(N'K07_421', N'C', 1, N'K07_501', 1, N'K07_502', 0, null),
(N'K07_430', N'T', 1, N'K07_500', 0, null, 0, null),
(N'K07_431', N'C', 1, N'K07_501', 1, N'K07_502', 0, null),
(N'K07_440', N'T', 0, null, 0, null, 0, null),
(N'K07_441', N'T', 1, N'K07_465', 0, null, 0, null),
(N'K07_450', N'T', 0, null, 0, null, 0, null),
(N'K07_451', N'T', 1, N'K07_465', 0, null, 0, null),
(N'K07_455', N'C', 1, N'K07_463', 1, N'K07_466', 0, null),
(N'K07_456', N'U', 1, N'K07_465', 0, null, 0, null),
(N'K07_457', N'C', 1, N'K07_463', 1, N'K07_466', 0, null),
(N'K07_458', N'C', 1, N'K07_463', 1, N'K07_466', 0, null),
(N'K07_459', N'C', 1, N'K07_463', 1, N'K07_466', 0, null),
(N'K07_460', N'T', 0, null, 0, null, 0, null),
(N'K07_461', N'T', 1, N'K07_465', 0, null, 0, null),
(N'K07_462', N'T', 1, N'K07_465', 0, null, 0, null),
(N'K07_463', N'T', 1, N'K07_501', 0, null, 0, null),
(N'K07_464', N'T', 1, N'K07_465', 0, null, 0, null),
(N'K07_465', N'T', 1, N'K07_500', 0, null, 0, null),
(N'K07_466', N'C', 0, null, 1, N'K07_502', 0, null),
(N'K07_470', N'T', 0, null, 0, null, 0, null),
(N'K07_471', N'T', 1, N'K07_485', 0, null, 0, null),
(N'K07_472', N'C', 1, N'K07_475', 0, null, 0, null),
(N'K07_473', N'C', 1, N'K07_475', 0, null, 0, null),
(N'K07_474', N'C', 1, N'K07_475', 0, null, 0, null),
(N'K07_475', N'C', 1, N'K07_501', 1, N'K07_502', 0, null),
(N'K07_476', N'C', 1, N'K07_475', 0, null, 0, null),
(N'K07_480', N'T', 0, null, 0, null, 0, null),
(N'K07_481', N'T', 1, N'K07_485', 0, null, 0, null),
(N'K07_482', N'T', 1, N'K07_485', 0, null, 0, null),
(N'K07_483', N'T', 1, N'K07_485', 0, null, 0, null),
(N'K07_484', N'T', 1, N'K07_485', 0, null, 0, null),
(N'K07_485', N'T', 1, N'K07_500', 0, null, 0, null),
(N'K07_488', N'T', 1, N'K07_500', 0, null, 0, null),
(N'K07_489', N'C', 1, N'K07_501', 1, N'K07_502', 0, null),
(N'K07_490', N'T', 1, N'K07_500', 0, null, 0, null),
(N'K07_491', N'C', 1, N'K07_501', 1, N'K07_502', 0, null),
(N'K07_492', N'C', 1, N'K07_501', 1, N'K07_502', 0, null),
(N'K07_500', N'T', -1, N'K07_600', 0, null, 0, null),
(N'K07_501', N'T', -1, N'K07_601', 0, null, 0, null),
(N'K07_502', N'C', -1, N'K07_510', 0, null, 0, null),
(N'K07_510', N'C', 0, null, 0, null, 0, null),
(N'K07_520', N'T', -1, N'K07_600', 0, null, 0, null),
(N'K07_521', N'C', -1, N'K07_601', 1, N'K07_466', 0, null),
(N'K07_522', N'C', -1, N'K07_601', 1, N'K07_466', 0, null),
(N'K07_530', N'U', 1, N'K07_600', 0, null, 0, null),
(N'K07_531', N'I', 1, N'K07_601', 1, N'K07_591', 0, null),
(N'K07_532', N'C', -1, N'K07_601', 1, N'K07_703', 1, N'K07_502'),
(N'K07_533', N'I', 1, N'K07_601', 1, N'K07_591', 0, null),
(N'K07_540', N'U', 1, N'K07_600', 0, null, 0, null),
(N'K07_541', N'I', 1, N'K07_601', 1, N'K07_591', 0, null),
(N'K07_551', N'I', 1, N'K07_601', 1, N'K07_591', 0, null),
(N'K07_561', N'C', -1, N'K07_601', -1, N'K07_591', 0, null),
(N'K07_571', N'I', 1, N'K07_601', 1, N'K07_591', 0, null),
(N'K07_581', N'C', -1, N'K07_601', -1, N'K07_591', 0, null),
(N'K07_591', N'C', 0, null, 0, null, 0, null),
(N'K07_600', N'U', 1, N'K07_700', 0, null, 0, null),
(N'K07_601', N'C', 1, N'K07_701', 0, null, 0, null),
(N'K07_620', N'T', 1, N'K07_700', 0, null, 0, null),
(N'K07_621', N'C', 1, N'K07_701', 0, null, 0, null),
(N'K07_700', N'U', 0, null, 0, null, 0, null),
(N'K07_703', N'T', 1, N'K07_706', 1, N'K07_704', 0, null),
(N'K07_704', N'T', 1, N'K07_707', 0, null, 0, null),
(N'K07_705', N'T', 1, N'K07_706', 0, null, 0, null),
(N'K07_705CO', N'T', 1, N'K07_705SU', 0, null, 0, null),
(N'K07_705GA', N'T', 1, N'K07_705SU', 0, null, 0, null),
(N'K07_705MS', N'T', 1, N'K07_705SU', 0, null, 0, null),
(N'K07_705OT', N'T', 1, N'K07_705SU', 0, null, 0, null),
(N'K07_705RD', N'T', 1, N'K07_705SU', 0, null, 0, null),
(N'K07_705SU', N'T', 1, N'K07_707', 0, null, 0, null),
(N'K07_706', N'T', 0, null, 0, null, 0, null),
(N'K07_707', N'T', 0, null, 0, null, 0, null),
(N'K07_710', N'U', 1, N'K07_760', 1, N'K07_7100', 1, N'K07_C050'),
(N'K07_7100', N'U', 1, N'K07_O710', 0, null, 0, null),
(N'K07_720', N'U', 1, N'K07_760', 1, N'K07_7200', 1, N'K07_C055'),
(N'K07_7200', N'U', 1, N'K07_O720', 0, null, 0, null),
(N'K07_7300', N'U', 1, N'K07_O730', 0, null, 0, null),
(N'K07_7400', N'U', 1, N'K07_O740', 0, null, 0, null),
(N'K07_7500', N'U', 1, N'K07_O750', 0, null, 0, null),
(N'K07_760', N'U', 1, N'K07_910', 1, N'K07_7600', 1, N'K07_C075'),
(N'K07_7600', N'U', 0, null, 0, null, 0, null),
(N'K07_800', N'U', 1, N'K07_810', 0, null, 0, null),
(N'K07_800N', N'U', 1, N'K07_810N', 0, null, 0, null),
(N'K07_805', N'U', 1, N'K07_810', 0, null, 0, null),
(N'K07_805N', N'U', 1, N'K07_810N', 0, null, 0, null),
(N'K07_810', N'U', 0, null, 0, null, 0, null),
(N'K07_810N', N'U', 0, null, 0, null, 0, null),
(N'K07_815', N'U', 1, N'K07_825', 0, null, 0, null),
(N'K07_815N', N'U', 1, N'K07_825N', 0, null, 0, null),
(N'K07_820', N'U', 1, N'K07_825', 0, null, 0, null),
(N'K07_820N', N'U', 1, N'K07_825N', 0, null, 0, null),
(N'K07_825', N'U', 0, null, 0, null, 0, null),
(N'K07_825N', N'U', 0, null, 0, null, 0, null),
(N'K07_830', N'U', 1, N'K07_840', 0, null, 0, null),
(N'K07_830N', N'U', 1, N'K07_840N', 0, null, 0, null),
(N'K07_835', N'U', 1, N'K07_840', 0, null, 0, null),
(N'K07_835N', N'U', 1, N'K07_840N', 0, null, 0, null),
(N'K07_840', N'U', 0, null, 0, null, 0, null),
(N'K07_840N', N'U', 0, null, 0, null, 0, null),
(N'K07_845', N'U', 1, N'K07_855', 0, null, 0, null),
(N'K07_845N', N'U', 1, N'K07_855N', 0, null, 0, null),
(N'K07_850', N'U', 1, N'K07_855', 0, null, 0, null),
(N'K07_850N', N'U', 1, N'K07_855N', 0, null, 0, null),
(N'K07_855', N'U', 0, null, 0, null, 0, null),
(N'K07_855N', N'U', 0, null, 0, null, 0, null),
(N'K07_860', N'U', 1, N'K07_870', 0, null, 0, null),
(N'K07_860N', N'U', 1, N'K07_870N', 0, null, 0, null),
(N'K07_865', N'U', 1, N'K07_870', 0, null, 0, null),
(N'K07_865N', N'U', 1, N'K07_870N', 0, null, 0, null),
(N'K07_870', N'U', 0, null, 0, null, 0, null),
(N'K07_870N', N'U', 0, null, 0, null, 0, null),
(N'K07_875', N'U', 1, N'K07_885', 0, null, 0, null),
(N'K07_875N', N'U', 1, N'K07_885N', 0, null, 0, null),
(N'K07_880', N'U', 1, N'K07_885', 0, null, 0, null),
(N'K07_880N', N'U', 1, N'K07_885N', 0, null, 0, null),
(N'K07_885', N'U', 0, null, 0, null, 0, null),
(N'K07_885N', N'U', 0, null, 0, null, 0, null),
(N'K07_900', N'U', -1, N'K07_920', 0, null, 0, null),
(N'K07_905', N'U', 1, N'K07_920', 0, null, 0, null),
(N'K07_910', N'U', -1, N'K07_940', 0, null, 0, null),
(N'K07_912', N'U', 1, N'K07_920', 0, null, 0, null),
(N'K07_915', N'U', 1, N'K07_920', 0, null, 0, null),
(N'K07_918', N'U', 1, N'K07_920', 0, null, 0, null),
(N'K07_920', N'U', 1, N'K07_940', 0, null, 0, null),
(N'K07_930', N'U', 1, N'K07_920', 0, null, 0, null),
(N'K07_940', N'U', 0, null, 0, null, 0, null),
(N'K07_C010', N'U', 1, N'K07_C045', 1, N'K07_C130', -1, N'K07_C050'),
(N'K07_C015', N'U', 1, N'K07_C045', 1, N'K07_C135', -1, N'K07_C055'),
(N'K07_C045', N'U', 0, null, 0, null, -1, N'K07_C075'),
(N'K07_C050', N'U', 0, null, 0, null, 0, null),
(N'K07_C055', N'U', 0, null, 0, null, 0, null),
(N'K07_C060', N'U', 0, null, 0, null, 0, null),
(N'K07_C065', N'U', 0, null, 0, null, 0, null),
(N'K07_C070', N'U', 0, null, 0, null, 0, null),
(N'K07_C075', N'U', 0, null, 0, null, 0, null),
(N'K07_C100', N'U', 1, N'K07_C125', 1, N'K07_C130', 0, null),
(N'K07_C105', N'U', 1, N'K07_C125', 1, N'K07_C135', 0, null),
(N'K07_C125', N'U', 0, null, 0, null, 0, null),
(N'K07_C130', N'U', 1, N'K07_C155', 1, N'K07_C630', 0, null),
(N'K07_C135', N'U', 1, N'K07_C155', 1, N'K07_C635', 0, null),
(N'K07_C155', N'U', 0, null, 0, null, 0, null),
(N'K07_C400', N'U', 1, N'K07_C425', 0, null, 0, null),
(N'K07_C405', N'U', 1, N'K07_C425', 0, null, 0, null),
(N'K07_C425', N'U', 0, null, 0, null, 0, null),
(N'K07_C430', N'U', 1, N'K07_C455', 1, N'K07_C580', 1, N'K07_C4300'),
(N'K07_C4300', N'U', 1, N'K07_O430', 0, null, 0, null),
(N'K07_C435', N'U', 1, N'K07_C455', 1, N'K07_C585', 1, N'K07_C4350'),
(N'K07_C4350', N'U', 1, N'K07_O435', 0, null, 0, null),
(N'K07_C4400', N'U', 1, N'K07_O440', 0, null, 0, null),
(N'K07_C4450', N'U', 1, N'K07_O445', 0, null, 0, null),
(N'K07_C4500', N'U', 1, N'K07_O450', 0, null, 0, null),
(N'K07_C455', N'U', 1, N'K07_C4550', 1, N'K07_C890', -1, N'K07_C810'),
(N'K07_C4550', N'U', 0, null, 0, null, 0, null),
(N'K07_C460', N'U', 1, N'K07_C485', -1, N'K07_C490', 1, N'K07_C580'),
(N'K07_C465', N'U', 1, N'K07_C485', -1, N'K07_C495', 1, N'K07_C585'),
(N'K07_C485', N'U', -1, N'K07_C515', 0, null, 0, null),
(N'K07_C490', N'U', 0, null, 0, null, 0, null),
(N'K07_C495', N'U', 0, null, 0, null, 0, null),
(N'K07_C500', N'U', 0, null, 0, null, 0, null),
(N'K07_C505', N'U', 0, null, 0, null, 0, null),
(N'K07_C510', N'U', 0, null, 0, null, 0, null),
(N'K07_C515', N'U', 0, null, 0, null, 0, null),
(N'K07_C520', N'U', 1, N'K07_C545', 1, N'K07_C490', 1, N'K07_C5200'),
(N'K07_C5200', N'U', 1, N'K07_O520', 0, null, 0, null),
(N'K07_C525', N'U', 1, N'K07_C545', 1, N'K07_C495', 1, N'K07_C5250'),
(N'K07_C5250', N'U', 1, N'K07_O525', 0, null, 0, null),
(N'K07_C5300', N'U', 1, N'K07_O530', 0, null, 0, null),
(N'K07_C5350', N'U', 1, N'K07_O535', 0, null, 0, null),
(N'K07_C5400', N'U', 1, N'K07_O540', 0, null, 0, null),
(N'K07_C545', N'U', 0, null, 1, N'K07_C515', 1, N'K07_C5450'),
(N'K07_C5450', N'U', 0, null, 0, null, 0, null),
(N'K07_C550', N'U', 1, N'K07_C575', 1, N'K07_C580', 0, null),
(N'K07_C555', N'U', 1, N'K07_C575', 1, N'K07_C585', 0, null),
(N'K07_C575', N'U', 0, null, 0, null, 0, null),
(N'K07_C580', N'U', 1, N'K07_C599', 1, N'K07_C730', 0, null),
(N'K07_C585', N'U', 1, N'K07_C599', 1, N'K07_C735', 0, null),
(N'K07_C599', N'U', 0, null, 0, null, 0, null),
(N'K07_C600', N'U', 1, N'K07_C625', 1, N'K07_C630', 0, null),
(N'K07_C605', N'U', 1, N'K07_C625', 1, N'K07_C635', 0, null),
(N'K07_C625', N'U', 0, null, 0, null, 0, null),
(N'K07_C630', N'U', 1, N'K07_C655', 0, null, 0, null),
(N'K07_C635', N'U', 1, N'K07_C655', 0, null, 0, null),
(N'K07_C655', N'U', 0, null, 0, null, 0, null),
(N'K07_C700', N'U', 1, N'K07_C725', 1, N'K07_C730', 0, null),
(N'K07_C705', N'U', 1, N'K07_C725', 1, N'K07_C735', 0, null),
(N'K07_C725', N'U', 0, null, 0, null, 0, null),
(N'K07_C730', N'U', 1, N'K07_C755', 0, null, 0, null),
(N'K07_C735', N'U', 1, N'K07_C755', 0, null, 0, null),
(N'K07_C755', N'U', 0, null, 0, null, 0, null),
(N'K07_C800', N'U', -1, N'K07_C890', 0, null, 0, null),
(N'K07_C810', N'U', 0, null, 0, null, 0, null),
(N'K07_C830', N'U', -1, N'K07_C890', 0, null, 0, null),
(N'K07_C870', N'U', -1, N'K07_C890', 0, null, 0, null),
(N'K07_C890', N'U', 0, null, 0, null, 0, null),
(N'K07_I110', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I120', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I130', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I140', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I150', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I160', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I170', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I180', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I190', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I200', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I290', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I295', N'T', 1, N'K07_I300', 0, null, 0, null),
(N'K07_I300', N'T', 0, null, 0, null, 0, null),
(N'K07_I310', N'C', 1, N'K07_I400', 0, null, 0, null),
(N'K07_I320', N'C', 1, N'K07_I400', 0, null, 0, null),
(N'K07_I400', N'C', 0, null, 0, null, 0, null),
(N'K07_O091', N'U', -1, N'K07_O251', 0, null, 0, null),
(N'K07_O100', N'U', 1, N'K07_O200', 0, null, 0, null),
(N'K07_O101', N'U', 1, N'K07_O202', 0, null, 0, null),
(N'K07_O115', N'U', 1, N'K07_O200', 0, null, 0, null),
(N'K07_O116', N'U', 1, N'K07_O200', 0, null, 0, null),
(N'K07_O117', N'U', 1, N'K07_O200', 0, null, 0, null),
(N'K07_O118', N'U', 1, N'K07_O200', 0, null, 0, null),
(N'K07_O120', N'U', 1, N'K07_O202', 0, null, 0, null),
(N'K07_O200', N'U', 1, N'K07_O250', 0, null, 0, null),
(N'K07_O202', N'U', 1, N'K07_O251', 0, null, 0, null),
(N'K07_O250', N'U', 1, N'K07_O300', 0, null, 0, null),
(N'K07_O251', N'U', 1, N'K07_O301', 0, null, 0, null),
(N'K07_O270', N'T', -1, N'K07_O300', 0, null, 0, null),
(N'K07_O271', N'T', -1, N'K07_O301', 0, null, 0, null),
(N'K07_O280', N'T', -1, N'K07_O300', 0, null, 0, null),
(N'K07_O281', N'T', -1, N'K07_O301', 0, null, 0, null),
(N'K07_O300', N'U', 0, null, 0, null, 0, null),
(N'K07_O301', N'U', 0, null, 0, null, 0, null),
(N'K07_O430', N'U', 1, N'K07_O455', 0, null, 0, null),
(N'K07_O435', N'U', 1, N'K07_O455', 0, null, 0, null),
(N'K07_O440', N'U', 1, N'K07_O455', 0, null, 0, null),
(N'K07_O445', N'U', 1, N'K07_O455', 0, null, 0, null),
(N'K07_O450', N'U', 1, N'K07_O455', 0, null, 0, null),
(N'K07_O455', N'U', 0, null, 0, null, 0, null),
(N'K07_O520', N'U', 1, N'K07_O545', 0, null, 0, null),
(N'K07_O525', N'U', 1, N'K07_O545', 0, null, 0, null),
(N'K07_O530', N'U', 1, N'K07_O545', 0, null, 0, null),
(N'K07_O535', N'U', 1, N'K07_O545', 0, null, 0, null),
(N'K07_O540', N'U', 1, N'K07_O545', 0, null, 0, null),
(N'K07_O545', N'U', 0, null, 0, null, 0, null),
(N'K07_O710', N'U', 1, N'K07_O760', 0, null, 0, null),
(N'K07_O720', N'U', 1, N'K07_O760', 0, null, 0, null),
(N'K07_O730', N'U', 1, N'K07_O760', 0, null, 0, null),
(N'K07_O740', N'U', 1, N'K07_O760', 0, null, 0, null),
(N'K07_O750', N'U', 1, N'K07_O760', 0, null, 0, null),
(N'K07_O760', N'U', 0, null, 0, null, 0, null),
(N'K07_P091', N'U', -1, N'K07_P251', 1, N'K07_O091', 0, null),
(N'K07_P091US', N'U', -1, N'K07_P251US', 0, null, 0, null),
(N'K07_P101', N'U', 1, N'K07_P1010', 1, N'K07_PP101', 0, null),
(N'K07_P115', N'U', 1, N'K07_O115', 1, N'K07_P200', 0, null),
(N'K07_P116', N'U', 1, N'K07_O116', 1, N'K07_P200', 0, null),
(N'K07_P117', N'U', 1, N'K07_O117', 1, N'K07_P200', 0, null),
(N'K07_P118', N'U', 1, N'K07_O118', 1, N'K07_P200', 0, null),
(N'K07_P120', N'U', 1, N'K07_O120', 1, N'K07_P202', 0, null),
(N'K07_P120US', N'U', 0, null, 1, N'K07_P202US', 0, null),
(N'K07_P200', N'U', 1, N'K07_P250', 0, null, 0, null),
(N'K07_P202', N'U', 1, N'K07_P251', 0, null, 0, null),
(N'K07_P202US', N'U', 1, N'K07_P251US', 0, null, 0, null),
(N'K07_P250', N'U', 1, N'K07_P300', 0, null, 0, null),
(N'K07_P251', N'U', 1, N'K07_P301', 0, null, 0, null),
(N'K07_P251US', N'U', 1, N'K07_P301US', 0, null, 0, null),
(N'K07_P270', N'T', -1, N'K07_P300', 1, N'K07_O270', 0, null),
(N'K07_P271', N'T', -1, N'K07_P301', 1, N'K07_O271', 0, null),
(N'K07_P271US', N'T', -1, N'K07_P301US', 0, null, 0, null),
(N'K07_P280', N'T', -1, N'K07_P300', 1, N'K07_O280', 0, null),
(N'K07_P281', N'T', -1, N'K07_P301', 1, N'K07_O281', 0, null),
(N'K07_P281US', N'T', -1, N'K07_P301US', 0, null, 0, null),
(N'K07_P300', N'U', 0, null, 0, null, 0, null),
(N'K07_P301', N'U', 0, null, 0, null, 0, null),
(N'K07_P301US', N'U', 0, null, 0, null, 0, null),
(N'K07_P430', N'U', 1, N'K07_O430', 1, N'K07_P455', 0, null),
(N'K07_P435', N'U', 1, N'K07_O435', 1, N'K07_P455', 0, null),
(N'K07_P440', N'U', 1, N'K07_O440', 1, N'K07_P455', 0, null),
(N'K07_P445', N'U', 1, N'K07_O445', 1, N'K07_P455', 0, null),
(N'K07_P450', N'U', 1, N'K07_O450', 1, N'K07_P455', 0, null),
(N'K07_P455', N'U', 0, null, 0, null, 0, null),
(N'K07_P489US', N'T', 0, null, 0, null, 0, null),
(N'K07_P520', N'U', 1, N'K07_O520', 1, N'K07_P545', 0, null),
(N'K07_P521US', N'T', 0, null, 0, null, 0, null),
(N'K07_P525', N'U', 1, N'K07_O525', 1, N'K07_P545', 0, null),
(N'K07_P530', N'U', 1, N'K07_O530', 1, N'K07_P545', 0, null),
(N'K07_P535', N'U', 1, N'K07_O535', 1, N'K07_P545', 0, null),
(N'K07_P540', N'U', 1, N'K07_O540', 1, N'K07_P545', 0, null),
(N'K07_P545', N'U', 0, null, 0, null, 0, null),
(N'K07_P710', N'U', 1, N'K07_O710', 1, N'K07_P760', 0, null),
(N'K07_P710US', N'U', 1, N'K07_P760US', 0, null, 0, null),
(N'K07_P720', N'U', 1, N'K07_O720', 1, N'K07_P760', 0, null),
(N'K07_P720US', N'U', 1, N'K07_P760US', 0, null, 0, null),
(N'K07_P730', N'U', 1, N'K07_O730', 1, N'K07_P760', 0, null),
(N'K07_P740', N'U', 1, N'K07_O740', 1, N'K07_P760', 0, null),
(N'K07_P750', N'U', 1, N'K07_O750', 1, N'K07_P760', 0, null),
(N'K07_P760', N'U', 0, null, 0, null, 0, null),
(N'K07_P760US', N'U', 0, null, 0, null, 0, null),
(N'K07_PA101', N'U', -1, N'K07_PP101', 0, null, 0, null),
(N'K07_PP101', N'U', 0, null, 0, null, 0, null);
My attempt now was to get the nested hierarchy with multiple joins. But this doesn't seem to do the trick.
with Hierarchy(AccountNumber, ParentsPaths)
as (
select ad1.AccountNumber,
cast(N'' as nvarchar(max))
from #AccountDefinitions as ad1
where ad1.SumAccount1 is null
union all
select ad2.AccountNumber,
cast(case when h.ParentsPaths = N''
then (cast(ad2.AccountNumber as nvarchar(max)))
else (h.ParentsPaths + '.'
+ cast(ad2.AccountNumber as nvarchar(max)))
end as nvarchar(max))
from #AccountDefinitions as ad2
inner join Hierarchy as h
on h.AccountNumber = ad2.SumAccount1
or h.AccountNumber = ad2.SumAccount2
or h.AccountNumber = ad2.SumAccount3
where ad2.SumAccount1 is not null
)
select *
from Hierarchy
order by len(ParentsPaths) desc
option (maxrecursion 0)
In the end I would need to get a list of all accounts and all intermediary steps where they were added to, so that I can join the resulting table with all financial bookings and get all values summed up together.
Example: for K07_1000 there is only one possible path being K07_1000 adds to K07_O100, which adds up to K07_O200 and finally ends in K07_O250. In order to be able to join this, I would need the result:
Original | Mapped
-------------------
K07_1000 | K07_1000 <-- preserve the original posting
K07_1000 | K07_O100 <-- the one and only path
K07_1000 | K07_O200
K07_1000 | K07_O250
K07_1000 | K07_O300
If I join this table on the booking table then I would get for one booking to account K07_1000 four resulting bookings: the original and three more (which would add to the summed up parent accounts).
For account K07_1010 it would be more difficult as it can take four different paths.
Original | Mapped
-------------------
K07_1010 | K07_1010 <-- the original posting
K07_1010 | K07_100 <-- first possible path
K07_1010 | K07_1000
K07_1010 | K07_O100
K07_1010 | K07_O200
K07_1010 | K07_O250
K07_1010 | K07_O300
K07_1010 | K07_101P <-- second possible path
K07_1010 | K07_O101
K07_1010 | K07_O202
K07_1010 | K07_O251
K07_1010 | K07_O301
K07_1010 | K07_C130 <-- third possible path
K07_1010 | K07_C155 <- third path with sub path 1
K07_1010 | K07_C630 <- third path with sub path 2
K07_1010 | K07_C655 <- third path with sub path 2
I hope this makes sense and someone can help.
Ralph, try this. I think it is working:
with Hierarchy
as (
select ad1.AccountNumber,
SumAccount1,
SumAccount2,
SumAccount3
from #AccountDefinitions as ad1
union all
select h.AccountNumber,
ad2.SumAccount1,
ad2.SumAccount2,
ad2.SumAccount3
from #AccountDefinitions as ad2
inner join Hierarchy as h
on h.SumAccount1 = ad2.AccountNumber
or h.SumAccount2 = ad2.AccountNumber
or h.SumAccount3 = ad2.AccountNumber
),
cter
as(
SELECT AccountNumber, AccountNumber AS SumAccount FROM #AccountDefinitions
UNION
select AccountNumber,
SumAccount1 as SumAccount
from Hierarchy
where SumAccount1 is not null
union
select AccountNumber,
SumAccount2 as SumAccount
from Hierarchy
where SumAccount2 is not null
union
select AccountNumber,
SumAccount3 as SumAccount
from Hierarchy
where SumAccount3 is not null)
select * from cter
WHERE AccountNumber = 'K07_1010'
option (maxrecursion 0)
Related
Seq sortWith function with strange behaviour
I was trying to sort elements of a Seq object with the sortWith function when I got an exception. I didn't use the sorted function because the code below is a simplification of the real code where the seq has tuples instead of ints. See below that in the last two cases, when comparing with (v1 <= v2) an exception is thrown, but when comparing with (v1 < v2) no exception is thrown. heitor#heitor-340XAA-350XAA-550XAA:~$ sbt console [info] welcome to sbt 1.6.2 (Ubuntu Java 11.0.11) [info] loading settings for project global-plugins from sbt-updates.sbt ... [info] loading global plugins from /home/heitor/.sbt/1.0/plugins [info] loading project definition from /home/heitor/project [info] loading settings for project root from build.sbt ... [info] set current project to example (in build file:/home/heitor/) [info] Starting scala interpreter... Welcome to Scala 2.13.8 (OpenJDK 64-Bit Server VM, Java 11.0.11). Type in expressions for evaluation. Or try :help. scala> val lst69 = List(1, 10, 4, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 3, 1, 4, 10, 1, 1, 3, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1) val lst69: List[Int] = List(1, 10, 4, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 3, 1, 4, 10, 1, 1, 3, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1) scala> lst69.size val res0: Int = 69 scala> val lst68 = lst69.take(68) val lst68: List[Int] = List(1, 10, 4, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 3, 1, 4, 10, 1, 1, 3, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1) scala> lst68.size val res1: Int = 68 scala> lst68.sorted val res2: List[Int] = List(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 10, 10) scala> lst68.sortWith{ case (v1,v2) => (v1 <= v2) } val res3: List[Int] = List(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 10, 10) scala> lst69.sorted val res4: List[Int] = List(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 10, 10) scala> lst69.sortWith{ case (v1,v2) => (v1 <= v2) } java.lang.IllegalArgumentException: Comparison method violates its general contract! at java.base/java.util.TimSort.mergeHi(TimSort.java:903) at java.base/java.util.TimSort.mergeAt(TimSort.java:520) at java.base/java.util.TimSort.mergeForceCollapse(TimSort.java:461) at java.base/java.util.TimSort.sort(TimSort.java:254) at java.base/java.util.Arrays.sort(Arrays.java:1441) at scala.collection.SeqOps.sorted(Seq.scala:700) at scala.collection.SeqOps.sorted$(Seq.scala:692) at scala.collection.immutable.List.scala$collection$immutable$StrictOptimizedSeqOps$$super$sorted(List.scala:79) at scala.collection.immutable.StrictOptimizedSeqOps.sorted(StrictOptimizedSeqOps.scala:78) at scala.collection.immutable.StrictOptimizedSeqOps.sorted$(StrictOptimizedSeqOps.scala:78) at scala.collection.immutable.List.sorted(List.scala:79) at scala.collection.SeqOps.sortWith(Seq.scala:727) at scala.collection.SeqOps.sortWith$(Seq.scala:727) at scala.collection.AbstractSeq.sortWith(Seq.scala:1161) ... 59 elided scala> lst69.sortWith{ case (v1,v2) => (v1 < v2) } val res6: List[Int] = List(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 10, 10) scala> ```
You can try instead: lst69.sortWith{ case (v1,v2) => (v1 < v2 || v1 == v2 ) }
MongoDB indexed text search only works for exact match
I have field 'user_name' populated with data. This code gives me no results: history = db.history history.create_index([('user_name', 'text')]) history.find({'$text' : {'$search' : 'a'}}) But when I specify the exact name, it works history.find({'$text' : {'$search' : 'exact name'}}) Here is the output of explain() for 'a' search: { "executionSuccess": true, "nReturned": 0, "executionTimeMillis": 0, "totalKeysExamined": 0, "totalDocsExamined": 0, "executionStages": { "stage": "TEXT", "nReturned": 0, "executionTimeMillisEstimate": 0, "works": 1, "advanced": 0, "needTime": 0, "needYield": 0, "saveState": 0, "restoreState": 0, "isEOF": 1, "indexPrefix": {}, "indexName": "user_name_text", "parsedTextQuery": { "terms": [], "negatedTerms": [], "phrases": [], "negatedPhrases": [] }, "textIndexVersion": 3, "inputStage": { "stage": "TEXT_MATCH", "nReturned": 0, "executionTimeMillisEstimate": 0, "works": 0, "advanced": 0, "needTime": 0, "needYield": 0, "saveState": 0, "restoreState": 0, "isEOF": 1, "docsRejected": 0, "inputStage": { "stage": "FETCH", "nReturned": 0, "executionTimeMillisEstimate": 0, "works": 0, "advanced": 0, "needTime": 0, "needYield": 0, "saveState": 0, "restoreState": 0, "isEOF": 1, "docsExamined": 0, "alreadyHasObj": 0, "inputStage": { "stage": "OR", "nReturned": 0, "executionTimeMillisEstimate": 0, "works": 0, "advanced": 0, "needTime": 0, "needYield": 0, "saveState": 0, "restoreState": 0, "isEOF": 1, "dupsTested": 0, "dupsDropped": 0 } } } }, "allPlansExecution": [] } Here is the output of explain() for exact match of username ('akkcess'): { "executionSuccess": true, "nReturned": 39, "executionTimeMillis": 1, "totalKeysExamined": 39, "totalDocsExamined": 39, "executionStages": { "stage": "TEXT", "nReturned": 39, "executionTimeMillisEstimate": 0, "works": 40, "advanced": 39, "needTime": 0, "needYield": 0, "saveState": 0, "restoreState": 0, "isEOF": 1, "indexPrefix": {}, "indexName": "user_name_text", "parsedTextQuery": { "terms": ["akkcess"], "negatedTerms": [], "phrases": [], "negatedPhrases": [] }, "textIndexVersion": 3, "inputStage": { "stage": "TEXT_MATCH", "nReturned": 39, "executionTimeMillisEstimate": 0, "works": 40, "advanced": 39, "needTime": 0, "needYield": 0, "saveState": 0, "restoreState": 0, "isEOF": 1, "docsRejected": 0, "inputStage": { "stage": "FETCH", "nReturned": 39, "executionTimeMillisEstimate": 0, "works": 40, "advanced": 39, "needTime": 0, "needYield": 0, "saveState": 0, "restoreState": 0, "isEOF": 1, "docsExamined": 39, "alreadyHasObj": 0, "inputStage": { "stage": "OR", "nReturned": 39, "executionTimeMillisEstimate": 0, "works": 40, "advanced": 39, "needTime": 0, "needYield": 0, "saveState": 0, "restoreState": 0, "isEOF": 1, "dupsTested": 39, "dupsDropped": 0, "inputStage": { "stage": "IXSCAN", "nReturned": 39, "executionTimeMillisEstimate": 0, "works": 40, "advanced": 39, "needTime": 0, "needYield": 0, "saveState": 0, "restoreState": 0, "isEOF": 1, "keyPattern": { "_fts": "text", "_ftsx": 1 }, "indexName": "user_name_text", "isMultiKey": false, "isUnique": false, "isSparse": false, "isPartial": false, "indexVersion": 2, "direction": "backward", "indexBounds": {}, "keysExamined": 39, "seeks": 1, "dupsTested": 0, "dupsDropped": 0 } } } } }, "allPlansExecution": [] } Do you have any idea why it behaves this way? According to docs and tutorials, this it should work.
"a" is almost surely a stop word. Almost every natural language text would include it. Therefore if it was searched for, you'd get every single document in the result set. Since this isn't very useful, text search drops stop words like "a" from the query. Separately, MongoDB text search does include exact matching functionality, but it requires the query to be quoted which you haven't done therefore you are using the regular stemmed matching, not exact matching in your posted query.
Numpy array: Conditional encoding
I have following numpy array: array([1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 2, 0, 1, 1, 2, 3, 3, 3, 3, 1, 1, 1, 1, 1, 3, 1, 1, 3, 0, 1, 3, 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 0, 1, 2, 0, 2, 2, 2, 1, 2, 2, 0, 2, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 3, 0, 2, 1, 1, 1, 1, 3, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 0, 2, 3, 2, 1, 1, 1, 1, 3, 1, 0]) Question: How can I create another array that encodes the data, given condition: If value = 3 or 2, then "1", else "0". I tried: from sklearn.preprocessing import label_binarize label_binarize(doc_topics, classes=[3,2])[:15] array([[0, 0], [0, 0], [0, 0], [1, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 1], [0, 0], [0, 0], [0, 0], [0, 1]]) However, this seems to return a 2-D array.
Use np.where and pass your condition to mask the elements of interest to set where the condition is met to 1, 0 otherwise: In[18]: a = np.where((a==3) | (a == 2),1,0) a Out[18]: array([0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0]) Here we compare the array with the desired values, and use the unary | to or the conditions, due to operator precedence we have to use parentheses () around the conditions. To do this using sklearn: In[68]: binarizer = preprocessing.Binarizer(threshold=1) binarizer.transform(a.reshape(1,-1)) Out[68]: array([[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0]]) This treats values above 1 as 1 and 0 otherwise, this only works for this specific data set as you want 2 and 3 to be 1, it won't work if you have other values, so the numpy method is more general
Probit model in winbugs
I conducted an analysis using a logit model and now want to do the same using a probit model. Can anyone please turn this winbugs logit model into a winbugs probit model? model { for (i in 1:n) { # Linear regression on logit logit(p[i]) <- alpha + b.sex*sex[i] + b.age*age[i] # Likelihood function for each data point frac[i] ~ dbern(p[i]) } alpha ~ dnorm(0.0,1.0E-4) # Prior for intercept b.sex ~ dnorm(0.0,1.0E-4) # Prior for slope of sex b.age ~ dnorm(0.0,1.0E-4) # Prior for slope of age } Data list(sex=c(1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1), age= c(69, 57, 61, 60, 69, 74, 63, 68, 64, 53, 60, 58, 79, 56, 53, 74, 56, 76, 72, 56, 66, 52, 77, 70, 69, 76, 72, 53, 69, 59, 73, 77, 55, 77, 68, 62, 56, 68, 70, 60, 65, 55, 64, 75, 60, 67, 61, 69, 75, 68, 72, 71, 54, 52, 54, 50, 75, 59, 65, 60, 60, 57, 51, 51, 63, 57, 80, 52, 65, 72, 80, 73, 76, 79, 66, 51, 76, 75, 66, 75, 78, 70, 67, 51, 70, 71, 71, 74, 74, 60, 58, 55, 61, 65, 52, 68, 75, 52, 53, 70), frac=c(1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1), n=100) Initial Values list(alpha=0, b.sex=1, b.age=1)
WinBUGS accepts multiple types of link functions (see page 15 in the WinBUGS manual). For a probit model, change your linear regression equation to: probit(p[i]) <- alpha + b.sex*sex[i] + b.age*age[i] I would recommend you center the age variable, otherwise you may well run into some convergence problems, so something like: probit(p[i]) <- alpha + b.sex*sex[i] + b.age*(age[i] - mean(age[])) Alternatively, for a probit model (if the probit functions gives you some trap errors) you could use the phi standard normal cdf function: p[i] <- phi(alpha + b.sex*sex[i] + b.age*(age[i] - mean(age[])))
easiest way to prototype a symbolic orthogonal matrix
I have 8 sins and cosines that I try to symbolically define as shown below using Matlab. My goal is to symbolically build a matrix H (accumulated Givens rotations matrix) of 8x8 using all these sins and cosines and end up seeing what the formula for this H orthogonal projection matrix is. I can do that using the code below conceptually G7*G6*...*G0*I where I is the Identity 8x8 and the Gi are the Givens rotation corresponding to elements (i:i+1,i:i+1). c_0 = sym('c_0'); c_1 = sym('c_1'); c_2 = sym('c_2'); c_3 = sym('c_3'); c_4 = sym('c_4'); c_5 = sym('c_5'); c_6 = sym('c_6'); c_7 = sym('c_7'); s_0 = sym('s_0'); s_1 = sym('s_1'); s_2 = sym('s_2'); s_3 = sym('s_3'); s_4 = sym('s_4'); s_5 = sym('s_5'); s_6 = sym('s_6'); s_7 = sym('s_7'); % create H orthogonal matrix using the sin and cos symbols % filling in the first rotation I=eye(9,9) H = I; H(1:2,1:2) = [c_0 -s_0; s_0 c_0] % build the 2nd rotation and update H G = I; G(2:3,2:3) = [c_1 -s_1; s_1 c_1] H = G*H % build the 3rd rotation and update H G = I; G(3:4,3:4) = [c_2 -s_2; s_2 c_2] H = G*H % build the 4rth rotation and update H G = I; G(4:5,4:5) = [c_3 -s_3; s_3 c_3] H = G*H % build the 5th rotation and update H G = I; G(5:6,5:6) = [c_4 -s_4; s_4 c_4] H = G*H % build the 6th rotation and update H G = I; G(6:7,6:7) = [c_5 -s_5; s_5 c_5] H = G*H % build the 7th rotation and update H G = I; G(7:8,7:8) = [c_6 -s_6; s_6 c_6] H = G*H % build the 8th rotation and update H G = I; G(8:9,8:9) = [c_7 -s_7; s_7 c_7] H = G*H The code fails with the following error and can't find how to fix this: The following error occurred converting from sym to double: Error using mupadmex Error in MuPAD command: DOUBLE cannot convert the input expression into a double array. If the input expression contains a symbolic variable, use the VPA function instead. Error in build_rotH_test (line 26) H(1:2,1:2) = [c_0 -s_0; s_0 c_0]
I solved it like this. Note I realized I need the transpose of each rotation so I can build and apply H'*x i.e. G7'*G6'*...*G0'*I that's why the sin signs are flipped in the solution. clear all; % defining 0 and 1 as symbols too, solves the problem sym_0 = sym('0'); sym_1 = sym('1'); c0 = sym('c0'); c1 = sym('c1'); c2 = sym('c2'); c3 = sym('c3'); c4 = sym('c4'); c5 = sym('c5'); c6 = sym('c6'); c7 = sym('c7'); s0 = sym('s0'); s1 = sym('s1'); s2 = sym('s2'); s3 = sym('s3'); s4 = sym('s4'); s5 = sym('s5'); s6 = sym('s6'); s7 = sym('s7'); % create H orthogonal matrix using the sin and cos symbols % filling in the first rotation I = repmat(sym_0,9,9); for i=1:9 I(i,i)=sym_1; end H = I H(1:2,1:2) = [c0 s0; -s0 c0] % build the 2nd rotation and update H G = I; G(2:3,2:3) = [c1 s1; -s1 c1] H = G*H; % build the 3rd rotation and update H G = I; G(3:4,3:4) = [c2 s2; -s2 c2] H = G*H; % build the 4rth rotation and update H G = I; G(4:5,4:5) = [c3 s3; -s3 c3] H = G*H; % build the 5th rotation and update H G = I; G(5:6,5:6) = [c4 s4; -s4 c4] H = G*H; % build the 6th rotation and update H G = I; G(6:7,6:7) = [c5 s5; -s5 c5] H = G*H; % build the 7th rotation and update H G = I; G(7:8,7:8) = [c6 s6; -s6 c6] H = G*H; % build the 8th rotation and update H G = I; G(8:9,8:9) = [c7 s7; -s7 c7] H = G*H and the output is: H = [ 1, 0, 0, 0, 0, 0, 0, 0, 0] [ 0, 1, 0, 0, 0, 0, 0, 0, 0] [ 0, 0, 1, 0, 0, 0, 0, 0, 0] [ 0, 0, 0, 1, 0, 0, 0, 0, 0] [ 0, 0, 0, 0, 1, 0, 0, 0, 0] [ 0, 0, 0, 0, 0, 1, 0, 0, 0] [ 0, 0, 0, 0, 0, 0, 1, 0, 0] [ 0, 0, 0, 0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