dlm package - Error in optim(parm, logLik, method = method, ...) : L-BFGS-B needs finite values of 'fn' - dlm

require(dlm)
start.vals = c(0,0,0)
names(start.vals) = c("lns2_obs", "lns2_alpha", "lns2_beta")
buildTVP <- function(parm, x.mat){
parm <- exp(parm)
return( dlmModReg(X=x.mat, dV=parm[1], dW=c(parm[2], parm[3])) )
}
TVP.mle = dlmMLE(y=k[,1], parm=start.vals, x.mat=k[,2], build=buildTVP, hessian=T)
in this code, k[,1] and k[,2] are 2 stocks prices. on TVP.mle line I got
Error in optim(parm, logLik, method = method, ...) : L-BFGS-B needs finite values of 'fn' " error.
k file link: https://drive.google.com/open?id=1scLaKRpSdmp-1T9qTp_5cEcBFnWKDAus
I could not find my mistake. Could you help me please?

Related

How to use mapFieldType with gdal.VectorTranslate

I'm trying to export a postgresql database into a .gpkg file, but some of my fields are lists, and ogr2ogr send me the message :
Warning 1: The output driver does not natively support StringList type for field my_field_name. Misconversion can happen. -mapFieldType can be used to control field type conversion.
But, as in the documentation, -mapFieldType is not a -lco, i don't find how to use it with the python version of gdal.VectorTranslate
here ma config :
gdal_conn = gdal.OpenEx(f"PG:service={my_pgsql_service}", gdal.OF_VECTOR)
gdal.VectorTranslate("path_to_my_file.gpkg"), gdal_conn,
SQLStatement=my_sql_query,
layerName=my_mayer_name,
format="GPKG",
accessMode='append',
)
so i've tried to add it in the -lco :
layerCreationOptions=["-mapFieldType StringList=String"]
but it didn't work
so i diged into the code of gdal, added a field mapFieldType=None into the VectorTranslateOptions function, and added into its code the following lines :
if mapFieldType is not None:
mapField_str = ''
i = 0
for k, v in mapFieldType.items():
i += 1
mapField_str += f"{k}={v}" if i == len(mapFieldType) else f"{k}={v},"
new_options += ['-mapFieldType', mapField_str]
And it worked, but is there an other way ?
And if not, where can i propose this feature ?
Thank you for your help

Changes in lua language cause error in ai script

When I run script in game, I got an error message like this:
.\AI\haick.lua:104: bad argument #1 to 'find' (string expected, got nill)
local haick = {}
haick.type = type
haick.tostring = tostring
haick.require = require
haick.error = error
haick.getmetatable = getmetatable
haick.setmetatable = setmetatable
haick.ipairs = ipairs
haick.rawset = rawset
haick.pcall = pcall
haick.len = string.len
haick.sub = string.sub
haick.find = string.find
haick.seed = math.randomseed
haick.max = math.max
haick.abs = math.abs
haick.open = io.open
haick.rename = os.rename
haick.remove = os.remove
haick.date = os.date
haick.exit = os.exit
haick.time = GetTick
haick.actors = GetActors
haick.var = GetV
--> General > Seeding Random:
haick.seed(haick.time())
--> General > Finding Script Location:
local scriptLocation = haick.sub(_REQUIREDNAME, 1, haick.find(_REQUIREDNAME,'/[^\/:*?"<>|]+$'))
Last line (104 in file) causes error and I don`t know how to fix it.
There are links to .lua files below:
https://drive.google.com/file/d/1F90v-h4VjDb0rZUCUETY9684PPGw7IVG/view?usp=sharing
https://drive.google.com/file/d/1fi_wmM3rg7Ov33yM1uo7F_7b-bMPI-Ye/view?usp=sharing
Help, pls!
When you use a function in Lua, you are expected to pass valid arguments for the function.
To use a variable, you must first define it, _REQUIREDNAME in this case is not available, haick.lua file is incomplete. The fault is of the author of the file.
Lua has a very useful reference you can use if you need help, see here

uninitialized variables and #Bugs:dic.set cannot be executed (is greyed out) issue in WinBUGS

I am running the following model using r2WinBUGS package from R.
model{
for (i in 1:Nshim) {
y.SHIM[i]~ dnorm(mu[i], tau)
mu[i] <- zeta[PID[i]]+beta0[T[PID[i]]]+beta1[T[PID[i]]]*TIME[i]+beta2[T[PID[i]]]*pow(TIME[i],2)
}
for ( k in 1:Mshim){
zeta[k]~dnorm(0, prec.zeta)
}
for ( j in 1:Mshim){
T[j]~ dcat(p[])
}
for ( k in 1:2){
beta0[k]~dnorm(0.0,1.0E-6)
beta1[k]~dnorm(0.0, 1.0E-6)
beta2[k]~dnorm(0.0, 1.0E-6)
}
prec.zeta~dgamma(0.1,0.01)
p[1:2]~ ddirch(alpha[])
alpha[1] <- 1
alpha[2] <- 1
tau~dgamma(0.1,0.1)
sig2<-1/tau
sig2.zeta<-1/prec.zeta
}
Considering the following initials for 3-chains.
init_2quad.SHIM_RI=function(){list(beta0=c(10,15),beta1=rnorm(2),beta2=rnorm(2),zeta=rgamma(70,1,1),T=rbinom(70,1,0.5)+1)
list(beta0=c(12,18),beta1=rnorm(2),beta2=rnorm(2),zeta=rgamma(70,1,1),T=rbinom(70,1,0.5)+1)
list(beta0=c(15,20),beta1=rnorm(2),beta2=rnorm(2),zeta=rgamma(70,1,1),T=rbinom(70,1,0.5)+1)
}
setwd("C:/Users/mnudd/Desktop/IndStudy/WinBUGS14")
mix_2quad.RI_res=bugs(data = SHIM_2quad,model.file = "mix_2Q_SHIM_RI.bug",
inits = init_2quad.SHIM_RI, parameters.to.save = c("beta0","beta1", "beta2", "zeta","T","sig2"),
n.chains = 3, n.iter = n.iter_2q.RI, n.burnin = n.burnin_2q.RI, n.thin = n.thin_2q.RI,
bugs.directory = "C:/Users/mnudd/Desktop/IndStudy/WinBUGS14",
debug=T,codaPkg=F)
Unfortunately each time I run, getting the following warning
this chain contains uninitialized variables
inits(3,C:/Users/mnudd/AppData/Local/Temp/RtmpWQM8aq/inits3.txt)
this chain contains uninitialized variables
gen.inits()
initial values generated, model initialized
thin.updater(10)
update(100)
set(beta0)
set(beta1)
set(beta2)
set(zeta)
set(T)
set(sig2)
set(deviance)
dic.set()
command #Bugs:dic.set cannot be executed (is greyed out)
update(1000)
coda(*,C:/Users/mnudd/AppData/Local/Temp/RtmpWQM8aq/coda)
How can identify which one is uninitialized and why the bugs can not be executed(grayed out)?
Any suggestion or help would be appreciated.

How can I signal parsing errors with LPeg?

I'm writing an LPeg-based parser. How can I make it so a parsing error returns nil, errmsg?
I know I can use error(), but as far as I know that creates a normal error, not nil, errmsg.
The code is pretty long, but the relevant part is this:
local eof = lpeg.P(-1)
local nl = (lpeg.P "\r")^-1 * lpeg.P "\n" + lpeg.P "\\n" + eof -- \r for winblows compat
local nlnoeof = (lpeg.P "\r")^-1 * lpeg.P "\n" + lpeg.P "\\n"
local ws = lpeg.S(" \t")
local inlineComment = lpeg.P("`") * (1 - (lpeg.S("`") + nl * nl)) ^ 0 * lpeg.P("`")
local wsc = ws + inlineComment -- comments count as whitespace
local backslashEscaped
= lpeg.P("\\ ") / " " -- escaped spaces
+ lpeg.P("\\\\") / "\\" -- escaped escape character
+ lpeg.P("\\#") / "#"
+ lpeg.P("\\>") / ">"
+ lpeg.P("\\`") / "`"
+ lpeg.P("\\n") -- \\n newlines count as backslash escaped
+ lpeg.P("\\") * lpeg.P(function(_, i)
error("Unknown backslash escape at position " .. i) -- this error() is what I wanna get rid of.
end)
local Line = lpeg.C((wsc + (backslashEscaped + 1 - nl))^0) / function(x) return x end * nl * lpeg.Cp()
I want Line:match(...) to return nil, errmsg when there's an invalid escape.
LPeg itself doesn't provide specific functions to help you with error reporting. A quick fix to your problem would be to make a protected call (pcall) to match like this:
local function parse(text)
local ok, result = pcall(function () return Line:match(text) end)
if ok then
return result
else
-- `result` will contain the error thrown. If it is a string
-- Lua will add additional information to it (filename and line number).
-- If you do not want this, throw a table instead like `{ msg = "error" }`
-- and access the message using `result.msg`
return nil, result
end
end
However, this will also catch any other error, which you probably don't want. A better solution would be to use LPegLabel instead. LPegLabel is an extension of LPeg that adds support for labeled failures. Just replace require"lpeg" with require"lpeglabel" and then use lpeg.T(L) to throw labels where L is an integer from 1-255 (0 is used for regular PEG failures).
local unknown_escape = 1
local backslashEscaped = ... + lpeg.P("\\") * lpeg.T(unknown_escape)
Now Line:match(...) will return nil, label, suffix if there is a label thrown (suffix is the remaining unprocessed input, which you can use to compute for the error position via its length). With this, you can print out the appropriate error message based on the label. For more complex grammars, you would probably want a more systematic way of mapping the error labels and messages. Please check the documentation found in the readme of the LPegLabel repository to see examples of how one may do so.
LPegLabel also allows you to catch the labels in the grammar by the way (via labeled choice); this is useful for implementing things like error recovery. For more information on labeled failures and examples, please check the documentation.

3Ds MAx Script for Reading Pixels from an image

We are trying to read the pixels from an uploaded Bitmap image, yet the line
aBrightness = (0.2126*aPixel[1].red) + (0.7152*aPixel[1].green) + (0.0722*aPixel[1].blue) always gives an error saying "Unknown property: "red" in undefined".
Our current script is:
aBitmap = selectBitMap caption:"Select a Bitmap"
Print(aBitmap.height)
Print(aBitmap.width)
aLength = aBitmap.height
aWidth = aBitmap.width
for i = 0 to (aLength - 10) by 10 do
(
for j = 0 to (aWidth - 10) by 10 do
(
Print(i)
Print(j)
aPixel = getPixels aBitmap [i,j] 1
aBrightness = (0.2126*aPixel[1].red) + (0.7152*aPixel[1].green) + (0.0722*aPixel[1].blue)
aBox = box pos:[i,j,0] width:0.1 length:0.1 height:aBrightness
)
)
We would really appreciate any help regarding this script.
You have your coordinates wrong. The X value goes first.
It should be
APixels = Getpixels aBitmap [j, i] 1
You can check to see if aPixel is undefined before using it.
aPixel = getPixels aBitmap [i,j] 1
if (aPixel == undefined) do ( format "ERROR!!! [%,%]\n" i, j to:listener; continue )
aBrightness = (0.2126*aPixel[1].red) + (0.7152*aPixel[1].green) + (0.0722*aPixel[1].blue)
This might help you figure where the bug is. Often a function will return 'undefined' to a variable so you need to check if it is undefined. In this case, once you fix the bug, you can remove this type of code since you will have eliminated undefined behavior. Notice I used "format" instead of "print", this is much nicer to use for just a tiny extra code.
I see two suspicious things to check.
1) Most indexing in maxscript starts with 1 not 0. Check the documentation.
2) As Rotem pointed out, [x,y], not [y,x]