a MCMC code in winbugs - bayesian

i have a problem with winbugs code. i start to learn it recently and now i want to write a code for predicting some variable with mcmc method. the equation is:
R=a1*U + a2*B + a3*D^a4 + a5*S^a6 + a7. i have all valuse of R,U,D,B,S and a1 to a7 are random variable and i want to estimate them. this is a code that i write and i know thats not true. can anyone help me to correct it?
model
{
for(i in 1 : N) {
R [i] <- a1 * U [i] + a2 * B [i] + a3 * pow(D [i] , a4) + a5 * pow( S [i] , a6) + a7
}
# priors:
a1 ~ dnorm(0.0,1.0E-4)
a2 ~ dnorm(0.0,1.0E-4)
a3 ~ dnorm(0.0,1.0E-4);
a4 ~ dnorm(0.0,1.0E-4)
a5 ~ dnorm(0.0,1.0E-4)
a6 ~ dnorm(0.0,1.0E-4)
a7 ~ dnorm(0.0,1.0E-4)
}
list(N=120, R = c( 2.19, 2.12, 1.88, 2.81, 2.2, 2.37, 2.34, 2.9, 3.04, 3.07, 3.04, 2.95, 2.66, 2.3, 2.17, 2.87, 2.48, 2.02, 1.87, 2, 2.45, 2.18, 2.17, 2.09, 2.1, 2.05, 2.6, 2.46, 2.2, 2.14, 2.78, 2.03, 2.05, 2.47, 2.05, 2.47, 1.83, 2.08, 1.87, 2.1, 2.31, 2.43, 1.79, 2.17, 2.17, 1.65, 1.75, 1.87, 1.78, 1.78, 2.39, 1.28, 1.85, 2.2, 2.43, 2.13, 1.71, 2.04, 2.39, 2.14, 1.88, 1.88, 1.27, 2.14, 1.77, 2.14, 1.6, 2.05, 2.44, 2.42, 1.67, 2.12, 2.17, 2.28, 2.12, 2.47, 2, 1.5, 2.47, 2.35, 2.07, 2.28, 2.16, 2.61, 1.91, 2.26, 1.85, 1.58, 1.51, 2.36, 1.82, 1.5, 2.05, 1.84, 1.38, 1.96, 1.85, 1.95, 1.6, 1.88, 1.88, 2.11, 1.64, 1.51, 2.17, 2.16, 1.98, 2.09, 1.77, 2, 1.84, 2.15, 1.7, 1.46, 2.23, 1.87, 1.82, 2.64, 1.94, 1.97
),
U = c( 199.7, 199.3, 199, 189.7, 189, 168.3, 174.1, 177.9, 180.7, 184.1, 192.4, 191, 191, 194.5, 195.2, 188.3, 188.4, 193.1, 182.8, 182.4, 182.4, 192.7, 192.6, 164.1, 160.7, 160.7, 144.8, 140, 137.2, 136.6, 135.2, 136.2, 137, 137.4, 137, 137.4, 139.3, 139.4, 141, 140.7, 139.7, 138.9, 136.5, 134.2, 131.4, 130, 128.6, 130, 131, 131, 129, 130, 130.1, 130.2, 138.6, 138.8, 138.8, 138.7, 138.6, 137.4, 137.2, 136.2, 135.2, 134.8, 134.1, 133.3, 133.4, 134.1, 134.3, 134.3, 134.3, 133.9, 133.8, 143.4, 145.4, 146.2, 145.9, 137.5, 140.7, 145.5, 158.6, 159, 159.3, 159.3, 160.3, 159.6, 153.8, 154.5, 157.9, 160.3, 163.2, 164.3, 166.4, 173.1, 173.1, 174.1, 174.5, 175.2, 176, 176.8, 176.8, 177, 175.9, 175.2, 173.1, 156.9, 150.7, 147.6, 147, 145.6, 144.8, 140.7, 125.5, 123.1, 122.3, 121.7, 121.7, 121.7, 120.7, 119.7
),
B= c( 55, 55, 55, 56, 56, 58, 58, 58, 57, 57, 54, 54, 53, 52, 52, 46, 45, 43, 39, 39, 39, 42, 42, 46, 46, 46, 42, 43, 42, 41, 41, 40, 39, 39, 39, 39, 38, 38, 38, 38, 37, 37, 35, 34, 34, 34, 32, 32, 32, 32, 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 32, 32, 32, 33, 33, 33, 33, 34, 35, 35, 36, 36, 38, 37, 36, 36, 36, 35, 36, 35, 33, 33, 32, 32, 32, 30, 31, 31, 31, 31, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 29, 29, 29, 28, 29, 29, 29, 29, 30, 30, 29, 29
),
D = c( 0.8, 1.6, 2, 0.2, 2, 1.6, 2, 0.4, 0.2, 0.4, 0.4, 0.4, 0.1, 0.4, 0.4, 0.2, 0.2, 0.8, 0.8, 0.8, 0.4, 0.4, 0.2, 0.8, 1.6, 1.6, 0.2, 0.1, 1.6, 0.8, 0.4, 0.4, 1.6, 0.8, 1.6, 0.8, 2, 2, 1.6, 1.6, 1.6, 0.8, 1.6, 2, 2, 1.6, 1.6, 1.6, 0.4, 0.4, 0.8, 0.8, 0.8, 0.8, 0.4, 0.8, 0.4, 0.8, 0.2, 1.6, 0.8, 0.4, 1.6, 0.4, 1.6, 0.8, 1.6, 0.8, 0.4, 0.4, 1.6, 0.4, 0.4, 1.6, 1.6, 0.4, 0.8, 1.6, 0.1, 0.8, 0.1, 0.2, 0.8, 0.8, 0.8, 0.4, 0.8, 2, 1.6, 0.1, 0.8, 0.4, 0.4, 0.8, 0.8, 0.8, 0.1, 0.4, 2, 1.6, 1.6, 0.4, 2, 2, 1.6, 0.8, 1.6, 2, 2, 1.6, 2, 1.6, 2, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.2
),
S = c( 25, 21, 20, 42, 40, 41, 35, 61, 55, 49, 32, 34, 23, 33, 30, 29, 24, 41, 20, 66, 55, 55, 59, 19, 66, 31, 67, 46, 70, 62, 29, 32, 21, 46, 21, 46, 41, 40, 10, 28, 36, 47, 14, 41, 44, 15, 10, 15, 2, 11, 39, 2, 26, 32, 58, 55, 8, 17, 50, 69, 67, 45, 7, 62, 76, 47, 18, 19, 30, 40, 4, 81, 60, 33, 71, 32, 61, 10, 15, 52, 36, 74, 35, 78, 26, 20, 52, 52, 10, 70, 21, 4, 74, 33, 11, 37, 4, 56, 71, 88, 77, 89, 66, 79, 63, 51, 81, 44, 86, 72, 70, 68, 70, 10, 57, 36, 56, 80, 68, 60
))
list(a7 = 2, a1= 0, a2 = 0, a3 = 0,
a4 = 0, a5= 0, a6 = 0)
a1, a2, a3, a4, a5, a6, a7

This looks like you're doing regression where you'd do:
Where is your function of a1, a2, etc. However, your model implies:
The big difference is that there is no error or uncertainty in your model. You can change that by doing:
model{
for(i in 1:N) {
mu[i] <- a1 * U [i] + a2 * B [i] + a3 * pow(D [i] , a4) + a5 * pow( S [i] , a6) + a7
R [i] ~ dnorm(mu[i], prec)
}
# priors:
prec ~ dgamma(.001, .001)
a1 ~ dnorm(0.0,1.0E-4)
a2 ~ dnorm(0.0,1.0E-4)
a3 ~ dnorm(0.0,1.0E-4)
a4 ~ dnorm(0.0,1.0E-4)
a5 ~ dnorm(0.0,1.0E-4)
a6 ~ dnorm(0.0,1.0E-4)
a7 ~ dnorm(0.0,1.0E-4)
}
Note that this also introduces a new parameter, the standard deviation of the distribution, modelled here as precision.

Related

How to merge classes in multiclass image segmentation

I am performing an image segmentation with a u-net model.
My mask has classes from 0-50.
I also have a text file dictionary with codes representing each class.
For example -
{1: '1234', 2:'5678', 3:'1245'} etc.
How do I combine when the 2 first string characters are the same so for example above key 1 and 3 are the same because they both start with "12".
How can I do this for all classes?
firstTwoCharDict = {}
for key, value in dictionary.items():
if key == 0:
value == value
firstTwoCharDict[key] = value
else:
value = value[:2]
firstTwoCharDict[key] = value
newDict = {}
for key, value in firstTwoCharDict.items():
if value not in newDict:
newDict[value] = [key]
else:
newDict[value].append(key)
This provides this
{'62': [1, 39],
'90': [2, 5, 9, 20, 32, 42, 47, 72, 88, 91, 95],
'97': [3, 49, 55],
'98': [4, 24, 34, 40, 53, 76, 81, 90, 96],
'31': [6, 17, 30, 48, 83],
'69': [7, 13, 15, 16, 27, 44, 51, 54, 56, 75],
'79': [8, 50],
'71': [10, 19, 22, 35, 61, 63, 65],
'99': [11, 12, 21, 46, 52, 69, 78, 84, 89],
'48': [14, 36, 74],
'60': [18],
'64': [23, 38, 66, 97]
```
Now i have an 2d array with integers, how do I replace them with they keys if the array values are equal to the values in the dict?

Null values at the end of rows after INSERT INTO

I am currently trying to INSERT INTO my SQL database a row of 144 columns.
The problem is that the last 10 values of the new row are NULL while they are supposed to be float and int.
That's an example of what I have in my DB after the INSERT INTO :
First column
Before last column
Last column
1
NULL
NULL
That's the SQL request I am using
INSERT INTO "historic_data2"
VALUES (28438, 163, 156, 1, 'FIST 2', 91, 81, 82, 84, 90, 6, '2 Pts Int M', 'Offensive', 0, '91_81_82_84_90', 86, 85, 0, 36, 62, 24, 0, 132, 86, 0, 83, 0, 0, 0, 0, 42, 77, 24, 0, 173, 107, 0, 204, 0, 0, 0, 0, 42, 77, 24, 0, 173, 107, 0, 204, 0, 0, 0, 81, 62, 34, 23, 19, 45, 32, 18, 9, 19, 0.5555555555555556, 0.5161290322580645, 0.5294117647058824, 0.391304347826087, 1.0, 82, 54, 34, 18, 28, 49, 27, 17, 8, 28, 0.5975609756097561, 0.5, 0.5, 0.4444444444444444, 1.0, 302, 233, 132, 89, 69, 168, 116, 69, 35, 69, 0.5562913907284768, 0.4978540772532189, 0.5227272727272727, 0.39325842696629215, 1.0, 214, 161, 84, 73, 53, 119, 79, 39, 36, 53, 0.5560747663551402, 0.4906832298136646, 0.4642857142857143, 0.4931506849315068, 1.0, 717, 544, 298, 233, 173, 416, 285, 175, 97, 173, 0.5801952580195258, 0.5238970588235294, 0.587248322147651, 0.41630901287553645, 1.0, 466, 315, 183, 128, 151, 357, 233, 138, 91, 151, 0.7660944206008584, 0.7396825396825397, 0.7540983606557377, 0.7109375,1.0,112)
I can't figure out how to solve this issue. My guess would be that there is a hard limit on how much column you can insert at once but I don't know how to solve that.
Thank you in advance for your help

I need to filter the column from the beginning of a sentence

In my code, I can filter a column from exact texts, and it works without problems. However, it is necessary to filter another column with the beginning of a sentence.
The phrases in this column are:
A_2020.092222
A_2020.090787
B_2020.983898
B_2020.209308
So, I need to receive everything that starts with A_20 and B_20.
Thanks in advance
My code:
from bs4 import BeautifulSoup
import pandas as pd
import zipfile, urllib.request, shutil, time, csv, datetime, os, sys, os.path
#location
dt = datetime.datetime.now()
file_csv = "/home/Downloads/source.CSV"
file_csv_new = "/var/www/html/Data/Test.csv"
#open CSV
with open(file_csv, 'r', encoding='CP1251') as file:
reader = csv.reader(file, delimiter=';')
data = list(reader)
#list to dataframe
df = pd.DataFrame(data)
#filter UF
df = df.loc[df[9].isin(['PR','SC','RS'])]
#filter key
# A_ & B_
df = df.loc[df[35].isin(['A_20','B_20'])]
#print (df)
#Empty DataFrame
#Columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, ...]
#Index: []
#[0 rows x 119 columns]```
Give the following a try:
lst1 = ['A_2020.092222', 'A_2020.090787 ', 'B_2020.983898', 'B_2020.209308', 'C_2020.209308', 'D_2020.209308']
df = pd.DataFrame(lst1, columns =['Name'])
df.loc[df.Name.str.startswith(('A_20','B_20'))]

Convert ECC PKCS#8 public and private keys to traditional format

I have ECC public and private generated with BouncyCastle:
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
ECNamedCurveParameterSpec ecSpec = ECNamedCurveTable
.getParameterSpec("secp192r1");
KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", "BC");
g.initialize(ecSpec, new SecureRandom());
KeyPair pair = g.generateKeyPair();
System.out.println(Arrays.toString(pair.getPrivate().getEncoded()));
System.out.println(Arrays.toString(pair.getPublic().getEncoded()));
byte[] privateKey = new byte[]{48, 123, 2, 1, 0, 48, 19, 6, 7, 42, -122, 72, -50, 61, 2, 1, 6, 8, 42, -122, 72, -50, 61, 3, 1, 1, 4, 97, 48, 95, 2, 1, 1, 4, 24, 14, 117, 7, -120, 15, 109, -59, -35, 72, -91, 99, -2, 51, -120, 112, -47, -1, -115, 25, 48, -104, -93, 78, -7, -96, 10, 6, 8, 42, -122, 72, -50, 61, 3, 1, 1, -95, 52, 3, 50, 0, 4, 64, 48, -104, 32, 41, 13, 1, -75, -12, -51, -24, -13, 56, 75, 19, 74, -13, 75, -82, 35, 1, -50, -93, -115, -115, -34, -81, 119, -109, -50, -39, -57, -20, -67, 65, -50, 66, -122, 96, 84, 117, -49, -101, 54, -30, 77, -110, -122}
byte[] publicKey = new byte[]{48, 73, 48, 19, 6, 7, 42, -122, 72, -50, 61, 2, 1, 6, 8, 42, -122, 72, -50, 61, 3, 1, 1, 3, 50, 0, 4, 64, 48, -104, 32, 41, 13, 1, -75, -12, -51, -24, -13, 56, 75, 19, 74, -13, 75, -82, 35, 1, -50, -93, -115, -115, -34, -81, 119, -109, -50, -39, -57, -20, -67, 65, -50, 66, -122, 96, 84, 117, -49, -101, 54, -30, 77, -110, -122}
How to convert them into traditional format which can be reused later in https://github.com/kmackay/micro-ecc/blob/master/uECC.h? I need 24 bytes private and 48 public key while now it is 125 and 75.
Gives 24 and 48, sometimes when 0 is added at the beginning 25 or 49:
ECPrivateKey ecPrivateKey = (ECPrivateKey)privateKey;
System.out.println(ecPrivateKey.getS().toByteArray().length);
ECPublicKey ecPublicKey = (ECPublicKey)publicKey;
System.out.println(ecPublicKey.getW().getAffineX().toByteArray().length + ecPublicKey.getW().getAffineY().toByteArray().length);

BytesArray. ObjC to Swift

I am trying to write Swift implementation of the following ObjC(header file) code.
#include <stddef.h>
#ifndef VO_CERTIFICATE_TYPE
#define VO_CERTIFICATE_TYPE
typedef struct _voCertificate
{
const char* bytes;
size_t length;
}
voCertificate;
#endif
static const char myCertificate_BYTES[] =
{
103, 92, -99, 33, 72, 48, 119, -72,
-77, 75, -88, 81, 113, -46, -119, -119,
5, 42, -33, 94, 23, 3, -112, 34,
-63, 75, -77, 26, -41, -69, 50, 71,
19, 121, 109, -60, 40, 18, 46, -86,
..........
};
voCertificate const myCertificate =
{
myCertificate_BYTES,
sizeof(myCertificate_BYTES)
};
//////////////////////////////////////
NSData *certificate = [NSData dataWithBytes:myCertificate.bytes length:myCertificate.length];
My best assumption was:
let myCertificate = [
103, 92, -99, 33, 72, 48, 119, -72,
-77, 75, -88, 81, 113, -46, -119, -119,
5, 42, -33, 94, 23, 3, -112, 34,
-63, 75, -77, 26, -41, -69, 50, 71,
19, 121, 109, -60, 40, 18, 46, -86,
........................]
var certificate = NSData(bytes: myCertificate as [Byte], length: myCertificate.count)
I tried to reach ObjC variable through Bridging-Header too, but there was "Undefined symbols for architecture armv7" error.
I would really appreciate any help.
Your biggest problem is that the type of your myCertificate array is Int not Int8. Here is something that is working for me. Note I reconstructed the array from the NSData object to see if everything came out ok.
let myCertificate = Array<Int8>(arrayLiteral:
103, 92, -99, 33, 72, 48, 119, -72,
-77, 75, -88, 81, 113, -46, -119, -119,
5, 42, -33, 94, 23, 3, -112, 34,
-63, 75, -77, 26, -41, -69, 50, 71,
19, 121, 109, -60, 40, 18, 46, -86)
var certificate = NSData(bytes: myCertificate, length: myCertificate.count)
var buffer = [Int8](count: certificate.length, repeatedValue: 0)
certificate.getBytes(&buffer, length: certificate.length)