Maximum likelihood programming in Stata - optimization

I am trying to learn ml programming in Stata. As a part of this I am running a program myprobit (the code is adopted from Maximum likelihood estimation with Stata by Gould, Pitblado, and Sribney).
capture program drop myprobit
program define myprobit
args todo b lnf g negH g1
tempvar xb lj
mleval `xb'=`b'
quietly{
gen double `lj'=normal(`xb') if $ML_y1==1
replace `lj'=normal(-`xb') if $ML_y1==0
mlsum `lnf'=ln(`lj')
if (`todo'==0|`lnf'>= .) exit
replace `g1'= normalden(`xb')/`lj' if $ML_y1==1
replace `g1'=-normalden(`xb')/`lj' if $ML_y1==0
mlvecsum `lnf' `g'=`g1', eq(1)
if (`todo'==1|`lnf'==>.)exit
mlmatsum `lnf' `negH'=`g1'*(`g1'+`xb'),eq(1,1)
}
end
sysuse cancer, clear
gen drug2=drug==2
gen drug3=drug==3
ml model d1 myprobit (died=drug2 drug3 age)
ml check
ml maximize
But, I got an error varlist required:
Here is a trace of its execution:
------------------------------------------------------------------------------
-> myprobit 1 __000000 __000001 __000002 __000003
- `begin'
= capture noisily version 11: myprobit 1 __000000 __000001 __000002 __000003
---------------------------------------------------------------------------------------------------------------------------------- begin myprobit ---
- args todo b lnf g negH g1
- tempvar xb lj
- mleval `xb'=`b'
= mleval __000005=__000000
- quietly{
- gen double `lj'=normal(`xb') if $ML_y1==1
= gen double __000006=normal(__000005) if died==1
- replace `lj'=normal(-`xb') if $ML_y1==0
= replace __000006=normal(-__000005) if died==0
- mlsum `lnf'=ln(`lj')
= mlsum __000001=ln(__000006)
- if (`todo'==0|`lnf'>= .) exit
= if (1==0|__000001>= .) exit
- replace `g1'= normalden(`xb')/`lj' if $ML_y1==1
= replace = normalden(__000005)/__000006 if died==1
varlist required
replace `g1'=-normalden(`xb')/`lj' if $ML_y1==0
mlvecsum `lnf' `g'=`g1', eq(1)
if (`todo'==1|`lnf'==>.)exit
mlmatsum `lnf' `negH'=`g1'*(`g1'+`xb'),eq(1,1)
}
------------------------------------------------------------------------------------------------------------------------------------ end myprobit ---
- `end'
= set trace off
------------------------------------------------------------------------------
Fix myprobit.
r(100);
end of do-file
Note: The program runs without an error if likelihood evaluator is changed to do.
Any suggestion in this regard will be highly appreciated.

You provided 5 arguments to your program, but 6 are needed. Hence local macro g1 is not defined, which bites when you try to replace the variable it names.
Stata is telling you some of this. The lines
- replace `g1'= normalden(`xb')/`lj' if $ML_y1==1
= replace = normalden(__000005)/__000006 if died==1
show that local macro g1 is interpreted as nothing, i.e. an empty string, so Stata complains because it expects a variable name after replace.
The line
if (`todo'==1|`lnf'==>.)exit
is also problematic, as the operator ==> should be >=.
These are the problems I noticed; there may be others.

Related

generating palindromes with John the Ripper

How can I configure John the Ripper to generate only mangled (Jumbo) palindromes from a word-list to crack a password hash?
(I've googled it but only found "how to avoid palindromes")
in john/john.conf (for e.g. 9 and 10 letter palindromes) -append the following rules at the end:
# End of john.conf file.
# Keep this comment, and blank line above it, to make sure a john-local.conf
# that does not end with \n is properly loaded.
[List.Rules:palindromes]
f
f D5
then run john with your wordlist plus the newly created "palindromes" rules:
$ john --wordlist=wordlist.lst --rules:palindromes hashfile.hash
rule f simply appends a reflection of itself to the current word from the wordlist, e.g. P4ss! -> P4ss!!ss4P
rule f D5 not only reflects the word but then deletes the 5th character, e.g. P4ss! -> P4ss!ss4P
I haven't found a way to "delete the middle character" so as of now, the rule has to be adjusted to the required length of palindromes, e.g. f D4 for length of 7, f D6 for length of 11 etc.
Edit: Possible solution for variable length (not tested yet):
f
Mr[6
M = Memorize current word, r = Reverse the entire word , [ = Delete first character, 6 = Prepend the word saved to memory to current word
With this approach the palindromes could additionally be "turned inside out" (word from wordlist at the end of the resulting palindrome instead of at beginning)
f
Mr[6
Mr]4
M = Memorize current word, r = Reverse the entire word , ] = Delete last character, 4 = Append the word saved to memory to current word

Lua - It is possible to stop inputs while "ex.sleep" is running?

Basic stuff that I can't figure out or find in internet:
The little code I'm using for tests is simple:
require("ex")
a = true
b = nil
while (a == true) do
b = io.read()
ex.sleep(5)
print(b)
end
Very simple. If I input "1" (I am using notepad++ and windows command prompt), it will wait 5 seconds and print it, then repeat. But my problem is... If I input more numbers during the 5 seconds of sleeping, it all will be executed automatically, in order, when the sleep ends.
Is it possible to stop that? I don't want any input being read during that time. Where these "ghost" inputs are stored?
You can control reading by means of "buffer size" argument in bytes:
b = io.read(1)
In this case reading completes after the first byte was taken from input. Rest input bytes will be available for the next "read" statement.
Important note: if you input "1" and press "Enter" then there will be 3 bytes for reading (including "\r\n").
See https://www.lua.org/pil/21.1.html for details.
In addition, you want to know a way to clean input buffer before next reading. This is easy: use io.read("*line") statement as follows:
b = io.read("*line") -- suppose, input is: "1234"
b = string.sub(b, 0, 1)
print(b) -- prints 1
b = io.read("*line") -- suppose, input is: "567"
b = string.sub(b, 0, 1)
print(b) -- prints 5
b = io.read("*line") -- suppose, input is: ""
b = string.sub(b, 0, 1)
print(b) -- prints empty string
io.read("*line") gets whole line from input, but you can take only the first character from it.

Pyomo scheduling

I am trying to formulate a flowshop scheduling problem in Pyomo. This is an Abstract model
Problem description
There are 3 jobs (chest, door and chair) and 3 machines (cutting, welding, packing in that order). Objective is to minimise the makespan. The python code and the data are as follows.
## flowshop.py ##
from pyomo.environ import *
flowshop = AbstractModel()
flowshop.jobs = Set()
flowshop.machines = Set()
flowshop.machinesN = Param()
flowshop.jobsN = Param()
flowshop.proc_T = Param(flowshop.jobs,
flowshop.machines,
within=NonNegativeReals)
flowshop.start_T = Var(flowshop.jobs,
flowshop.machines,
within=NonNegativeReals)
flowshop.makespan = Var(within=NonNegativeReals)
def makespan_rule(flowshop,i,j):
return flowshop.makespan >= flowshop.start_T[i,j]+flowshop.proc_T[i,j]
flowshop.makespan_cons = Constraint(flowshop.jobs,
flowshop.machines,
rule=makespan_rule)
def objective_rule(flowshop):
return flowshop.makespan
flowshop.objc = Objective(rule=objective_rule,sense=minimize)
## data.dat ##
set jobs := chest door chair ;
set machines := cutting welding packing ;
param: machinesN := 3 ;
param: jobsN := 3 ;
param proc_T:
cutting welding packing :=
chest 10 40 45
door 30 20 25
chair 05 30 15
;
I havent added all the constraints yet, I plan to add them after this issue gets fixed. In the code (flowhop.py) above, for the makespan_rule, I want the makespan to be more that the completion time of only the last machine.
Currently, it is set to be more than completion times of all the machines.
For that, I believe, I have to get the last index of the machines set.
For that, I tried flowshop.machines[-1], but it gives an error saying:
Cannot index unordered set machines
How do I solve this issue?
Thanks for the help.
PS - I am also struggling to model the binary variables used to define the precedence of a job. If you have any ideas regarding that, that would also be helpful.
As the error says Cannot index unordered sets, the set flowshop.machines is not ordered. One needs to provide ordered=True argument in the while declaring the set -
flowshop.machines = Set(ordered=True)
After this, one can access any element by normal indexing - flowshop.machines[i]
For the binary variables, one can declare them as -
c = flowshop.jobsN*(flowshop.jobsN-1)/2
flowshop.prec = Var(RangeSet(1,c),within=Binary)
Then, this variable can be used to decide the precedence between 2 jobs and to formulate the assignment constraints. The precedence variable corresponding to a pair of jobs can be found out using the indices of the jobs (for which the flowshop.jobs has to be an ordered set - flowshop.jobs = Set(ordered=True))

Code folding in RStudio: Creating hierarchy in the code

I'm writing R scripts in RStudio and I use the code folding a lot. I found that you can see the hierarchy of the folding by pressing cmd + shift + O. This is super helpful.
# to my dear love ---------------------------------------------------------
2+2
# yo man ====
x.2 = function (x) {x+2}
### I do love potatoes ####
See the result by pressing cmd + shift + O.
I don't understand how this is working because when I write the code below, I can create a subsection without text but not when there is text in it (using # ==== but not # yo man ====).
# to my dear love ---------------------------------------------------------
2+2
# ====
# yo man ====
### I do love potatoes ####
x.2 = function (x) {x+2}
data = "here is some data"
See the result by pressing cmd + shift + O.
You can see that under # to my dear love --------------------------------------------------------- everything under is shifted to the right! This is cool!
The question is thus, how could it be possible to create a hierarchy of sections that include text in it?
Is it a peculiar package or Emac that is doing this? How can I create subsections, with text, and see the hierarchy in the cmd + shift + O box?
How can I down shift a section (going to a higher section (say section 2) to a lower section (section 1), by decreasing the visual hierarchy in the right box?
As per Chris's answer subheaders within functions
RStudio Code Folding hierarchy only works within function definitions and if-else structures. For example:
# Section 1 ----
a <- 1
testfunct1 <- function () {
# sect in function=====
b <- 2
c <- 3
}
# Section 2 #####
d <- 4
# Section 3 =======
e <- 5
testfunct2 <- function () {
# sect in function 2 =====
f <- 6
testsubfunct2_1 <- function () {
# sect in subfunction 2_1 -----
if (a == 1) {
# section in if ----
g < 7
} else {
# section in else ----
h = 8
}
}
}
# Section 4 ####
j <- 9
Produces this outline:
I don't know why the if-else section labels do not line up.
Just discovered I could use various special characters across the top of my keyboard in combination with hyphens to produce a hierarchical look to the code section ToC. I chose the asterisk for this example, but you can use anything from the special character keys across the top to produce this look.

SAS Index function?

Can anyone explain why the below piece of code gives two different values?
87 data _null_;
88 length a b $14;
89 a = 'ABC.DEF (X=Y)';
90 b = 'X=Y';
91 x = index(a,b);
92 y = index('ABC.DEF (X=Y)','X=Y');
93 put x y;
94 run;
0 10
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
Thanks.
It seems this is an exact copy of the example on the SAS website, so it would have been helpful if you would have looked for an answer there first.
This is their explanation:
Example 2:
Removing Trailing Spaces When You Use the INDEX Function with the TRIM Function
The following example shows the results when you use the INDEX function with and without the TRIM function. If you use INDEX without the TRIM function, leading and trailing spaces are considered part of the excerpt argument. If you use INDEX with the TRIM function, TRIM removes trailing spaces from the excerpt argument as you can see in this example. Note that the TRIM function is used inside the INDEX function.
options nodate nostimer ls=78 ps=60;
data _null_;
length a b $14;
a='ABC.DEF (X=Y)';
b='X=Y';
q=index(a,b);
w=index(a,trim(b));
put q= w=;
run;
SAS writes the following output to the log:
q=0 w=10
Added based on mjsqu's comment:
data _null_;
length a b $14 c $3;
a='ABC.DEF (X=Y)';
b='X=Y';
c='X=Y';
x=index(a,b);
y=index(a,c);
z=index(a,trim(b));
d = "|" || a ||"|";
e = "|" || b ||"|";
f = "|" || c ||"|";
put d=;
put e=;
put f=;
put x= y= z=;
run;
d=|ABC.DEF (X=Y) |
e=|X=Y |
f=|X=Y|
x=0 y=10 z=10
You can see that b has a trailing space which is part of the string that the Index function will be looking for. Since in string a X=Y is followed by ) and not a space, this means it will not be found => q = 0. You can also see here that if you change the length of b to the actual lenght of the string you want to look for (3 in this case), it would give you the same outcome.