Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate using Pathfinder #72

Open
marcdotson opened this issue Jan 27, 2022 · 4 comments
Open

Investigate using Pathfinder #72

marcdotson opened this issue Jan 27, 2022 · 4 comments
Assignees

Comments

@marcdotson
Copy link
Owner

Let's document in Writing, using the mle-optimize branch, what cmdstanr is doing when $optimize() finds the MLE. What does this mean for hierarchical models?

@marcdotson
Copy link
Owner Author

Screen Shot 2022-08-12 at 4 00 17 PM

@marcdotson
Copy link
Owner Author

Instead, use the pathfinder branch to try and implement this new variational approximation method.

@marcdotson marcdotson changed the title Investigate using $optimize() to find the MLE Investigate using Pathfinder Aug 29, 2023
@marcdotson
Copy link
Owner Author

Currently requires using the release candidate:

cmdstanr::install_cmdstan(version = "2.33.0-rc1", cores = 4)

And possibly a workaround for it not being implemented in cmdstanr yet?

run_pathfinder = function(data,mod){
	data_file = tempfile(fileext='.json')
	output_file = tempfile(fileext='.csv')
	cmdstanr::write_stan_json(data,file=data_file)
	processx::run(
		command = mod$exe_file()
		, args = c(
			'pathfinder'
			, 'data'
			, paste0('file=',data_file)
			, 'output'
			, paste0('file=',output_file)
		)
		, echo_cmd = T
		, stdout = ""
		, stderr = "2>&1"
	)
	
	(
		paste0("grep '^[#l]' '",output_file,"'")
		%>% system(intern=T)
		%>% strsplit('\n')
		%>% unlist()
	) -> header
	header_nlines = which(stringr::str_starts(header,'lp'))
	found_samples_col_names = unlist(strsplit(header[header_nlines],','))
	(
		data.table::fread(
			cmd = paste0(
				"tail -n+"
				, header_nlines + 1
				, " '"
				, output_file
				, "' | grep -v '^[#]' --color=never"
			)
			, data.table = FALSE
			, sep = ','
			, header = F
			, col.names = found_samples_col_names
			, colClasses = list(numeric=1:length(found_samples_col_names))
		)
		%>% as_tibble()
	) ->
		out
	return(out)
}

@marcdotson
Copy link
Owner Author

That doesn't work. Keep an eye out for Pathfinder being implement in cmdstanpy first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants